×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Tricky C question?

Tricky C question?

Tricky C question?

(OP)
{
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.

RE: Tricky C question?

hi lim

 following is true

 pvPtr1 == *pvPtr2;

 am i right


msnivas


RE: Tricky C question?

(OP)
Msnivas, Try it :)
This will add your C experience.

RE: Tricky C question?

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
http://www.geocities.com/baskar52
 

RE: Tricky C question?

(OP)
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 ?

RE: Tricky C question?

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

RE: Tricky C question?

(OP)
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.

RE: Tricky C question?

(OP)
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.




Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close