By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

pointer

P: 18
how a pointer in C is related to an one dimension array?
Nov 8 '06 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
how a pointer in C is related to an one dimension array?
Arrays and pointers have a very close relationship and it is possible, and sometimes more efficient, to use pointers to manipulate the elements of arrays.

The name of an array refers to the address of its first element (it is a constant pointer to the first element). Consider:
Expand|Select|Wrap|Line Numbers
  1.     int *p_array, x[100];   /* define pointer to int and array of int */
  2.  
  3.     p_array = x;           /* point to first element of array */
  4.     p_array = &x[0];       /* point to first element of array */
  5.  
This defines a pointer to int and an array of int and then shows two ways of assigning the value of the address of the first element of x to pointer p_array. Once the pointer is set up it can be used instead of the array name. For example, the following statements are alternative ways of accessing the first element of x and assigning 0 to it:
Expand|Select|Wrap|Line Numbers
  1.     x[0] = 0;                /* zero first element of array */
  2.     *x = 0;                 /* zero first element of array */
  3.     *p_array = 0;            /* zero first element of array */
  4.     p_array[0] = 0;          /* zero first element of array */
  5.  
Nov 8 '06 #2

vpawizard
P: 66
Actually there is a significant difference between concept of arrays and pointers. Arrays are collection of element while pointer stores the address of the element.
But the similarity is when the program is compiled. The term a[i] is internally converted to *(a+i). This leads to the fact that a[i],i[a],*(a+i),*(i+a) are all equivalent.
Nov 16 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.