add a char to char*

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iLL
    New Member
    • Oct 2006
    • 63

    add a char to char*

    I can't believe I'm starting a thread about this, but how are you suppose to add a char to a string. I've been using the following method for as long as I can remember.

    Code:
    ...
    
    char ch = 'a';
    char *str;
    
    str = (char*)malloc(x);	//x is some integer
    
    str[y] = ch;			//y is some integer < x
    
    free(str);
    
    ...
    gcc -Wall gives a warning of “warning: array subscript has type ‘char’”. Even though ch is a f-ing char.

    Anyway, my code is not suppose to have any warnings in it at all. What is the proper way of adding a char into a char array if not this way?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by iLL
    gcc -Wall gives a warning of “warning: array subscript has type ‘char’”. Even though ch is a f-ing char.
    This warning is to do with the type of y not the type of ch, explain here although it wonders a little off topic after the first 10 or so posts.

    Comment

    Working...