How to save pointer in C
In my app, for debugging I want to save a pointer, before I do other operations on it, e.g.
void foo(...)
{
/* suppose ptr1 points to one of my structs */
ptr1 = NULL;
/* before that ptr1=NULL I want to save value of that pointer - how to do it ? */
}
Thanks for any开发者_JAVA技巧 help
If by "saving the pointer", you mean saving the place it points to, it is simply:
ptr2 = ptr1;
If you mean saving the data ptr1
points to then:
memmove(ptr1, buffer, some_size); /* for void* pointers */
*buffer = *ptr1; /* for typed pointers */
mystruct *ptr;
mystruct copy= *ptr;
ptr=null;
Now copy has the value that was originally being pointed to by ptr
You do like so:
void foo(MyStruct *struct) {
MyStruct debugStruct = *struct;
// do stuff to struct
printf("Initial configuration: %s", debugStruct.stringField);
}
精彩评论