You are here:

C/seting a struct pointer to NULL

Advertisement


Question
Hi Narendra,
Thanks for your quick replies. Your last solution surely works. However, in my case I cannot change the synopsis of the CloseDisplay. This is a part of a common library (Minix XCloseDisplay function) and the synopsis should not be changed because of the compability issues. The parameter in Display type should be set to NULL after CloseDisplay. But I want this operation to be performed inside CloseDisplay to make sure that it is always set to NULL before CloseDisplay exits.

I feel that I'm looking for an impossible solution for C/C++.

Regards,
Cem
-------------------------

Followup To

Question -
Here is my code:

#include <stdio.h>
#include <stdlib.h>

// Some struct
typedef struct{
  void *ptr;
  int x;
  int *y;
}Display;

void CloseDisplay(Display *dpy)
{
   /*

  Do some stuff ...

  */

  *dpy=NULL; // Compiler does not allow that
  
}

int main(){

  Display *dpy=(Display *)malloc(sizeof(Display));
  CloseDisplay(dpy);
  // dpy should be NULL at this point
  return 0;
}

I want the dpy pointer in main function to be set to NULL after CloseDisplay is called. I'm looking for a solution in CloseDisplay function. Is this possible? or Is it strictly forbidden?

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

Followup To

Question -
Hi,
I have a function like that

void CloseDisplay(Display *disp){
...
}

where Display is a typedef struct

I would like to set *disp to NULL. But compiler does not allow me to set the top level pointer.
Is there any hack to do that?

This is an ANSI C level question so the solution must work on any C compiler.

Regards,
Cem POLAT

Answer -
Your question is not clear to me.
Any pointer can be set to NULL.

I will try to guess what you are doing and answer that here.
Say your program is:
void CloseDisplay(Display *disp)
{
    *disp = NULL;
}
If this is what you are doing, then it is wrong.
Instead you must do:
     disp = NULL;

While disp is a pointer, *disp is "NOT" a pointer. It is data of type Display and since that is not a pointer, you cannot initialize this to NULL.

If my guess is wrong and this is not what you are doing, then continue reading the rest of the mail...

If, you are experiencing problems then there must be some mistake in your declaration/definition itself.

Are you getting compilation errors?
If yes, what are those errors?

Why don't you post your code?
If it is very lengthy or you don't want to post it, then write a sample code, which can show the same behavior and post that sample code.

If you can answer the above queries, then I can try to help you out.

Answer -
Try this and see if it works.
Do let me know whether it worked or not!? (I am really curious to know the result)

#include <stdio.h>
#include <stdlib.h>

// Some struct
typedef struct{
void *ptr;
int x;
int *y;
}Display;

void CloseDisplay(Display **dpy)
{
  /*

Do some stuff ...

*/

*dpy=NULL; // Compiler does not allow that

}

int main(){

Display *dpy=(Display *)malloc(sizeof(Display));
CloseDisplay(&dpy);
// dpy should be NULL at this point
return 0;
}

Answer
OK. So, you want to keep the same prototype for the function.
Then, there is one more way, you can try:

#include <stdio.h>
#include <stdlib.h>

// Some struct
typedef struct
{
       void *ptr;
       int x;
       int *y;
} *Display, Display1;

void CloseDisplay(Display *dpy)
{
       *dpy=NULL; // Compiler does not allow that
}

int main()
{
       Display1 *dpy=(Display1 *)malloc(sizeof(Display1));
       CloseDisplay(&dpy);
       // dpy should be NULL at this point
       if (dpy == NULL)
       {
         printf("dpy is NULL\n");
       }
       else
       {
         printf("dpy is not NULL\n");
       }
       return 0;
}

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.