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!

Pointers to constants?

Status
Not open for further replies.

JasonWB007

Programmer
Dec 21, 2005
14
US
Hi,

Looking through some code, I found a function of the format:

int function (parms..., (void *)0, parms...);

Is this legal? I have a need to pass a constant to a function and I really don't want to define a variable to hold it. I have a function that will either read or write a device. When I read the device, I pass it a pointer to the variable to hold the result (return value is status of success or failure), but when I write to it, I would like to simply pass it a constant sometimes, not the address of a variable. Why is the above OK (it seems to work)?

Thanks,

Jason
 
The function itself is defined as:

int EEPROM_IO(unsigned int addr, unsigned int *dataptr, unsigned int len, int dir);

The variable "dataptr" is used to hold the returned value when the EEPROM is read, but when written, sometimes it would be very useful (clearing the EEPROM, for example) simply to pass a constant value. Note: dir = READ or WRITE.

Thanks,

Jason
 
What's wrong with creating an unsigned int and passing its address? If you're doing this in a big loop for some reason, you can declare the unsigned int outside the loop so it isn't re-created after each iteration...
 
That is what I have done. But it seems a waste of a variable and for some reason, the "(void *)0" seems to work. Do you know what it actually is doing? Am I just getting lucky?
 
I think you're playing with fire... If that parameter is marked as an output parameter, you should never pass a NULL pointer.

We just had the very same thing happen at my company a couple weeks ago when a customer reported a crash message. The product was working fine for years, but suddenly this one customer was ABENDing (the mainframe equivalent of a crash). One place in the code was passing a NULL to a system function for an output parameter. Since we only used that function for a certain thing, it never needed to write to the NULL pointer, but somehow this customer managed to make it crash...

BTW, I believe NULL is #defined as: (void*)0
 
> int EEPROM_IO(unsigned int addr, unsigned int *dataptr, unsigned int len, int dir);
Unless the function is clearly documented with
"If you don't want to store the answer, pass NULL for dataptr"
then passing NULL just means it's going to access that memory location.
For all the reasons cpjust said, this is a bad thing.

--
 
Look for specification of your function - constant is legal if parameter dataptr is described as unused for the WRITE case.
 
Well, I wrote the function also, so I can change if, if need be. For the READ case, this pointer holds the return value. For the WRITE case, this variable is the pointer to the data we wish to write. For the CLEAR case, it is ignored. So, this should work:

status = EEPROM_IO(0, (void *)0, (unsigned int)EEPROM_SIZE, CLEAR);
 
JasonWB007 said:
For the READ case, this pointer holds the return value. For the WRITE case, this variable is the pointer to the data we wish to write.
Am I just being thick here or...

... well, if I understand you right, the second argument is a pointer to the value you wish to write, or read to. So if [tt]x[/tt] is the variable whose value you wish to pass, you would write,
Code:
status = EEPROM_IO(0,&x,(unsigned int)EEPROM_SIZE,CLEAR);
... right? So why pass [tt](void *)0[/tt]?
 
Because of this:
JasonWB007 said:
For the CLEAR case, it is ignored.
and he doesn't want to create a temporary x variable that won't be used.

If you're really worried about creating lots of temporary ints, you could create a static int, that way it will only be created once.
 
Remember: zero (0) literal is int (integer 0) or pointer to (nothing, null pointer value) constant.
So (void*)0 is a correct casting (0 as a pointer to), but (void*)1 is not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top