Thread: Not populating a string

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    50

    Not populating a string

    Hi

    I have declared a string. If i dont populate the string, can i check this string for NULL? If i dont populate the string, then with what value it contains? Does it contains gurbage value?

    If i initialize the string with memset function the can i check the string with NULL?

    Thanks
    Sas

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It depends how you declare the string...

    Code:
    char *string;     <--- will point to garbage
    
    char *string = NULL;   <--- this will test true for null
    
    char *string = malloc(100); <--- this will conatain garbage
    
    char *string = calloc(1,100); <--- strlen() will return 0
    
    char string[100];  <--- declared as global, strlen() will return 0
    
    char string[100]; <--- declared in a function will contain garbage
    
    char string [100] = {0};  <--- strlen will return 0

  3. #3
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    How are you declaring the string? NULL is for pointers, but a string is just a convention that some sequence of characters ends with '\0'. If your string is actually an array, NULL will not be useful. If your string is a pointer, it is best to initialize it to NULL yourself. That is easier than remembering the rules of where variables are initialized to zero and where they are not.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    50
    I am defining the string in a structure. like below

    Code:
    struct RunParam
    {
    
    char redEyeExcp[RED_EYE];
    int upperTimeLimit;
    }
    This structure is declared in header file. In the code, i am doing following things

    Code:
    struct RunParam *modelRunParams=( struct RunParams*)calloc(1, sizeof(struct RunParams));
    .

    Now what i am seeing is each element redEyeExcp char array is populated by '\0' and intgers and floats are automatically initialized with 0, 0.000.
    Are they automatically initialized when calloc works

  5. #5
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    calloc() just combines malloc() and memset() with a value of 0. It is not safe to use memset() on anything but int or char types. You have to initialize the members separately.
    Code:
    struct RunParam* modelRunParams = (struct RunParams*)malloc(sizeof(struct RunParams));
    
    modelRunParams->redEyeExcp[0] = '\0'; /* Empty string, strlen() will return 0 */
    modelRunParams->upperTimeLimit = 0;

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    50
    Thank you

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    50
    Why it is not safe to use memset apart from int and char?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It is not guaranteed that all-bits-zero will give you the value 0 for floating-point types (it does for IEEE floats which are very common). It is also not guaranteed that NULL is all-bits-zero, or even that all-bits-zero is a valid pointer representation.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Bah! You'd do fine on a C standards committee. Real world programming has been OK with zeros in each data type for every architecture/machine since the late 70s.
    Please cite real-world examples of where floating point all-zero is not legit, or where a NULL pointer is not zero.

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    int main(void)
    {
    	
    	double f;
    	memset(&f,0,sizeof(f));
    	
    	printf("%f\n",f);
    	
    	char *p;
    	memset(&p,0,sizeof(*p));
    	
    	if(p == NULL) {
    		printf("ok null\n");
    	} else {
    		printf("nonoob is real noob\n");
    	}
    	
    return 0;
    }
    
    0.000000
    nonoob is real noob
    http://c-faq.com/null/machnon0.html
    http://c-faq.com/~scs/cgi-bin/faqcat...=malloc#calloc
    Last edited by Bayint Naung; 06-03-2011 at 12:24 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nonoob View Post
    Bah! You'd do fine on a C standards committee. Real world programming has been OK with zeros in each data type for every architecture/machine since the late 70s.
    Please cite real-world examples of where floating point all-zero is not legit, or where a NULL pointer is not zero.
    I've not used them personally, but I thought the old-but-not-as-old-as-the-70s segment:pointer system didn't have an all-zero NULL. That might have been just a scary story told by my programming instructors though. (And I don't know what system Bayint was using.)

  12. #12
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by tabstop View Post
    (And I don't know what system Bayint was using.)
    Obviously one where sizeof(char**) != sizeof(char) and which didn't 0-fill stack space

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. populating 2-D array
    By edster in forum C Programming
    Replies: 4
    Last Post: 10-22-2009, 02:42 PM
  2. Populating a Struct
    By larry_2k4 in forum C Programming
    Replies: 4
    Last Post: 10-21-2009, 09:27 PM
  3. populating arrays
    By jafa401 in forum C Programming
    Replies: 12
    Last Post: 08-04-2009, 11:22 PM
  4. populating a string only alpha characters
    By MSF1981 in forum C Programming
    Replies: 10
    Last Post: 02-06-2009, 10:58 AM
  5. populating arrays
    By john_murphy69 in forum C Programming
    Replies: 7
    Last Post: 04-04-2003, 12:03 PM