[SOLVED] Address of value pointed to by pointer.

Status
Not open for further replies.

Pheetuz

Full Member level 3
Joined
Feb 12, 2010
Messages
162
Helped
21
Reputation
42
Reaction score
19
Trophy points
1,298
Location
Brighton
Activity points
2,835
Hi Folks,

I have a pointer that has been returned to me via a malloc function on my PIC microcontroller, what I need to do is take the address of this pointer and store it as an INT such that I can place it in the FSR0L:FSR0H pair so that I can then use these registers for copying the stack, I am however having some difficulty doing this as I cannot seem to get the address of the variable pointed to by the pointer as a numerical value.

I have tried things such as:

UCHAR * stkPtr = NULL;
UINT stkAdd = 0;

void main (void){

malloc(stkPtr);

stkAdd = stkPtr;

}


or


void main (void){

malloc(stkPtr);

stkAdd = &(*stkPtr);

}

but to no avail, I know I am overlooking something stupid and would greatly appreciate it if someone could point me in the right direction.

Cheers,

Pheetuz
 

Hi,

The prototype of malloc() is:

void * malloc ( size_t size );

so you should use it like this:

stkPtr = malloc(number_of_bytes_required);

and if you want the address, it would be correct to declare stkAdd as a pointer of the needed type, that can be initialized like this:

UCHAR * stkPtr;
UCHAR * * stkAdd = &stkPtr;

Otherwise, declaring stkAdd as UINT you should use:
stkAdd = (UINT)&stkPtr;

nevertheless, maybe what you need is not exactly what you describe.
Regards

Z
 
Thanks zorro,

You were right about the malloc prototype apart from it is my own malloc function so it was slightly different but still I now have the numeric value of the address of the pointer, I was not casting the pointer and thats what the problem was... I really need to look into casting pointers as its something I have not really done a lot of.

Thanks again!
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…