LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-03-2014, 11:53 AM   #1
leadfree
LQ Newbie
 
Registered: Jul 2013
Location: Ireland
Distribution: Slackware
Posts: 17

Rep: Reputation: Disabled
Passing numerical constant to a function as pointer to int.


Language: C

This seems like a trivial task, but I can't find an answer in the books or online.

I want to declare the const value 0x1234 in a function call, but the function takes a pointer to int.

The function: void myfunction(int*)

I want to do something like myfunction(??? 0x1234 ????);

It is similar to passing a string constant
funct(char* "some ascii text");
except I want to pass a numerical value, not ascii.

Help appreciated.
 
Old 08-03-2014, 12:08 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Well, don't. Instead:
Code:
int value= YOUR_CONSTANT;
myfunction (&value);
 
Old 08-03-2014, 12:31 PM   #3
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 08-03-2014, 01:06 PM   #4
leadfree
LQ Newbie
 
Registered: Jul 2013
Location: Ireland
Distribution: Slackware
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thanks for replies.


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.
 
Old 08-03-2014, 01:22 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
IO addresses aren't memory addresses, so it would't work.
Maybe this will help: http://www.tldp.org/HOWTO/IO-Port-Programming.html#toc2
 
Old 08-03-2014, 01:23 PM   #6
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 08-03-2014, 01:51 PM   #7
leadfree
LQ Newbie
 
Registered: Jul 2013
Location: Ireland
Distribution: Slackware
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
IO addresses aren't memory addresses, so it would't work.
Maybe this will help: http://www.tldp.org/HOWTO/IO-Port-Programming.html#toc2
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.
 
Old 08-03-2014, 02:09 PM   #8
leadfree
LQ Newbie
 
Registered: Jul 2013
Location: Ireland
Distribution: Slackware
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by metaschima View Post
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")
 
Old 08-03-2014, 02:35 PM   #9
leadfree
LQ Newbie
 
Registered: Jul 2013
Location: Ireland
Distribution: Slackware
Posts: 17

Original Poster
Rep: Reputation: Disabled
I make an error which might be important.

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?");

Last edited by leadfree; 08-03-2014 at 02:43 PM.
 
Old 08-03-2014, 03:22 PM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by leadfree View Post
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).
 
Old 08-03-2014, 05:45 PM   #11
leadfree
LQ Newbie
 
Registered: Jul 2013
Location: Ireland
Distribution: Slackware
Posts: 17

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-03-2014, 06:18 PM   #12
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
The new thread that he started is here:

http://www.linuxquestions.org/questi...ar-4175513198/
 
Old 08-03-2014, 11:38 PM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
why omit unsigned int in unsuffixed decimal integer constant? password636 Programming 1 11-15-2011 11:00 PM
C: a function that would accept both INT and INT[] courteous Programming 5 05-28-2011 07:40 AM
( C ) How do you declare a function pointer where a parameter is a function pointer? spursrule Programming 5 11-27-2007 07:56 PM
awk command to multipy $(field) by numerical constant? johnpaulodonnell Linux - Newbie 3 01-31-2007 06:43 AM
passing function pointer as argument worldmagic Programming 7 08-04-2004 03:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration