Home > front end >  Why is the value and value at address of a array variable same?
Why is the value and value at address of a array variable same?

Time:10-15

I am learning about arrays in C programming and this is one of the example program in the book(the program is same as in the book except for the printf line in the end). Printing the value of s gives me the base address of the array(as expected) but when I try to use the *(value at) operator with s in the printf statement it still gives me the base address instead of the value 1234. Why do I need to use **s to get the value instead of using a single * operator?

#include <stdio.h>

int main()
{
        int s[4][2]={
                        {1234,56},
                        {1212,33},
                        {1434,80},
                        {1312,78}
                    };
        int i;
        for(i=0;i<=3;i  )
                printf("Address of %d th 1-D array = %u\n", i, s[i]);

        printf("%u\n", s);
        return 0;
}
#include <stdio.h>

int main()
{
        int s[4][2]={
                        {1234,56},
                        {1212,33},
                        {1434,80},
                        {1312,78}
                    };
        int i;
        for(i=0;i<=3;i  )
                printf("Address of %d th 1-D array = %u\n", i, s[i]);

        printf("%u\n", *s);
        return 0;
}

CodePudding user response:

s is an array of arrays of 2 integers.

So *s and s[0], s[1], s[2], s[3] are all arrays of 2 integers.

and consequently **sand s[0][0], s[0][1], ..., s[3][1] are all integers.

Since arrays are converted to pointers to the first array element in most expressions, printing s and *s will give the same value. But notice that their type differ. BTW: Printing &s[0][0] will also give the same value.

BTW:

To print a pointer use %p and cast the pointer to void-pointer. Like:

printf("%p\n", (void*)*s);

CodePudding user response:

Because s is a two-dimensional array, s[i] points to a pointer that itself points to a int value.

If you modify your code as following, you can see the actual values:

#include <stdio.h>

int main()
{
  int s[4][2]={
    {1234,56},
    {1212,33},
    {1434,80},
    {1312,78}
  };
  
  int i, j;
  for(i=0;i<=3;i  ) {
    printf("Address of %d th 1-D array = %u\n", i, s[i]);
    for(j=0;j<2;j  ) {
      printf("  Value of %d th element of that array = %u\n", j, s[i][j]);
    }
  }

  printf("%u\n", s);
  return 0;
}

Actually when compiling this file, you should receive the warnings that the interpolated values: s[i] in line 14 and s in line 20 are of types int * and int (*)[2] respectively.

$ gcc test.c
test.c:14:52: warning: format specifies type 'unsigned int' but the argument has type 'int *' [-Wformat]
    printf("Address of %d th 1-D array = %u\n", i, s[i]);
                                         ~~        ^~~~
test.c:20:18: warning: format specifies type 'unsigned int' but the argument has type 'int (*)[2]' [-Wformat]
  printf("%u\n", s);
          ~~     ^
2 warnings generated.

The printout of the modified program:

$ ./a.out
Address of 0 th 1-D array = 1830793904
  Value of 0 th element of that array = 1234
  Value of 1 th element of that array = 56
Address of 1 th 1-D array = 1830793912
  Value of 0 th element of that array = 1212
  Value of 1 th element of that array = 33
Address of 2 th 1-D array = 1830793920
  Value of 0 th element of that array = 1434
  Value of 1 th element of that array = 80
Address of 3 th 1-D array = 1830793928
  Value of 0 th element of that array = 1312
  Value of 1 th element of that array = 78
1830793904
  • Related