Without worrying about what a modifiable lvalue is, it's probably
easiest to think of an array variable as a constant pointer.
Refer to the code below:
#include <stdlib.h>
int main( ) {
/* the compiler figures out how many array elements there are */
int x[ ] = { 8, 6, 7, 5, 3, 0, 9 };
/* allocate a block of 7 consecutive integers and assign this
address
* to the const pointer named xx
*/
int * const xx = ( int * ) malloc( 7 * sizeof( int ) );
const int *xp;
int i;
/* sizeof( ) will tell you how many bytes of storage, then divide
by the
size of each array element (an int typically is 4 bytes) to
determine
the number of array elements */
for ( i = 0; i < sizeof( x ) / sizeof( x[ 0 ] ); i++ )
/* perfectly legal, though it looks confusing... */
xx[ i ] = * ( x + i ) * -2;
/* assignment to a pointer to a constant int; contrast with this
xx, which
* is a constant pointer to an int.
*/
xp = x;
printf( "The 2nd element of array x is %d\n", * ( xp + 1 ) );
printf( "The 2nd element of array x is also this %d\n", xp[1] );
/* just showing off here a little bit... */
for ( i = 0; i < sizeof( x ) / sizeof( * ( x + 0 ) ); i++,xp++ )
printf( "x[ %d ] = %d\n", i, *xp );
/* note the different values returned by sizeof */
printf( "The size of x is %d, the size of xx is %d\n",
sizeof( x ), sizeof( xx ) );
for ( i = 0; i < sizeof( x ) / sizeof( int ); i++ )
printf( "element %d x: %d xx: %d\n", i, x[ i ], xx[ i ] );
/* I almost forgot this; you don't want to use dynamic memory
unless you
* truly have to, or like to give yourself fits with memory leaks.
*/
free ( xx );
return ( 0 );
}
The important things to note are that you can refer to array elements
as offsets from a pointer or using array brackets. Similarly, you can
also use the array brackets with a pointer. This suggests a close
correlation between pointers and array names. Note that you can store
the address of array, but you can not assign a value to an array name.
This is just like the constant pointer xx that I used above.
Using a pointer to a const int is a safe way to step through an array.
The compiler will warn you if you inadvertently attempt to modify
*xp. I hope this isn't more information than you wanted.
Best of luck,
Victor
Hello,
Am not very good with pointers in C,but I have a small doubt about
the way these pointers work..
We all know that in an array say x[5],x is gonna point to the first
element in that array(i.e)it will have the address of the first
element.In the the program below am not able to increment the value
stored in x,which is the address of the first element.Why am I not
able to do that?Afterall 1 is also a hexadecimal number then why
does adding 1 to x show me a error?
I got the message "Lvalue Required" when I complied the program.Even
if I declared x[5] as long int the same error continued.Can
someone please help me solve it out??
Thanks to all those who are gonna help me in this..
--ambika
#include<stdio.h>
void main()
{
int x[5]={1,2,3,4,5};
printf("\naddr in x:%p",x);
printf("\nnumber in the addr stored in x is:%d",*x);
x=x+1;
printf("\naddr in x after incrementation is:%p",x);
printf("\nnumber in the addr stored in x is:%d",*x);
}