Menu

Empty an array and null a pointer, how?

2003-02-14
2012-09-26
  • Nobody/Anonymous

    I got this two vars in my program inside a function,
    char *pDot;
    char CurScheme[61];

    I need to be sure that at some point of my program CurScheme is empty and *pDot points to nothing. How can that be done?

    Thanks in advance

     
    • upcase

      upcase - 2003-02-14

      maybe pDot = NULL; or pDot = (char*)NULL;
      To "empty" the array try CurScheme[0]='\0';
      The array isn't really empty, but when you output it it will look like empty, because \0 is a "terminator"

      upcase

       
    • Anonymous

      Anonymous - 2003-02-14

      An array can never be 'empty', it is memory, it must contain something. If CurScheme is a string, then setting element [0] to '\0' will terminate it. If it is simply an array of chars then you can initialize the array to any value with memset().

      // init CurScheme[] to all zero.
      memset( CurScheme, sizeof(CurScheme), 0 ) ;

      Simply setting pDot to NULL or zero will suffice, since it is illegal to dereference a null pointer in C/C++. A run-time error occurs if you attempt it, but you can test the pointer for NULL or zero to check if it is initialised or not.

      Note that ant static variables are always automatically initialised to zero, so if you declare static or glovbal data you can rely on this aspect of C/C++.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.