tags:

views:

115

answers:

3

Hi

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 help

A: 

You do like so:

void foo(MyStruct *struct) {
  MyStruct debugStruct = *struct;

  // do stuff to struct

  printf("Initial configuration: %s", debugStruct.stringField);
}
Williham Totland
+1  A: 
mystruct  *ptr;
mystruct copy= *ptr;
ptr=null;

Now copy has the value that was originally being pointed to by ptr

Midhat
+3  A: 

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 */
petersohn