Error with strcat in C - c

I'm trying to use strcat function to manipulate the strings in linux but it throws me an error. I have no idea why.. Here is the code
int YX = 1234;
char YX2[100];
sprintf(YX2, "%d", YX); //converting int to str
char str[5] = {1,2,3,4,5};
strcat(YX2, str[4]);
//snprintf(YX2, sizeof(YX2), "%s%s", str[4]) <- this one won't work neither
The system just crash after this...

Both arguments of strcat should be pointer to null terminated char strings (char * type). str[4] is of char type.

You are trying to use a char as if it is a char *. The pointer str is "12345" while str[4] is the character '5'.
strcat receives two arguments (char *, char *)
while you are sending (char *, char).
I am not 100 percent what you are trying to do so I will give you two answers.
strcat(YX2, str);
This will give you the string "123412345"
Or:
YX2[4] = str[4]; or YX2[strlen(YX2)] = str[4];
Both of these will add the character to the end.

Change strcat to strcat( YX2, str + 4 ). When you put str[4] as parameter 2, its value is '5' which is converted to pointer and copy is started from address 0x00035 in memory... which is not what you want;
Change definition to char str[6] = {'1','2','3','4','5', 0}, char str[6] = "12345" or char str[] = "12345". In your case there is no 0 at end of string and strcat copies until 0.

Related

How do I access individual char's in an array of char pointers? [duplicate]

I have a string,
char* str = "HELLO"
If I wanted to get just the E from that how would I do that?
char* str = "HELLO";
char c = str[1];
Keep in mind that arrays and strings in C begin indexing at 0 rather than 1, so "H" is str[0], "E" is str[1], the first "L" is str[2] and so on.
You would do:
char c = str[1];
Or even:
char c = "Hello"[1];
edit: updated to find the "E".
Array notation and pointer arithmetic can be used interchangeably in C/C++ (this is not true for ALL the cases but by the time you get there, you will find the cases yourself). So although str is a pointer, you can use it as if it were an array like so:
char char_E = str[1];
char char_L1 = str[2];
char char_O = str[4];
...and so on. What you could also do is "add" 1 to the value of the pointer to a character str which will then point to the second character in the string. Then you can simply do:
str = str + 1; // makes it point to 'E' now
char myChar = *str;
I hope this helps.

Extraction one sign from string [duplicate]

I have a string,
char* str = "HELLO"
If I wanted to get just the E from that how would I do that?
char* str = "HELLO";
char c = str[1];
Keep in mind that arrays and strings in C begin indexing at 0 rather than 1, so "H" is str[0], "E" is str[1], the first "L" is str[2] and so on.
You would do:
char c = str[1];
Or even:
char c = "Hello"[1];
edit: updated to find the "E".
Array notation and pointer arithmetic can be used interchangeably in C/C++ (this is not true for ALL the cases but by the time you get there, you will find the cases yourself). So although str is a pointer, you can use it as if it were an array like so:
char char_E = str[1];
char char_L1 = str[2];
char char_O = str[4];
...and so on. What you could also do is "add" 1 to the value of the pointer to a character str which will then point to the second character in the string. Then you can simply do:
str = str + 1; // makes it point to 'E' now
char myChar = *str;
I hope this helps.

Should I manually allocate string ending char '\0' in c?

I'm new to c, and confused by string ending char '\0', should I allocate it?
for example, I want to store a string with a max length of 6;
If I use array, should I use char str[6] or char str[7]?
char as[3] = "abc";
printf("%s\n", as);
//seems no problem
If I use char pointer, should I use char *str = malloc(6) or char *str = malloc(7)?
For an array that is pre-initialized, you don't need to write a number in the brackets. You can just write
char str[] = "this is my string";
And the compiler will automatically calculate the number of bytes needed.
But for malloc, you must add 1. Ex:
char *strdup(const char *str)
{
char *ret = malloc(strlen(str) + 1);
strcpy(ret, str);
return ret;
}
You should be using string length + 1. In your case you must use 7 while declaring the char array.
The example you provided would have worked because of the undefined behaviour shown by printf().
In addition to stackptr's answer:
If you are planning to overwrite your array:
char str[30] = "abc";
...
strcpy(str, "Hello world"); /* This will overwrite the content of "str" */
... the length of the array must be the maximum length of the string plus 1.
In the example above you may write strings of up to 29 characters length to the array.
Note that the following definition:
char str[] = "abc";
... implicitly creates an array of 4 characters length so you are limit to 3 characters.

How can I get the nth character of a string?

I have a string,
char* str = "HELLO"
If I wanted to get just the E from that how would I do that?
char* str = "HELLO";
char c = str[1];
Keep in mind that arrays and strings in C begin indexing at 0 rather than 1, so "H" is str[0], "E" is str[1], the first "L" is str[2] and so on.
You would do:
char c = str[1];
Or even:
char c = "Hello"[1];
edit: updated to find the "E".
Array notation and pointer arithmetic can be used interchangeably in C/C++ (this is not true for ALL the cases but by the time you get there, you will find the cases yourself). So although str is a pointer, you can use it as if it were an array like so:
char char_E = str[1];
char char_L1 = str[2];
char char_O = str[4];
...and so on. What you could also do is "add" 1 to the value of the pointer to a character str which will then point to the second character in the string. Then you can simply do:
str = str + 1; // makes it point to 'E' now
char myChar = *str;
I hope this helps.

Some 'predict the outputs' with pointers

Help me in solving 2 questions on pointers:
1)Please tell me why do I get 'segmentation fault' when I run following snippet
main()
{
char *str1 = "united";
char *str2 ="front";
char *str3;
str3 = strcat(str1,str2);
printf("\n%s",str3);
}
2)Why don't I get output in following code:
main()
{
char str[10] = {0,0,0,0,0,0,0,0,0,0};
char *s;
int i;
s = str;
for(i=0 ; i<=9;i++)
{
if(*s)
printf("%c",*s);
s++;
}
}
Thank u.
You should review how strcat works. It will attempt to rewrite the memory at the end of your str1 pointer, and then return the destination pointer back to you. The compiler only allocated enough memory in str1 to hold "united\0" (7 chars), which you are trying to fill with "unitedfront\0" (12 chars). str1 is pointing to only 7 allocated characters, so there's no room for the concatenation to take place.
*s will dereference s, effectively giving you the first character in the array. That's 0, which will evaluate to false.
1) compiles to something like:
const char _str1[7] = "united";
const char _str2[6] ="front";
char *str1 = _str1;
char *str2 = _str2;
strcat(str1,str2);
str3 = str1;
str1 points to a buffer which is exactly 7 bytes long and is filled with 6 characters. The strcat put another 5 bytes into that buffer. 7 bytes cannot hold 11 characters.
The C there is no magic! If you do not explicitly allocate space for something, no one else does it either.....
2) isn't going to print anything. It steps through an array, every element of which is 0. It then tests if the current item (*s) is not 0 (if(*s)) , and if so, prints that item as a character. However, since the item always is 0, it always fails to test.
for question 2, think about what the following line does:
if(*s)

Resources