[SOLVED] Passing numerical constant to a function as pointer to int.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The problem is that '0x1234' in this exact form is an immediate value, it has no address, and therefore it cannot be passed to a function that requires an address.
However, you could use a hack like:
Code:
myfunction ("\064\022");
You'd have to get the ascii values of whatever you wanted to pass in, and you would have to account for endianness. This is a hack and you should just declare a constant like NevemTeve says, because it is easier to implement and saner.
Last edited by metaschima; 08-03-2014 at 12:33 PM.
The constant is actually declared in a header file for the chip eg #define StatusRegister (0x1005). There is a very large number of such constants, and I want to avoid creating an equal number of ints for that file.
I want to avoid writing a duplicate function which takes an int eg void myfunction(int)
metaschima's suggestion probably works, but I'd like something easily readable eg myfunction(???StatusRregister???)
I am certain I found the syntax to do it last year, but I have forgotten it. If there isn't a way to do it I will eat my keyboard.
So, just creating one integer and setting it right before calling the function is not optimal ? Something like:
Code:
int value= YOUR_CONSTANT;
myfunction (&value);
int value= YOUR_CONSTANT1;
myfunction (&value);
int value= YOUR_CONSTANT2;
myfunction (&value);
Really, the problem is you shouldn't declare huge tables of values using #define. Its use should be restricted to cases of substitution or a small number of values that are NOT passed around to other functions in other files. For large projects and large numbers of constants use the 'const int' instead of #define. If you had them as 'const int' there would not be a problem. Convert them all, nobody will notice.
It doesn't matter that those addresses are off-processor, the function void myfunction(int*) takes care of communicating with the chip. I can of course write code to convert the constants to ints, but I want to avoid it for reasons above and others.
So, just creating one integer and setting it right before calling the function is not optimal ? Something like:
Code:
int value= YOUR_CONSTANT;
myfunction (&value);
int value= YOUR_CONSTANT1;
myfunction (&value);
int value= YOUR_CONSTANT2;
myfunction (&value);
Really, the problem is you shouldn't declare huge tables of values using #define. Its use should be restricted to cases of substitution or a small number of values that are NOT passed around to other functions in other files. For large projects and large numbers of constants use the 'const int' instead of #define. If you had them as 'const int' there would not be a problem. Convert them all, nobody will notice.
I know a number of the common ways of making it work and try to avoid them for various reasons.
The header file is supplied, so it is extra work making the ints (there are a LOT of them).
Creating all those ints uses up memory, even if they are decalred const int and stored in ROM.
I am 99% sure I did it before and it worked just like myfunction("ascii text")
The function actually takes a pointer to char, so it is void myfunction(char*)
The declarations in the header file are char length eg #define low_voltage (0xA5)
'My apologies if I made it seem more difficult than it is.
Maybe there is a variation on the construct myfunction("ascii text"); which takes hex values eg
myfunction("?0xA5?"); which would then read like myfunction("?low_voltage?");
I want to avoid writing a duplicate function which takes an int eg void myfunction(int)
Why? Make it static and gcc will know to inline (assuming you compile with optimization).
Quote:
It doesn't matter that those addresses are off-processor, the function void myfunction(int*) takes care of communicating with the chip.
Wait, are those integers actually addresses? Maybe you want
Code:
myfunction((int*)YOUR_CONSTANT);
Note, this has a different meaning then the other solutions. If the others are correct then this one is wrong (and vice versa). It depends on how myfunction works.
Quote:
Creating all those ints uses up memory, even if they are decalred const int and stored in ROM.
No, they are stored on the stack, and the compiler can reuse the same spot on the stack (again, assuming optimizations enabled).
I'm afraid I was too ambiguous in my first post.
At the risk of straining helpful users, I will start a new thread which hopefully will better convey what I have in mind.
I might be mistaken in what I am trying to do, and I am making a marinade for that keyboard just in case.
The first and biggest problem is assuming that 'myfunction' expects a pointer and uses it as IO-address. It's highly unlikely it it was written by an actual programmer.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.