* array pointer
@ 2011-01-18 13:22 ratheesh k
2011-01-18 13:23 ` ratheesh k
0 siblings, 1 reply; 6+ messages in thread
From: ratheesh k @ 2011-01-18 13:22 UTC (permalink / raw)
To: linux-c-programming
int *s_ptr;
int *d_ptr;
int arr[2][2]={1,2,3,4};
s_ptr=*arr; /* This is perfectly fine */
d_ptr=arr /* this line throws a warning: assignment from incompatible
pointer type */
What is the problem here ?
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: array pointer 2011-01-18 13:22 array pointer ratheesh k @ 2011-01-18 13:23 ` ratheesh k 2011-01-18 13:32 ` ratheesh k 0 siblings, 1 reply; 6+ messages in thread From: ratheesh k @ 2011-01-18 13:23 UTC (permalink / raw) To: linux-c-programming i made a mistake d_ptr is int **d_ptr On Tue, Jan 18, 2011 at 6:52 PM, ratheesh k <ratheesh.ksz@gmail.com> wrote: > int *s_ptr; > int *d_ptr; > int arr[2][2]={1,2,3,4}; > > s_ptr=*arr; /* This is perfectly fine */ > d_ptr=arr /* this line throws a warning: assignment from incompatible > pointer type */ > > What is the problem here ? > -- To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: array pointer 2011-01-18 13:23 ` ratheesh k @ 2011-01-18 13:32 ` ratheesh k 2011-01-18 14:46 ` Michal Nazarewicz 2011-01-18 19:33 ` Glynn Clements 0 siblings, 2 replies; 6+ messages in thread From: ratheesh k @ 2011-01-18 13:32 UTC (permalink / raw) To: linux-c-programming int *s_ptr; int **d_ptr; int arr[2][2]={1,2,3,4}; s_ptr=*arr; /* This is perfectly fine */ d_ptr=arr /* this line throws a warning: assignment from incompatible pointer type */ ???????? On Tue, Jan 18, 2011 at 6:53 PM, ratheesh k <ratheesh.ksz@gmail.com> wrote: > i made a mistake d_ptr is int **d_ptr > > On Tue, Jan 18, 2011 at 6:52 PM, ratheesh k <ratheesh.ksz@gmail.com> wrote: >> int *s_ptr; >> int *d_ptr; >> int arr[2][2]={1,2,3,4}; >> >> s_ptr=*arr; /* This is perfectly fine */ >> d_ptr=arr /* this line throws a warning: assignment from incompatible >> pointer type */ >> >> What is the problem here ? >> > -- To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: array pointer 2011-01-18 13:32 ` ratheesh k @ 2011-01-18 14:46 ` Michal Nazarewicz 2011-01-18 16:59 ` David McMurray 2011-01-18 19:33 ` Glynn Clements 1 sibling, 1 reply; 6+ messages in thread From: Michal Nazarewicz @ 2011-01-18 14:46 UTC (permalink / raw) To: linux-c-programming, ratheesh k On Tue, 18 Jan 2011 14:32:43 +0100, ratheesh k <ratheesh.ksz@gmail.com> wrote: > int *s_ptr; > int **d_ptr; > int arr[2][2]={1,2,3,4}; One image is worth a thousand words, so here it goes: arr is something like this in memory: +---+---+---+---+ | 1 | 2 | 3 | 4 | +---+---+---+---+ d_ptr would require something like: +---------+---------+ | pointer | pointer | +----|----+----|----+ | | +---+---+ | +---> | 3 | 4 | | +---+---+ +---+---+ +---> | 1 | 2 | +---+---+ Ie. in arr all the numbers are lied linearly in memory whereas int **d_ptr is an array of pointers to array of ints. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: array pointer 2011-01-18 14:46 ` Michal Nazarewicz @ 2011-01-18 16:59 ` David McMurray 0 siblings, 0 replies; 6+ messages in thread From: David McMurray @ 2011-01-18 16:59 UTC (permalink / raw) To: linux-c-programming >> int *s_ptr; >> int **d_ptr; >> int arr[2][2]={1,2,3,4}; > > One image is worth a thousand words, so here it goes: > > arr is something like this in memory: > > +---+---+---+---+ > | 1 | 2 | 3 | 4 | > +---+---+---+---+ > > d_ptr would require something like: > > +---------+---------+ > | pointer | pointer | > +----|----+----|----+ > | | +---+---+ > | +---> | 3 | 4 | > | +---+---+ +---+---+ > +---> | 1 | 2 | > +---+---+ > > Ie. in arr all the numbers are lied linearly in memory whereas > int **d_ptr is an array of pointers to array of ints. I would say int **d_ptr is really a pointer to a pointer and not necessarily an array of pointers. So, the following is fine: d_ptr = &s_ptr; printf("s_ptr: %d, d_ptr: %d\n", *s_ptr, **d_ptr); and displays "s_ptr: 1, d_ptr: 1" To (attempt to) answer your original question though, the problem is that "arr" will give you the address of the array, not the address of a pointer to the array. For each extra dimension of the array you need to add a * when referring to the name of the array without indices in order to de-reference the additional dimensions. So, 1-dimensional array int *s_ptr; int arr[2]={1,2}; s_ptr = arr; /* Ok because arr references the address of the array immediately */ 2-dimensional array int *s_ptr; in arr[2][2]={1,2,3,4}; s_ptr = *arr; /* Ok because the * de-references arr to the address of the array, but arr is not the address of a pointer to the array */ 3-dimensional array int *s_ptr; int arr[2][2][2]={1,2,3,4,5,6,7,8}; s_ptr = **arr; /* Ok, again each * de-references additional dimensions of the array */ But these references are not pointers, which is why int **d_ptr; int arr[2][2]={1,2,3,4}; d_ptr = arr; will not work, arr will not be a pointer to a pointer. HTH, David McMurray. -- To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: array pointer 2011-01-18 13:32 ` ratheesh k 2011-01-18 14:46 ` Michal Nazarewicz @ 2011-01-18 19:33 ` Glynn Clements 1 sibling, 0 replies; 6+ messages in thread From: Glynn Clements @ 2011-01-18 19:33 UTC (permalink / raw) To: ratheesh k; +Cc: linux-c-programming ratheesh k wrote: > int *s_ptr; > int **d_ptr; > int arr[2][2]={1,2,3,4}; > > s_ptr=*arr; /* This is perfectly fine */ > d_ptr=arr /* this line throws a warning: assignment from incompatible > pointer type */ ???????? Arrays are not pointers. If you use the name of an array as an expression, it is treated as a pointer to the first element. For one-dimensional arrays, this means that you can use an array in most places where a pointer is required. But this doesn't hold for higher dimensions; an array of pointers isn't the same as an array of arrays. It also doesn't hold for many other cases, i.e. sizeof, variable declarations, etc. -- Glynn Clements <glynn@gclements.plus.com> ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2011-01-18 19:33 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2011-01-18 13:22 array pointer ratheesh k 2011-01-18 13:23 ` ratheesh k 2011-01-18 13:32 ` ratheesh k 2011-01-18 14:46 ` Michal Nazarewicz 2011-01-18 16:59 ` David McMurray 2011-01-18 19:33 ` Glynn Clements
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).