Connecting Tech Pros Worldwide Forums | Help | Site Map

add a char to char*

iLL iLL is offline
Member
 
Join Date: Oct 2006
Posts: 62
#1: Oct 8 '07
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. ...
  3.  
  4. char ch = 'a';
  5. char *str;
  6.  
  7. str = (char*)malloc(x);    //x is some integer
  8.  
  9. str[y] = ch;            //y is some integer < x
  10.  
  11. free(str);
  12.  
  13. ...
  14.  
  15.  
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's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,202
#2: Oct 8 '07

re: add a char to char*


Quote:

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.
Reply