Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-17
14
Medium Priority
?
260 Views
Last Modified: 2010-04-15
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

0
Comment
Question by:petse
  • 3
  • 3
  • 3
  • +3
14 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8156310
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
 
LVL 6

Expert Comment

by:gj62
ID: 8156359
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8156461
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:gj62
ID: 8156490
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8156597
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
 
LVL 20

Accepted Solution

by:
jmcg earned 1000 total points
ID: 8156888
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
 
LVL 22

Expert Comment

by:grg99
ID: 8162024
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
 
LVL 5

Expert Comment

by:Kocil
ID: 8163387
> 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
 
LVL 5

Expert Comment

by:Kocil
ID: 8163414
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
 

Author Comment

by:petse
ID: 8163586
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8163742
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
 
LVL 20

Expert Comment

by:jmcg
ID: 8163800
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
 

Author Comment

by:petse
ID: 8170778
jmcq, akshayxx,
I will open a new question to give 250 points to akshayxx.
Thanks guys!
 
0
 
LVL 20

Expert Comment

by:jmcg
ID: 8171239
Handsomely done, indeed. Glad we could help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question