MEshtay
asked on
concatenating character to string.
Hi all
i wanna create word by adding characters one by one.
my code:
char c;
char *word;
word="";
c=fgetc(fp);
strcat(word,c); // ERROR
could u help me in this problem?
i wanna create word by adding characters one by one.
my code:
char c;
char *word;
word="";
c=fgetc(fp);
strcat(word,c); // ERROR
could u help me in this problem?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
char c;
char *word;
word="";
c=fgetc(fp);
strncat(word,&c,1); // NO ERROR
char *word;
word="";
c=fgetc(fp);
strncat(word,&c,1); // NO ERROR
Ooops, correction - it has to be
#define MAX_WORD 255
char c;
char word[MAX_WORD]="";
c=fgetc(fp);
strncat(word,&c,1); // NO ERROR
If you have a constant string literal like
word="";
you CANNOT concatenate anything without a crash...
#define MAX_WORD 255
char c;
char word[MAX_WORD]="";
c=fgetc(fp);
strncat(word,&c,1); // NO ERROR
If you have a constant string literal like
word="";
you CANNOT concatenate anything without a crash...
The underlying problem with your original example is that no memory has been allocated to your char* word.
If you define word as jkr did, char word[255], you will have 255 bytes allocated to word.
Alternately, you could get the memory dynamically,
char* word = (char*) malloc(sizeof(char) * 255);
Good luck.
If you define word as jkr did, char word[255], you will have 255 bytes allocated to word.
Alternately, you could get the memory dynamically,
char* word = (char*) malloc(sizeof(char) * 255);
Good luck.
yes, its for the 0 (null) terminated string. I forgot you can add a length to strcat, that is cleaner. Haven't used it in a while....
Reading directly into the string in a loop then ending it with the zero when done removes the need to call strcat at all. Anyway, any of these solutions will work for you...
Reading directly into the string in a loop then ending it with the zero when done removes the need to call strcat at all. Anyway, any of these solutions will work for you...
You need to add a few random characters to the final command:
char word[100]="";
c=fgetc(fp);
strncat(word,&c,1); // n & ,1 <---- NO ERROR
-- Dan
char word[100]="";
c=fgetc(fp);
strncat(word,&c,1); // n & ,1 <---- NO ERROR
-- Dan
jonnin - is it not strncat that takes a length. i.e. number of bytes to copy?
I looked it up, and:
the only strcat I can find in my reference materials is
strcat(dest,src); //stops copying at the 0/null char
so either there is an enhanced one that takes a length or it's compiling and ignoring the extra parameter. This would crash unless the memory violated happened to belong to the app AND a null char was quickly found (much of ram is often 0, no surpise here).
So I'm sticking to my orig post, manually null terminate or set up the string functions to do so. And allocate memory for the thing, or make array, as was mentioned by boneTke.
the only strcat I can find in my reference materials is
strcat(dest,src); //stops copying at the 0/null char
so either there is an enhanced one that takes a length or it's compiling and ignoring the extra parameter. This would crash unless the memory violated happened to belong to the app AND a null char was quickly found (much of ram is often 0, no surpise here).
So I'm sticking to my orig post, manually null terminate or set up the string functions to do so. And allocate memory for the thing, or make array, as was mentioned by boneTke.
jonnin - I totally agree that the character array has to be properly initialised and be null terminated, I was just saying that the string concatenation function that takes a length (or size in bytes) is called strncat (note the 'n').
Ah. I can't see well and missed the n. In all the posts, I missed it... VERY Sorry for the confusion!
Yes, which means strNcat will work fine... which is why I didn't remember strcat taking a length (it didn't!)...
feel free to forget I said anything =)
Yes, which means strNcat will work fine... which is why I didn't remember strcat taking a length (it didn't!)...
feel free to forget I said anything =)
Happens to the best of us mate ;)
The trick is to insert random letters into the code at random places. Sometimes symbols, such as & and [ can come in handy too. That little dot above the comma has been useful in some assignments. When it compiles without error, you can turn it in and get an A.
-- Dan
-- Dan
Just a suggestion....