• C

How to pass in hard coded value in place of a pointer?

I have a function that takes in some pointer.  However I want to pass in a hard coded value instead of a pointer.  How can I do this?  I am currently trying to use a #define, but am not sure if can work.  

For dilema, say I have the following data at memory location 0x1d000000.

0x1d000000 01 04 aa ff 00 00 00 00

This data can be interpreted as follows:
byte 0 value=0x01  Generic Header
byte 1 value=0x04  Size
byte 2 value=0xaa  actual data
byte 3 value=0xff  End

Instead of passing a pointer that points to 0x1d000000, I would like to pass in hardcoded values, possible using a define as follows:

#define MEMORY (0x0104aaff)

main () {
...

somefunction((UINT64 *)MEMORY);  //type cast #define??
                                 //how else can this be done

...
}

Any help would be appreciated!
Thanks

petseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gj62Commented:
If you #define it outside of main(), you don't need to pass it, it would be global.

how about just

#define MEMORY 0x0104aaff

You can pass it, of course...

somefunction(long myVar)
{
}

In main(), you can call it as:

somefunction(MEMORY);
0
gj62Commented:
Or, if you don't want to change the function declaration (don't know why you'd want to do this, of course), you can do something like:

#define MEMORY 0x0104aaff

somefunction(void * aPointer)
{
}

In main() you can call it as

somefunction((void *)MEMORY);

You can cast it however you need to...
0
akshayxxCommented:
given  the following declarations as given by 'gj62'
---------------------------
#define MEMORY 0x0104aaff

somefunction(void * aPointer)
{
}

In main() you can call it as

somefunction((void *)MEMORY);      
-----------------------------

i think the function call should be like this

somefunction((void *)(&MEMORY));

Assuming he want to pass the value thats defined as MEMORY

yes he can read the address of the pointer passed as the value but .. that doenst go with his question statement ..
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

gj62Commented:
Hmmm, they said he didn't want to pass a pointer to the value, but the value itself.  I don't know why they don't just change the function declaration from pointer to long, but the statement:

"Instead of passing a pointer that points to 0x1d000000, I would like to pass in hardcoded values, possible using a define as follows:

#define MEMORY (0x0104aaff)"

sounds like they wants to do what I showed.  We'll have to wait until Petse responds, I guess...
0
akshayxxCommented:
yea lets wait
btw when it is #define  
why does he need to pass it at all :) .. assuming all of the code is his
0
jmcgOwnerCommented:
Petse, it sounds like you're getting tangled up in a couple of different issues. I'm going to try to restate your question into one I think I can answer -- but it'll be up to you to tell me if my restatement comes close enough to doing what you want.

"I have a function that normally takes a pointer as argument. At times, I want to pass a value directly to this function -- I'm calling this notion _hardcoded_. Can I somehow, in the subroutine calling sequence, have the compiler generate a pointer to the hardcoded value?"

There is no difficulty if you didn't have the part about making some magic happen in the subroutine calling sequence.

I can think of two possible approaches.

First, encode the data as a string.

#define MEMORY "\x01\x04\xaa\xff"

main () {
...

somefunction((UINT64 *)MEMORY);

...

The compiler allocates space for string storage and generates the pointers to them automatically. It's still up to you to get the data properly aligned and endianized to match the type of pointer you're casting to. There are architectures where that cast to UINT64 * would require you to add 4 bytes of \x00 to the string or to define the bytes in the opposite order.

The other alternative is the more normal way this sort of thing is done -- you create some initialized storage and pass a pointer to it.


static unsigned long MEMORY = 0x0104aaff;

main () {
...

somefunction((UINT64 *) &MEMORY);

...

The same caveats about casts apply.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
grg99Commented:
This is kind of a screwy concept, but you can do it with a good ol define macro:



#define  CallWithThisLiteral(Hex)  { UINT64 X=Hex; SomeFunction( (UINT64 *) &X );  }



.. then you can use it this way:


CallWithThisLiteral( 0x12345678 );

---------------------

Or you can define a helper function:

sometype SomeFunctionCaller( UINT64 X )
{
    SomeFunction( (UINT64 *) &X );
}

Note that in  both the cases above, the address is only valid for the duration of the enclosing { } block.
If you need the literal to stick around, you need something else.

0
KocilCommented:
> For dilema, say I have the following data at memory location 0x1d000000.

> 0x1d000000 01 04 aa ff 00 00 00 00

> Instead of passing a pointer that points to 0x1d000000, I would like to pass in hardcoded values, possible using a define as follows:

> #define MEMORY (0x0104aaff)
> main () {
> ...
>  somefunction((UINT64 *)MEMORY);
> ...
> }

That won't work bacause you mixed up the DATA as POINTER.
Please use this instead.

const UINT64 DATA = 0x0104aaff;
const UINT64* MEMORY=&DATA;

main()
{
   somefunction(MEMORY);
   ..
   somefunction(&DATA);
}

The problem is if you need the DATA is exactly at the memory address 0x1D000000. You need assembly langguage for this.

PUBLIC _DATA
MEMORY equ 1d000000h

.TEXT ORG MEMORY
_DATA QWORD 0104AAFF00000000h


And in the C you need
extern UINT64 DATA;
const UINT64* MEMORY=&DATA;
0
KocilCommented:
Oh ... I forgot. If you want to modify the value of the data use this instead.


UINT64 DATA = 0x0104aaff;
const UINT64* MEMORY=&DATA;

main()
{
  somefunction(MEMORY);
  ..
}
0
petseAuthor Commented:
Thanks for your help guys!  I am going with jmcg's suggestion... "The other alternative is the more normal way this sort of thing is done -- you create some initialized storage and pass a pointer to it."

0
akshayxxCommented:
hmm gj and me said similar thing..just that he missed & ( with a reason) , and i corrected it ( with another reason)..

the only difference being (from selected answer) .. #define and initialised storage

and in this case creating an initialised storage can be as good as #define
..anyways you know what suits you best
0
jmcgOwnerCommented:
Thanks, Petse.

Since it's come up, I'll just mention that you can ask over in the Community Support forum for one of the moderators to split the points between the answers. If you look at some of the already-answered questions in that forum, you'll see how it's done.
0
petseAuthor Commented:
jmcq, akshayxx,
I will open a new question to give 250 points to akshayxx.
Thanks guys!
 
0
jmcgOwnerCommented:
Handsomely done, indeed. Glad we could help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.