Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference among array, &array and &array[0] in C language? [duplicate]

Tags:

arrays

c

pointers

I've got confused when learning array and pointer in C language,why are ch, &ch,&ch[0] just equal to one another ,while sptr, &sptr,&sptr[0] are not? Here's my source code:

int main(void)
    {
        char ch[7] = { '1','2','3','4','5','6','0' };
        char *sptr = "132456";
        double db[4] = { 1.0,2.0,3.0,4.0 };
        printf("\n%p  %p  %p\n", ch, &ch,&ch[0]);
        printf("\n%p  %p  %p\n", sptr, &sptr,&sptr[0]);
        printf("\n%p  %p  %p\n", db, &db,&db[0]);
        return 0;

    }

And the inputs on my machine are:

00FDFD68  00FDFD68  00FDFD68

00037CD0  00FDFD5C  00037CD0

00FDFD34  00FDFD34  00FDFD34
like image 470
CircleBop Avatar asked Apr 21 '17 12:04

CircleBop


People also ask

What is the difference between int array [] and int [] array?

What is the difference between int[] a and int a[] in Java? There is no difference in these two types of array declaration. There is no such difference in between these two types of array declaration. It's just what you prefer to use, both are integer type arrays.

What are the 3 types of arrays?

There are three different kinds of arrays: indexed arrays, multidimensional arrays, and associative arrays.

How it is different from array?

Array refers to a collection consisting of elements of homogeneous data type. Structure refers to a collection consisting of elements of heterogeneous data type. Array is pointer as it points to the first element of the collection. Instantiation of Array objects is not possible.

What is difference between array and stack?

A stack is a type of linear data structure that is represented by a collection of pieces that are arranged in a predetermined sequence. An array is a collection of data values that are associated to one another and termed elements. Each element is recognized by an indexed array.


1 Answers

In most (though not all) contexts an array "decays" into a pointer to its first element. This is why ch and &ch[0] are the same in your example (array element access has higher precedence than the "address of" operator, so the latter could also be written as &(ch[0])).

The remaining &ch is one case where the array does not decay into a pointer; instead, you get the address of the array. Naturally enough, this is the same as the address of the first element of the array - however, importantly, it has a different type; it is of type char (*)[7], i.e. a pointer to an array of char with 7 elements. The other two pointers are of type char *, i.e. a pointer to an individual char.

Since sptr is a pointer, &sptr is the address of that pointer and naturally will be different. &sptr[0] is equivalent to sptr + 0, which is of course equal to sptr.

That you do not see why sptr and &sptr yield different addresses indicates a misunderstanding of what a pointer is. A pointer is a fixed-size object with a value that can refer to (point at) some arbitrary object of a particular type. Because it is an object itself, a pointer can be made to point at a different object. An array variable, on the other hand, always (during its lifetime) refers to the same array object.

In your example output:

00037CD0  00FDFD5C  00037CD0

The first value, 00037CD0, is the location to which sptr points - that is, it is the location in memory of the string constant "132456". The second value, 00FDFD5C, is the address of the sptr variable itself. What this shows is that there is a pointer object at address 00FDFD5C which holds the value 00037CD0.

Essentially, the difference between the two cases boils down to this:

  • The address of an array is the same as the address of its first element
  • The address of a pointer, on the other hand, bears no relation to what the pointer currently points to.
like image 99
davmac Avatar answered Sep 30 '22 11:09

davmac