Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strange problem

Status
Not open for further replies.

hbajaj

Programmer
Jan 14, 2001
4
CA
The problem is in the following code :

main(){

int arr[2][2] = {1,2,3,4};
int **a;
a = arr;

printf("Value of a+1 -> %d",*(a+1));
}

This code is printing value 2 whether we define int **a or int ***a or int ****a ??????
Can anyone explain me this.
I hv tested this code on Linux as well as Turbo C compiler for DOS.

TIA

 
Hello,

I think your initialization of arr is giving you the problem. Try:

int arr[2][2] = { {1,2} , {3,4} };

eswhite
 
In this case definition doesn't mater for value that you are putting in.
If you have apple basket and you are putting cherry there, they will not become apples, but will stay a good cherry in a wrong basket. But if you have basket for one sort of apples and putting the other one in, you can sell this.
Seriosly:
In "=" operation integer can become a double or something, but not a pointer to something.
Your print will be always the same and be equal to the next line:
printf("Value of a+1 -> %d",*(arr+1));
Only compiler can show some warnings.

 
The correct initialization of the 2-d array to an anonymous array, regardless of type, is
type arr[2][2] = { {}, {} }. I am not sure what Lim is talking about, I am not sure if he knows what he is talking about for that matter, but if you change your initialization of arr your code will work as you expect.
 
I know what I am talking about.

The wrong array initiallization is not a point of his problem. It dosn't mater hom much "*" he will add in line
int ********a;
If next lines will be the same his printf will always print
"Value of a+1 -> 2"
Because of the reason I have described in previous post.
Actually I don't know what hbajaj was expecting to see in his print, but he was wondering why it always 2 regardles on his "a" definition. I said that it is more important what you are putting in you basket that how you are calling this basket.


 
O, now I see what hbajaj whanted to see.
Sorry I did not get it before. It is really not so easy as I thought. This is RIGHT explanation:

You should declare "a" as
int (*a)[2]; pointer to array of 2
in this case *(a+1) will be the same as &arr[1]
And by the way *(a+1) will be the same as (a+1)
because arr == &arr; and arr[1] == &arr[1];
+1 will jump to ptr to the next array of 2. In this case your "*" will do nothing and you got your pointer.

in your case
int **a; pointer to the other pointer;
you will have compiler warning and do illigal thing actually.
What will really happen:
+1 will jump to the size other pointer wchich accidently
( because sizeof(int) == sizeof (int*) ) will be the same as ptr to element 2 and take this value. And you have your 2. This will not work with char array because sizeof (char) != sizeof (char*).

Regards, Lim.
 
May be this will be clear:
int a[2][4]={{1,2,3,4},{5,6,7,8}}
in this case a+1 will shift pointer to the sizeof(int[4]) and this will be ptr to 5 (a[1] do the same job);
for this array a+1 == a[1] == &a[1] == *(a+1);

in this case
int **a;
a+1 will shift pointer to the sizof (int*) and this is acidently ptr to 2.
 
Hi Lim & eswhite,

Thanks a lot for helping me in clearing my doubt.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top