How can I add '.' to the char Array := "Hello World" in C, so I get a char Array: "Hello World." The Question seems simple but I'm struggling.

Tried the following:

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

But it does not work. It shows me the error: "passing argument 2 of ‘strcat’ makes pointer from integer without a cast" I know that in C a char can be cast as int aswell. Do I have to convert the tmp to an char array aswell or is there a better solution?

4

5 Answers

strcat has the declaration:

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

It expects 2 strings. While this compiles:

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

It will cause bad memory issues because strcat is looking for a null terminated cstring. You can do this:

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

Live example.

If you really want to append a char you will need to make your own function. Something like this:

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

Of course you may also want to check your string size etc to make it memory safe.

9

The error is due the fact that you are passing a wrong to strcat(). Look at strcat()'s 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 commented by OP:

That was just a example with Hello World to describe the Problem. It must be empty as first in my real program. Program will fill it later. The problem just contains to add a char/int to an char Array

In that case, snprintf() can handle it easily to "append" integer types to a char buffer too. The advantage of snprintf() is that it's more flexible to concatenate various types of data into a char buffer.

For example to concatenate a string, char and an int:

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

In C/C++ a string is an array of char terminated with a NULL byte ('\0');

  1. Your string str has not been initialized.
  2. You must concatenate strings and you are trying to concatenate a single char (without the null byte so it's not a string) to 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 assign a string literal to an array only during its declaration.
For example the following code is not permitted:

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

and should be replaced with

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

I think you've forgotten initialize your string "str": You need initialize the string before using strcat. And also you need that tmp were a string, not a single char. Try change 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] = "."; 

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); // 
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy