Solved

array and pointer initilization

Posted on 2003-10-22
5
296 Views
Last Modified: 2010-04-15


 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).
0
Comment
Question by:xinex
  • 2
  • 2
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9598024
pt1=vi2[0] ;

pt2=&vi2[2][1]; //this is as short as it gets
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9598084
isn't
pt1=&vi2[0][0] ;
equal to
pt1 = v2;
?
...S
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9598147
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
 
LVL 5

Expert Comment

by:snehanshu
ID: 9598692
Right sunnycoder,
Then how about
pt1 = *vi2
?
...S
0
 
LVL 5

Accepted Solution

by:
dennis_george earned 50 total points
ID: 9604493
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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now