How to add char / int to a char array in C?

How to add '.' to char Array: = "Hello World" in C, so I get an array of char: "Hello World". The question seems simple, but I'm struggling.

Tried the following:

char str[1024]; char tmp = '.'; strcat(str, tmp); 

But that will not work. It shows me an error: "Passing argument 2 of" strcat makes the pointer a whole without casting "I know that in C a char can be distinguished as int. Do I need to convert tmp to char array or is there a better solution?

+6
source share
5 answers

strcat has the declaration:

 char *strcat(char *dest, const char *src) 

He expects 2 lines. While this is compiling:

 char str[1024] = "Hello World"; char tmp = '.'; strcat(str, tmp); 

This will cause bad memory problems, since strcat looking for a null terminated cstring. You can do it:

 char str[1024] = "Hello World"; char tmp[2] = "."; strcat(str, tmp); 

Real-time example.

If you really want to add char, you will need to create your own function. Something like that:

 void append(char* s, char c) { int len = strlen(s); s[len] = c; s[len+1] = '\0'; } append(str, tmp) 

Of course, you can also check your string size, etc., to make memory safe.

+11
source

The error is due to the fact that you are mistaken in strcat() . Check out the strcat() prototype:

  char *strcat(char *dest, const char *src); 

But you pass char as the second argument, which is obviously wrong.

Use snprintf() instead.

 char str[1024] = "Hello World"; char tmp = '.'; size_t len = strlen(str); snprintf(str + len, sizeof str - len, "%c", tmp); 

As the OP commented:

This was just an example from Hello World to describe the problem. It should be empty, like the first in my real program. The program will fill it out later. The problem is simply to add char / int to a char Array

In this case, snprintf() can easily handle the "adding" of integer types to the char buffer. The advantage of snprintf() is that it is more flexible to combine different data types into a char buffer.

For example, to combine a string, char and int:

 char str[1024]; ch tmp = '.'; int i = 5; // Fill str here snprintf(str + len, sizeof str - len, "%c%d", str, tmp, i); 
+1
source

In C / C ++, a string is an array of char ending in a NULL byte ( '\0' );

  • String str is not initialized.
  • You have to concatenate strings, and you are trying to concatenate one char (without a null byte, not a string) into a string.

The code should look like this:

 char str[1024] = "Hello World"; //this will add all characters and a NULL byte to the array char tmp[2] = "."; //this is a string with the dot strcat(str, tmp); //here you concatenate the two strings 

Note that you can only assign a string literal to an array when it is declared.
For example, the following code is not allowed:

 char str[1024]; str = "Hello World"; //FORBIDDEN 

and it should be replaced by

 char str[1024]; strcpy(str, "Hello World"); //here you copy "Hello World" inside the src array 
+1
source

I think you forgot to initialize the string "str": you need to initialize the string before using strcat. And also you need tmp to be a string, not a single char. Try changing this:

 char str[1024]; // Only declares size char tmp = '.'; 

for

 char str[1024] = "Hello World"; //Now you have "Hello World" in str char tmp[2] = "."; 
0
source

Suggest replacing this:

 char str[1024]; char tmp = '.'; strcat(str, tmp); 

with this:

 char str[1024] = {'\0'}; // set array to initial all NUL bytes char tmp[] = "."; // create a string for the call to strcat() strcat(str, tmp); // 
0
source

All Articles