Thread: Appending a char to a char *foo;

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    Appending a char to a char *foo;

    Hello all,

    I am trying to append a char to a char* which is returned from a function -- I'll demonstrate this with an example:

    Code:
    typedef struct
    {
    	char key;
    	int  value;
    } charmap_t;
    
    charmap_t t[] =
    {
    	{'s', MaskS},
    	{'c', MaskC},
    	{'l', MaskL},
    	{'m', MaskM},
    	{'1', Mask1},
    	{'2', Mask2},
    	{'3', Mask3},
    	{0, 0}
    };
    
    char *modifier_to_string(int mask, charmap_t *t)
    {
    	char *mods = malloc(20);
    	char c;
    
    	for (c = 0; t->key !=0; t++)
    	{
    		if (mask & t->value)
    		{
    			c = table->key;
    			strcat(mods, (char *)&c);	
    		}
    	}
    	return allmods;
    }
    This piece of code works OK -- although when trying to print the result returned from the function above I get garbage, as in:

    Code:
    char *mod_string = modifier_to_string(2, modifiers);
    fprintf(stderr, "Modifier:\t%s\n", mod_string;
    I know that "2" exists, and that modifiers (of type charmap_t) is being iterated over, but still I get garbage.

    Any ideas?

    Thanks!

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    strcat starts at the first null character but there isn't one in mods. before the loop say *mods='\0'; or use calloc instead of malloc so the memory is zero filled.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    The problem is that strcat has to receive null terminated strings as parameters. In your example function, neither mods nor c are necessarily null terminated (In fact, evaluating (char*)&c until it finds a '\0' may crash your program, as you're accessing memory you don't own).

    Furthermore, I'd use a different name for a global variable. 't' is a far too common name.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    Quote Originally Posted by Meldreth View Post
    strcat starts at the first null character but there isn't one in mods. before the loop say *mods='\0'; or use calloc instead of malloc so the memory is zero filled.
    Gosh, that's it! Thank you. I feel so stupid, I forgot that step. In the case of doing that, don't I then have to free(mod_string) after I've printed it out?

    Thanks!

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    you should have been freeing it anyway. malloc and calloc are the same thing. the only difference is calloc zero fills the memory and malloc doesn't. ronix is right too. you need to turn c into a string also.
    Code:
    char c[2] = {0};
    for (c = 0; t->key !=0; t++)
    {
    	if (mask & t->value)
    	{
    		c[0] = table->key;
    		strcat(mods, c);

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I think it is a little bit of overhead to use strcat for assigning just one char at a given location of the array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM