Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# array and pointer initilization

Posted on 2003-10-22
Medium Priority
303 Views

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
Question by:xinex
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 45

Expert Comment

ID: 9598024
pt1=vi2[0] ;

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

LVL 5

Expert Comment

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

LVL 45

Expert Comment

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

ID: 9598692
Right sunnycoder,
pt1 = *vi2
?
...S
0

LVL 5

Accepted Solution

dennis_george earned 200 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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
###### Suggested Courses
Course of the Month12 days, 6 hours left to enroll