Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
char hi[10] = "bye";
char a = 'a';
strcat(hi, a);
Like the example above. How would I do this in C? Is there a more general string I cant let hi be?
a is a char type while strcat expects it's both arguments of type char *. To append the char to an array of characters you can do this
int index = strlen(hi);
if(index < sizeof(hi)-1)
hi[index] = a;
Note that in this particular case the initializer will initialize the first three elements of hi to b, y and e respectively. The rest of the elements will be initialized to 0. Therefore you do not need to take care of the null termination of the array after appending each character to it. But in general you have to take care of that.
int index = strlen(hi);
if(index < sizeof(hi)-1){
hi[index] = a;
hi[index+1] = '\0';
}
strcat(hi, (char[]){a,0});
This would append the a.
Or you can do this
char s[]={a,0};
strcat(hi,s);
Or simply
#define MAXLEN 10
...
size_t len = strlen(hi);
if( len+1 <= MAXLEN-1)
hi[len]=a,hi[len+1]=0;
else
// throw error.
In your case hi[len+1]=0 is not required as it is already filled with \0. Also as mentioned by Serge that you can use simply used the string literal as the second parameter to the strcat function.
strcat(hi,"a");
There is a subtle difference in this two as mentioned by Serge again, that string literals are const but the compound literals are not.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
This function make a strange error after using it several times and I really can't understand the reason behind it.
char *get_range(char *str,int min,int max){
char *_res=(char *)malloc(sizeof(str));
int cur=0;
while (min<max){
_res[cur]=str[min];
min++;
cur++;
}
return _res;
}
The problem is that after using this function several times, the output comes with additional chars and I don't understand why.
Notice: The additional chars are allway used returned by the function beffor
char *_res=(char *)malloc(sizeof(str));
is wrong. sizeof(str) is measuring the size of a char pointer. This is either 4 or 8 (typically) depending on your system (32 or 64 bit).
You need
char *_res=(char *)malloc(strlen(str) + 1);
strlen returns the number of characters in the string, and you need to add 1 for the terminating 0;
Second you have to add a terminating zero at the end, do:
_res[cur] = '\0';
before returning
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying my best to understand the memchr function but having some issues with some simple output. I'm getting extra characters at the end of *newchar.
#include <stdio.h>
#include <string.h>
int main() {
char plus[6] = "12+123";
char *newchar = (char*) memchr(plus,43,3);
printf("%s",newchar);
}
output:
+123( '
I expected to get "+123," why does it give me the extra characters? I noticed the output is consistent which confuses me earlier, it doesn't seem like these were grabbed from somewhere random in memory but were caused by the memchr function.
char plus[6] = "12+123";
You've defined an array of size 6, and initialized it with 6 characters. You haven't left enough room for the NUL terminator. There is garbage at the end of your string, and printf doesn't know when to stop printing it.
Do this instead, allowing the string to be appropriately sized automatically:
char plus[] = "12+123";
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to count a char*, but my looping never stop until it reach the last part of memory allocation...?
Here's what I mean:
char* text1 = "Hello Guys!";
char* text2 = "i dont know why";
char* text3 = "Hello World";
int counter = 0;
for(char* temp = text1;temp != '\0';)
{
++temp; ++counter;
}
//then i printed Counter
But for some reason, my counter shows 106506.
When I debug those, after temp shown "!" and then it goes to "" it keeps going. :(
and after a few step, my char* (temp) reach the text2 memory, and text3 memory. "I don't know why" and "Hello World" are counted as well.
temp is a pointer and it won't be equal to '\0', which is 0, in the loop.
To access to the character pointed at by that, use *temp.
First of all you should not assign string literal to char * even if compiler allows that for compatibility, second you do not need additional pointer:
const char* text1 = "Hello Guys!";
int counter = 0;
for( ; text1[counter]; ++counter );
but better use strlen():
int counter = strlen( text1 );
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I need to fill manually an array of chars. I declared it like this:
char* coor = malloc(sizeof(char) * 5);
Then I manually assigned every variable to its position:
coor[0] = O_colum;
coor[2] = ((char)(O_row+48));
coor[3] = '-';
coor[4] = D_colum;
coor[5] = ((char)(D_row+48));
(D_Row and O_row are integers, I need that number in character form, not the equivalent value in ASCII; that’s why I do +48)
The problem comes when I try to print it. If I use printf(" %s", coor) it only prints the first characters and I don’t know why. I’m using %s, so it should print all the characters in the string.
When I do this:
char *p = "hello";
printf("%s",p);
It does print hello.
There are two mistakes in your code:
you are skipping the position 1 of the array. This is probably the
reason why it prints only the first element.
you need to add the end string character \0 in the end of the
string.
This should fix it :
char* coor = malloc(sizeof(char) * 6);
coor[0] = O_colum;
coor[1] = ((char)(O_row+48));
coor[2] = '-';
coor[3] = D_colum;
coor[4] = ((char)(D_row+48));
copr[5] = '\0';
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
When I run this program it prints a strange character in the terminal. Can someone tell me what that is?
int main(){
char x=1;
printf("%c\n",x);
return 0;
}
This is because you are assigning 1 (ASCII value of a character) to x . Assign '1' to x. It will give output 1.
char x = '1';
printf("%c\n",x);
Its printing character 1 from the ASCII table
(making reasonable assumptions about your platform, that you aren't on an EBCDIC platform or something)
Characters in C should usually be assigned using characters, not by using ASCII encoding. For example:
char x = 'A';
printf("%c\n", x);
Will print the character 'A' on the terminal. By giving the character the ASCII index of 1, you're assigning it the START OF HEADING character (SOH). If you were looking for 'A' or '1', that would be:
char x = 65; // x = 'A'
char y = '1'; // y = '1'
But like I said, this is very awkward and requires ASCII memorization to read, so assigning numbers is very bad practice.
You can find an ASCII table at: http://www.asciitable.com/
It depends on what you want as output. If you think that your code is giving something weird as output, you certainly want '1' as output. And for that, you should replace your statement
char x = 1;
to
char x = '1';
Thats it! Your problem would be solved!