array and pointer initilization



 int *pt1,*pt2 ;
 
 pt1=&vi2[0][0] ;
 pt2=&vi2[2][1] ;

 how can i rewrite  right side (rvalue) i n a shorter pt1=&vi2[0][0] (more compact way).
xinexAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
dennis_georgeConnect With a Mentor Commented:
hi...

an array
(int arr[MAX1][MAX2] ;)

 can be represented in following forms....

Array[x][y] can be also represented as

   *(*(Array + x) + y)  --->  *( *(x + Array) + y )   ---> x[Array][y]

since we know for addition a + b = b + a

and if you want to represent &Array[x][y] ;

   (*(Array + x) + y)

So if you want to represent
pt1=&vi2[0][0] ;
 pt2=&vi2[2][1] ;

you can do by

pt1 = (*(vi2 + 0) + 0 );     --->       pt1 = *(vi2  + 0) ;        ---> pt1 = *vi2;
pt2 = (*(vi2 + 2) + 1 );


Dennis
0
 
sunnycoderCommented:
pt1=vi2[0] ;

pt2=&vi2[2][1]; //this is as short as it gets
0
 
snehanshuCommented:
isn't
pt1=&vi2[0][0] ;
equal to
pt1 = v2;
?
...S
0
 
sunnycoderCommented:
vi2 would be an int **
pt1 is an int pointer
the value may be same or different depending on how array was allocated

consider this

int ** vi2;
vi2 = (int **) malloc (sizeof(int *) * 10 );

vi2[0] =  (int *) malloc (sizeof(int ) * 10 );

now vi2[0] holds the address of [0][0] (the int we want) while vi2 holds the address of the array of pointers (not the int)

on the other hand, if the allocation was static, the addresses will be the same
0
 
snehanshuCommented:
Right sunnycoder,
Then how about
pt1 = *vi2
?
...S
0
All Courses

From novice to tech pro — start learning today.