Static mutlidimensional arrays in C vs pointers and addresses -


i'm learning c , got stuck on following piece of code:

int a[num_rows][num_cols], (*p)[num_cols], i; (p = &a[0]; p < &a[num_rows]; p++) {     (*p)[i] = 0; } 

according author supposed assign 0 i-th column of 2d array.

i understand in reality it's not rows , columns continuous block of memory - pretty 1d array each element 1d array. also: array name equivalent of address of first element of array (according book i'm reading). 1d arrays makes sense me, 1st element can int or char or whatever. in 2d arrays however, each element array, again address of first element of array - time "inner" one? mean "a[0]" gives address of 1st element of array, , use "&" operator on it? give me, address of address? :/

could please explain what's happening here, step-by-step? what's address here, what's pointer, etc. went on number of chapters on pointers in various c books compare how authors explain this, looks use "pointer" , "address" interchangeably.

i tried comparing contents of 3, so:

printf("%d ", a); printf("%d ", a[0]); printf("%d ", &a[0]); 

but had same value :/

you're pretty correct this. , remember, a[b] equivalent *(a+b), , &*x equivalent x (assuming x appears in expression context), &a[b] equivalent &*(a+b) a+b, &a[0] a (again, in expression context).

in case, int a[num_rows][num_cols] block of num_rows*num_cols elements of type int. grouped num_rows blocks (rows) of num_cols int elements. if write a[i][j] it's equivalent *(*(a + i) + j). inner dereference doesn't perform memory access, rather removes level of dereference data type. can think of type cast.

when used in expression context, a becomes int (*)[num_cols] pointer points first row of a. pointer addition scales size of element of a, sizeof(int [num_cols]) num_cols*sizeof(int).

you commonly see people talk of array names "decaying" pointers when used in expression context. in one-dimensional case, if have int b[dim], "decays" int * value address of b. instance, when passed argument function, array isn't passed, instead address passed.

in two-dimensional case, if have int a[num_rows][num_cols], "decays" int (*)[num_cols] pointer, pointer array of num_cols int elements.

in example, pass following printf:

  1. a (decays int (*)[num_cols])
  2. a[0] (decays int *)
  3. &a[0] (this &*(a + 0) or a, decayed int (*)[num_cols])

the first , last cases same. second case differs in data type. note data type affects pointer addition. when adding int pointer, int scaled size of whatever pointer points to.

also note printf formats aren't correct, since you're passing pointer values int values expected. won't work on platforms, , compilers warn bad data types format string.

the safe way format address printf %p. expects pointer, safe if pointer , int don't have same size.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -