# Pointers and arrays relationship quiz

### Pointers and Array in C - relationship and use - Codeforwin A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and. Note that, array and pointers are closely related to each other; in the other words, we will discuss the array in details along with it's relationship with pointer. In C programming, pointers and array shares a very close relationship. You can use array name as a pointer pointing at zeroth element of array.

For example to access zeroth element we use arr, similarly to access fifth element we use arr. Let us transform things in the context of pointers. In C programming, array exhibits a special behaviour. Whenever you refer an array name directly, is behaves a pointer pointing at zeroth array element. Which means both of the below statements are equivalent. Things such as you can interchangeably use array and pointers. You can also use array name as a pointer pointing at zeroth element.

In context of array and pointer, all of the following statements are valid. For the above case I have assumed integer size as 4 bytes. Hence, next array element i.

### Using Pointers with Arrays - The Basics of C Programming | HowStuffWorks

Since array elements are stored sequentially, hence you can easily apply pointer arithmetic to iterate though elements. You can use following pointer arithmetic operations to access array elements. This is because scanf function accepts memory address and pointer variable contains memory address. Hence, there we can use pointer variable directly in scanf. Is name of array is constant pointer to zeroth array element?

As I spoke earlier whenever you use array name directly, the C compiler assumes it as a pointer pointing at zeroth array element.

### Aptitude Test On C-Programming (Pointers And Strings) SET -A - ProProfs Quiz

This pointer is not modifiable. Therefore, you may think it as a const pointer. Constant pointer is a special pointer whose value cannot be modified. Means a pointer that once point to a memory location cannot point to another memory location later in the program. Let us write a C program to prove array name behaves as a constant pointer in C. Which is a proof that the array name as a pointer is not modifiable.

How much higher depends on the array's data type char, int, float, and so forth. Take an array of type int. As you learned on Day 3, "Storing Data: Variables and Constants," a single int variable can occupy two bytes of memory. Each array element is therefore located two bytes above the preceding element, and the address of each array element is two higher than the address of the preceding element.

## Learning C++ Pointers for REAL Dummies

A type float, on the other hand, can occupy four bytes. In an array of type float, each array element is located four bytes above the preceding element, and the address of each array element is four higher than the address of the preceding element. Array storage for different array types.

By looking at Figure 9. You can also see that x is at the address of Line 2 shows this too. It can be read like this: Lines 4, 5, and 6 are virtually identical to 1, 2, and 3, respectively. They vary in the difference between the addresses of the two array elements. In the type int array x, the difference is two bytes, and in the type float array, expenses, the difference is four bytes.

How do you access these successive array elements using a pointer? You can see from these examples that a pointer must be increased by 2 to access successive elements of a type int array, and by 4 to access successive elements of a type float array. You can generalize and say that to access successive elements of an array of a particular data type, a pointer must be increased by sizeof datatype. Remember from Day 3 that the sizeof operator returns the size in bytes of a C data type.

Displaying the addresses of successive array elements. The exact addresses that your system displays might be different from these, but the relationships are the same.

In this output, there are two bytes between int elements, four bytes between float elements, and eight bytes between double elements. Some machines use different sizes for variable types. If your machine differs, the preceding output might have different-size gaps; however, they will be consistent gaps. This listing takes advantage of the escape characters discussed on Day 7, "Fundamentals of Input and Output. Looking more closely at Listing 9. Line 8 declares array i of type int, line 9 declares array f of type float, and line 10 declares array d of type double.

Line 16 prints the column headers for the table that will be displayed. Lines 18 and 19, along with lines 27 and 28, print dashed lines across the top and bottom of the table data. This is a nice touch for a report. Lines 23, 24, and 25 are a for loop that prints each of the table's rows. The number of the element x is printed first. This is followed by the address of the element in each of the three arrays. Pointer Arithmetic You have a pointer to the first array element; the pointer must increment by an amount equal to the size of the data type stored in the array. How do you access array elements using pointer notation? You use pointer arithmetic. Pointer arithmetic is simple, and it makes using pointers in your programs much easier. You have to be concerned with only two pointer operations: Incrementing Pointers When you increment a pointer, you are increasing its value.

For example, when you incre-ment a pointer by 1, pointer arithmetic automatically increases the pointer's value so that it points to the next array element. In other words, C knows the data type that the pointer points to from the pointer declaration and increases the address stored in the pointer by the size of the data type.

The same holds true for increments greater than 1. If you add the value n to a pointer, C increments the pointer by n array elements of the associated data type. Decrementing Pointers The same concepts that apply to incrementing pointers hold true for decrementing pointers.

Decrementing a pointer is actually a special case of incrementing by adding a negative value.

Pointers and Arrays in C (HINDI)

By incrementing pointers, the program can step through all the elements of the arrays efficiently. Using pointer arithmetic and pointer notation to access array elements. In this program, a defined constant named MAX is set to 10 in line 5; it is used throughout the listing. The elements in this array are initialized at the same time that the array is declared. Line 13 declares two additional int variables. The other variable is a simple type int variable named count.

In line 17, a second array is defined and initialized. This array is of type float, contains MAX values, and is initialized with float values. The main function is on lines 23 through The program assigns the beginning address of the two arrays to the pointers of their respective types in lines 27 and Remember, an array name without a subscript is the same as the address of the array's beginning. A for statement in lines 32 and 33 uses the int variable count to count from 0 to the value of MAX. For each count, line 33 dereferences the two pointers and prints their values in a printf function call. The increment operator then increments each of the pointers so that each points to the next element in the array before continuing with the next iteration of the for loop.

You might be thinking that this program could just as well have used array subscript notation and dispensed with pointers altogether.

This is true, and in simple programming tasks like this, the use of pointer notation doesn't offer any major advantages. As you start to write more complex programs, however, you should find the use of pointers advantageous. Remember that you can't perform incrementing and decrementing operations on pointer constants. An array name without brackets is a pointer constant. Also remember that when you're manipulating pointers to array elements, the C compiler doesn't keep track of the start and finish of the array.

If you're not careful, you can increment or decrement the pointer so that it points somewhere in memory before or after the array. Something is stored there, but it isn't an array element. You should keep track of pointers and where they're pointing. Other Pointer Manipulations The only other pointer arithmetic operation is called differencing, which refers to subtracting two pointers. If you have two pointers to different elements of the same array, you can subtract them and find out how far apart they are.

Again, pointer arithmetic automatically scales the answer so that it refers to array elements. Thus, if ptr1 and ptr2 point to elements of an array of any typethe following expression tells you how far apart the elements are: Lower array elements that is, those having a lower subscript always have a lower address than higher array elements.

This covers all allowed pointer operations. Many arithmetic operations that can be performed with regular variables, such as multiplication and division, don't make sense with pointers. The C compiler doesn't allow them. Description Assignment You can assign a value to a pointer. Address of You can use the address-of operator to find the address of a pointer, so you can have pointers to pointers.

This is an advanced topic and is covered on Day 15, "Pointers: Decrementing You can subtract an integer from a pointer in order to point to a different memory location. Differencing You can subtract an integer from a pointer in order to point to a different memory location. Comparison Valid only with two pointers that point to the same array. Pointer Cautions When you're writing a program that uses pointers, you must avoid one serious error: For example, the following statement declares a pointer to type int: To be more exact, it doesn't point to anything known.

An uninitialized pointer has some value; you just don't know what it is. In many cases, it is zero. If you use an uninitialized pointer in an assignment statement, this is what happens: That address can be almost anywhere in memory--where the operating system is stored or somewhere in the program's code.

The 12 that is stored there might overwrite some important information, and the result can be anything from strange program errors to a full system crash. The left side of an assignment statement is the most dangerous place to use an uninitialized pointer. Other errors, although less serious, can also result from using an uninitialized pointer anywhere in your program, so be sure your program's pointers are properly initialized before you use them. You must do this yourself. The compiler won't do this for you! DON'T try to perform mathematical operations such as division, multiplication, and modulus on pointers.

Adding incrementing and subtracting differencing pointers are acceptable. DON'T forget that subtracting from or adding to a pointer changes the pointer based on the size of the data type it points to. It doesn't change it by 1 or by the number being added unless it's a pointer to a one-byte character. DO understand the size of variable types on your computer. As you can begin to see, you need to know variable sizes when working with pointers and memory.