Pointers and Arrays in C Language with Example Programs

Pointers-and-Arrays-in-C-programming-language-with-example-programs

Introduction:

We have looked at the Introduction to pointers and pointer to pointer variable in earlier posts. In today’s article, We will look at the relationship between the pointers and arrays in c programming language.

Arrays in C Langauge:

As we have discussed in the arrays article, The C Array is a collection of data items of a similar datatype. The Array elements are stored in the contiguous memory.

For example, Let’s create an array named cars that store the information of 5 cars.

int cars[5] = {20, 50, 80, 90, 110};

The First element( 20) of the array will be stored at Zeroth Index and the second value( 50) at the 1st index, and so on, The last value is stored at the n-1th index. (where n is the size of the array).

To access the elements of the array we use the subscript [ ] operator with the element index.

So we can say,

cars[0] = 20

cars[1] = 50

cars[2] = 80

cars[3] = 90

cars[4] = 110

How Arrays in C programming stored in the Memory:

Now, Let’s look at how the arrays are stored in the memory.

int cars[5] = {20, 50, 80, 90, 110};

how-arrays-are-stored-in-memory-in-c

Let’s say the array’s starting memory address is 0x9000 and the integer size is 4 Bytes.

Then the first element of the array(cars[0]) will be stored at the memory address 0x9000, the Second element will be stored at the 0x9004, similarly, all other elements will be stored in the consecutive memory address. The final element will be stored at the index 0x9016 memory address.

cars[0] = 0x9000

cars[1] = 0x9004

cars[2] = 0x9008

cars[3] = 0x9012

cars[4] = 0x9016

Let’s look at the program to get the values and addresses of the array elements.

Program to get the values and address of the array elements:

We use the address of operator(&) to get the address of array elements (&marks[i]).

Program Output:

Let’s compile and run the program.

Compile the program.

$ gcc array-values-addrs.c

Run the executable file.

program-to-get-array-address-in-c-language

If you observe addresses carefully, The array elements are stored in contiguous memory and each element occupied 4 Bytes.

Now, We know how arrays are stored in memory. Let’s go ahead and see how arrays and pointers are related in the C programming language.

Pointers and Arrays in C:

We can get the address of the array by using the address of operator (&) with the first element of the array ( arr[0])

Similarly, We can get the address of the second element in the array using the &scores[1], and so on so.

In C Programming language, The arrays and The pointers are closely related and an array name can be used as a pointer to the first element of the array.

As the array name also holds the memory address of the first element of the array. So we directly assign the array name to the pointer variable to store the address of the array.

So the ptrwill point to the first element in the array (i.e 0th Index element) and the ptr+1 will point to the second element in the array ( 1st Index), and so on and so.

📢As we discussed in the pointers arithmetic tutorial, The ptr+1 will give us the next element in the list.

In summary,

ptr points to the address of the 0th element in the array – i.e &scores or &scores[0]

ptr+1 points to the address of the 1st element in the array – i.e &scores[1]

ptr+2 points to the address of the 2nd element in the array – i.e &scores[2]

ptr+3 points to the address of the 3rd element in the array – i.e &scores[3]

ptr+4 points to the address of the 4th element in the array – i.e &scores[4]

We can get the value of the array element pointed by the pointer ptr by using the dereference operator/indirection operator ( *) like below.

To get the value of the 0th element use *ptr – i.e 60

To get the value of 1st element use *(ptr+1) – i.e 70

Similarly, To get the value of 2nd element use *(ptr+2) – i.e 80

To get the value of 3rd element use *(ptr+3) – i.e 90

To get the value of 4th element use *(ptr+4) – i.e 100

We can also use the subscript notation [ ] like below to get the array values using the pointer variable.

*(ptr+1) = ptr[1]

*(ptr+2) = ptr[2]

…

…

*(ptr+i) = ptr[i]

Let’s look at the program to understand the relationship between the arrays and pointers in the c programming language.

Accessing Array elements using Pointer in C Language – Array and Pointers in C:

In the following program, we are going to access the integer array marks elements using a pointer variable ptr.

Program Output:

Let’s compile and run the program using GCC.

accessing-array-elements-using-pointers-in-c-programming-language

As we can see from the above program and output, We started the program by creating an integer array named marks, Then we stored the address of the marks array in the integer pointer ptr.

int *ptr = marks;

Note that, We used the marks array. As the array name also holds the memory address of the first element of the array.

Then we used a for loop to iterate over the array elements and printed the elements using the pointer ptr with Index(i) (i.e *(ptr+i))

Get Array Values using the Pointer subscript notation:

Similarly, We can also use the pointer variable with the subscript notation [ ] to get the array elements. Here is the program to demonstrate the same.

Program Output:

Here is the output of the program.

accessing-array-elements-using-pointer-subscript-notation

As we can see from the above output, we are able to get the array elements values using the subscript notation(ptr[i])

Passing An array to function using a pointer:

Let’s look at how to pass an array as a pointer to a function. The function should accept the input array as a pointer variable, which means the function’s formal argument should be a pointer variable.

We have defined a function called printArray to print the array elements. The printArray() function takes two formal arguments, an integer pointer *scorePtr and array size size (We can also use the SIZE macro as well).

void printArray(int *scorePtr, int size)

The printArray() function uses a for loop to traverse through the array and prints the array elements using the scorePtr pointer variable. ( We are using the pointer with a subscript notation to access the array elements).

Finally, Call the printArray() function from the main() Function and pass the scorearray and its size(i.e SIZE).

Program Output:

Compile the program.

$ gcc passing-array-to-func.c

The above command generates the executable file named a.out. Run the executable file using ./a.out command.

passing-arrays-to-function-using-pointer-notation-in-c-programming

As you can see from the above output, The printArray() function printed the array elements on the console.

Related Articles:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

4 Responses

  1. […] pointer in c programming language. As we have discussed earlier in the introduction to pointer and pointers and array articles, We can return a pointer variable from a function in C language. We are going to look at […]

  2. […] an earlier article, we discussed the relationship between pointers and arrays. In today’s article, We will look at the pointer to array in c programming […]

  3. […] Pointers and Arrays in C […]

  4. […] Pointers and Arrays in C Language with Example Programs […]

Leave a Reply