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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tricky C question? 1

Status
Not open for further replies.

Lim

Programmer
Aug 15, 2000
192
0
0
US
{
double adArray[2];
void *pvPtr1 = NULL;
void *pvPtr2 = NULL;

pvPtr1 = (void*) adArray;
pvPtr2 = (void*) &adArray;
}

What is the true?
1) pvPtr1 == pvPtr2;
2) pvPtr1 == *pvPtr2;
3) &pvPtr1 == pvPtr2;
4) All 1,2,3 are false.

Please, just answer, try it later.
I know the answer, I want to know what you think.

Leon.
 
hi lim

following is true

pvPtr1 == *pvPtr2;

am i right


msnivas


 
Msnivas, Try it :)
This will add your C experience.
 
Hi Lim,
From the way you are asking, I guess the answer should be 4. Bcoz once you convert it to void type, it doesnt matter what is there inside. I mean the value doesnt matter too much. May be if type cast it into double once again, I guess the answer will be 2. Am i right ?
Cheers,
Baskar.

Baskar
baskar52@hotmail.com
 
Basking,
Value does matter.
You was right about *pvPtr2;
Compiler will give the error.
But it doesn't matter how you will cast it.
Cast doesn't change a value, The Trick was about the Value!
Let make it easy:
1) pvPtr1 == pvPtr2
2) pvPtr1 != pvPtr2
1 or 2 ?
 
Hi,
Nothing to do with the question but I'm a little rusty with C so... I understand type-casting, but why and in what case would one want to cast to void or void*?

Rob Marriott
rob@career-connections.net
 
You are right about 1. But not bcoz ptr+0.
Where do you see ptr+0?
&adArray is not a ptr+0.
It is bacause array name in an expression is treated by the compiler as a pointer to the first element of the array (ANSI C Standard, paragraph 6.2.2.1). Array address is already taken with the & operator. So Compiler ignores &adArray and reads it as adArray. &adArray == adArray.

Good luck.
 
CCTC1,
There are few good reasons for it.
1) If my function calls user defined function (call back function) which came to my function as an argument and the other argument is user data which he whants to pass in his function. So I don't know the type of this data and I use void* or void**. I don't care about this data, user will cast this to real data type and use it inside his function.
2)If your function allocate some user data which came through argument and type of this data came in other argument. So I use void** to pass this argument. There is better way in C++ for it, but in C this is a very usefull.
3) When I want to give only ptr (handle to object) to some data to user. So user can't use this data directly and he doesn't care about internal structure of my data. He can use this object only by calling my functions with this ptr as an argument. THIS IS ACTUALLY THE MAIN REASON. This helps to make independent modules, which connected to each other by public functions, not a public structures. And you have a freedom to change internal structures as you wish.
4) Other reasons are less important.




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top