By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,948 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,948 IT Pros & Developers. It's quick & easy.

Casting as a pointer help.

P: 66
For :
Expand|Select|Wrap|Line Numbers
  1. int *ptr;
  2. ptr = structInst->member
  3.  
Where the type of "member" is unsigned int. So, I get a warning for different signedness. How do I cast structInst->member to get rid of the warning?? Do I use:
1. &
2. (int)
3. (int *)

Please help I don't know what is needed here. (this is not homework)
Nov 14 '08 #1
Share this Question
Share on Google+
6 Replies


Savage
Expert 100+
P: 1,764
3 .
Nov 14 '08 #2

Expert 100+
P: 2,415
Wait. Why are you doing this? What sort of information is stored in structInstr->member? If it isn't a pointer then why would you want to convert it to one? If it is a pointer then why not declare it as a pointer in the first case?

The result from converting a nonzero integer to a pointer type is nonportable.
Nov 14 '08 #3

P: 66
What sort of information is stored in structInstr->member? If it isn't a pointer then why would you want to convert it to one? If it is a pointer then why not declare it as a pointer in the first case?
I think that the code is assigning the pointer ptr to the value stored in "member".
But "ptr" is an int, while "member" is a uint.

So I need to case it first to signed and use "*" to get the value, right? I don't understand how casting it as a ponter works, but logically I thought this would be the way. Anyway - isn't this: "structInst->memeber" technically a pointer by definition?

What the best way to do what the intention of the code is? Please help me understand this...
Nov 14 '08 #4

boxfish
Expert 100+
P: 469
Anyway - isn't this: "structInst->memeber" technically a pointer by definition?
Your use of the arrow operator means that structInst is a pointer. member may not be. Is it? For now I'll assume it isn't.
I think that the code is assigning the pointer ptr to the value stored in "member". But "ptr" is an int, while "member" is a uint.
ptr is not an int. It is a pointer to an int. You should not store the address of an unsigned int in an int*. If you want to make an int variable and store the value from member in it, that's fine.
Expand|Select|Wrap|Line Numbers
  1. int intVar;
  2. intVar = structInst->member;
  3.  
If you want to create an dynamic int variable and store the value from member in it, that's fine too.
Expand|Select|Wrap|Line Numbers
  1. int *ptr = new int;
  2. *ptr = structInst->member;
  3.  
But if you want to store the address of member, use an unsigned int*:
Expand|Select|Wrap|Line Numbers
  1. unsigned int *ptr;
  2. ptr = &(structInst->member);
  3.  
Hope this makes it a bit clearer. Good luck.
Nov 14 '08 #5

P: 9
what are you asking?
Nov 14 '08 #6

Expert 100+
P: 2,415
What the best way to do what the intention of the code is? Please help me understand this...
That is precisely the issue. You asked a very specific question: how to cast an integer into a pointer. However, you haven't provided enough information for us to even guess at the intention of the code.

In my experience, these are the most common reasons for casting an integer into a pointer:
1) Poor software design, the wrong type was used.
2) Lazy software implementation, didn't go back and revise the types after the design changed.
3) Hard-coded address of memory-mapped I/O.
4) Reuse of a general library function.
Nov 14 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.