initializing char pointers

ant2009 picture ant2009 · Nov 2, 2009 · Viewed 50.5k times · Source

I have a char pointer which would be used to store a string. It is used later in the program.

I have declared and initialized like this:

char * p = NULL;

I am just wondering if this is good practice. I'm using gcc 4.3.3.

Answer

f0b0s picture f0b0s · Nov 2, 2009

Yes, it's good idea. Google Code Style recommends:

  1. To initialize all your variables even if you don't need them right now.
  2. Initialize pointers by NULL, int's by 0 and float's by 0.0 -- just for better readability.

    int i = 0;
    double x = 0.0;
    char* c = NULL;