c - Why sizes of an array and a pointer to a first element are different? -


kernighan & ritchie 2nd ed. says:

the correspondence between indexing , pointer arithmetic close. definition, value of variable or expression of type array address of element 0 of array. after assignment
pa = &a[0];

pa , a have identical values. since name of array synonym location of initial element, assignment pa=&a[0] can written as
pa = a;

if a , pa identical, why code:

#include <stdio.h>  int main() {     char a[] = "hello";     char *pa = a;     printf("array: %ld\n", sizeof(a));     printf("pointer: %ld\n", sizeof(pa)); } 

outputs this:

array: 6 pointer: 8 

reference authoritative source appreciated.

two objects can have same address sizes can different.

from c standard (6.5.3.4 sizeof , alignof operators)

2 sizeof operator yields size (in bytes) of operand, may expression or parenthesized name of type. the size determined type of operand....

consider following example

#include <stdio.h>  int main( void ) {     struct     {         char c;         int x;     } a;      printf( "object a:\taddress - %p size - %zu\n",             &a, sizeof( ) );     printf( "object a.c:\taddress - %p size - %zu\n",             &a.c, sizeof( a.c ) ); }     

the program output is

object a:   address - 0x7fff164e16d0 size - 8 object a.c: address - 0x7fff164e16d0 size - 1 

as seen object a of type struct a , data member c of type char have same address different sizes.

as arrays pointer object stores address of other object. store address of other object enough allocate example 4 or 8 bytes of memory pointer depending on used system.

as arrays named extents of memory. arrays not store addresses. store own elements (that of course can pointers).

an array name used in expressions converted pointer first element.

according c standard (6.3.2.1 lvalues, arrays, , function designators)

3 except when operand of sizeof operator or unary & operator, or string literal used initialize array, an expression has type ‘‘array of type’’ converted expression type ‘‘pointer type’’ points initial element of array object , not lvalue. if array object has register storage class, behavior undefined.

in quote there listed when array not converted pointer first element. example when array operand of sizeof operator.

if return program

int main() {     char a[] = "hello";     char *pa = a;     printf("array: %ld\n", sizeof(a));     printf("pointer: %ld\n", sizeof(pa)); } 

then in statement

    char a[] = "hello"; 

string literal "hello" has type char[6] not converted pointer. in statement

    char *pa = a; 

array a converted pointer first element.

and in statement

    printf("array: %ld\n", sizeof(a)); 

array a not converted pointer because operand of sizeof operator.

however if used expression in sizeof operator example this

sizeof( + 0 ) 

then pointer , correspondingly sizeof return size of pointer instead of size of array


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -