Learn C - C Array Pointer






Arrays and Pointers

An array is a collection of value in the same type.

A pointer is a variable (a memory address) that can reference another variable.

You can use a pointer to hold the address of different variables, as long as they're all of the same type.

The array name without an index value refers to the address of the first element in the array.

You can change the address contained in a pointer, but you can't change the address referenced by an array name.

The following code shows that an array name by itself refers to an address.


    #include <stdio.h>
/*from   w w  w.  j a v a2s  . c  om*/
    int main(void)
    {
      char multiple[] = "My string";

      char *p = &multiple[0];
      printf("The address of the first array element  : %p\n", p);

      p = multiple;
      printf("The address obtained from the array name: %p\n", multiple);
      return 0;
    }

From the output we know that the expression &multiple[0] produces the same value as the expression multiple.

The code above generates the following result.





Example 2

Because multiple evaluates to the address of the first byte of the array, and &multiple[0] evaluates to the first byte of the first element of the array.

This program demonstrates the effect of adding an integer value to a pointer.


    #include <stdio.h>
    #include <string.h>
//from ww  w.  j  av  a 2 s.  c o m
    int main(void)
    {
      char multiple[] = "a string";
      char *p = multiple;
      for(int i = 0 ; i < strnlen(multiple, sizeof(multiple)) ; ++i) {
         printf("multiple[%d] = %c  *(p+%d) = %c  &multiple[%d] = %p  p+%d = %p\n", i, multiple[i], i, *(p+i), i, &multiple[i], i, p+i);
      }
      return 0;
    }

The code above generates the following result.





Example 3

Adding an integer n to the address in p evaluates to the same thing as multiple[n].


#include <stdio.h>
//from ww  w  . j  av a2 s  .c  o  m
int main(void)
{
  long multiple[] = {15L, 25L, 35L, 45L};
  long *p = multiple;

  for(int i = 0 ; i < sizeof(multiple)/sizeof(multiple[0]) ; ++i) {
    printf("address p+%d (&multiple[%d]): %llu        *(p+%d)   value: %d\n", i, i, (unsigned long long)(p+i),  i,  *(p+i));
  }
  printf("\n    Type long occupies: %d bytes\n", (int)sizeof(long));
  return 0;
}

The code above generates the following result.

Example 4

Note that you could use the array name directly in this example. You could write the for loop as:


#include <stdio.h>
/*w  w w  . ja v a2  s .com*/
int main(void)
{
      long multiple[] = {15L, 25L, 35L, 45L};
      long *p = multiple;


      for(int i = 0 ; i < sizeof(multiple)/sizeof(multiple[0]) ; ++i)
        printf("address p+%d (&multiple[%d]): %llu        *(p+%d)   value: %d\n",
                      i, i, (unsigned long long)(multiple+i),  i,  *(multiple+i));

      return 0;
}

The expressions multiple and multiple+i both evaluate to an address.

Incrementing multiple by 1 results in the address of the next element in the array, which is 4 bytes further along in memory.

An array name refers to a fixed address and is not a pointer.

You can use an array name as pointer in expression but you cannot modify it.

The code above generates the following result.

Multidimensional Arrays and Pointer

The following code is here to explore multidimensional arrays in relation to pointers.


    #include <stdio.h>
// www.  j av  a2 s .c  om
    int main(void)
    {
      char my_array[3][3] = {
                           {'1','2','3'},
                           {'4','5','6'},
                           {'7','8','9'}
                         };

      printf("address of my_array        : %p\n", my_array);
      printf("address of my_array[0][0]  : %p\n", &my_array[0][0]);
      printf("value of my_array[0]       : %p\n", my_array[0]);
      return 0;
    }

As you can see, all three output values are the same.

So when you access the two-dimensional array using the array name with a single index value, my_array[0], you're referencing the address of one of the subarrays.

To summarize the expressions:

    my_array
    my_array[0]
    &my_array[0][0]

These all have the same value: my_array is the address of a two-dimensional array of char elements, my_array[0] is the address of a one-dimensional array of char elements that is a subarray of my_array, &my_array[0][0] is the address of an array element of type char.

The code above generates the following result.

Example 5

The following code shows how to get values in a two-dimensional array.


    #include <stdio.h>
//from  w  w  w. j a  va 2 s  . c o  m
    int main(void)
    {
      char my_array[3][3] = {
                           {'1','2','3'},
                           {'4','5','6'},
                           {'7','8','9'}
                         };

      // List all elements of the array
      for(int i = 0 ; i < 9 ; ++i)
         printf(" my_array: %c\n", *(*my_array + i));
      return 0;
    }

The code above generates the following result.

Example 6

You can see multidimensional arrays and points in action here:


    #include <stdio.h>
//  ww  w  . ja v a 2  s  .  c  om
    int main(void)
    {
      char my_array[3][3] = {
                           {'1','2','3'},
                           {'4','5','6'},
                           {'7','8','9'}
                         };

      char *pmy_array = *my_array;             // A pointer to char
      for(int i = 0 ; i < 9 ; ++i)
      printf(" my_array: %c\n", *(pmy_array + i));

      return 0;
    }

The code above generates the following result.