Reputation: 19
#include <stdio.h>
#include <string.h>
int main ()
{
char str[800];
char insert[7] = "insert ";
char* ip = &insert;
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str, ip);
// or? strcat (str, insert);
strcat (str,"concatenated.");
printf("%s\n", str);
}
So im having trouble adding the array 'insert' to the array 'str'. Im not sure if I have to use a pointer for insert or not. Ive tried a few different ways but cant seem to get it to print "these strings are insert concatenated".
Upvotes: 0
Views: 2151
Reputation: 1977
Try this initialization instead
char insert[] = "insert ";
char *ip = insert;
This way you make sure the corresponding string is null-terminated and you don't have incompatible-pointer-type problem.
Note : Ani has kindly pointed out that the initialization char insert[7] = "insert ";
is not portable (which is logical since the size should be 8 when the null char
is included). Apparently it causes segfault on some compiler(s).
Upvotes: 1
Reputation: 3870
When you do-
char insert[7] = "insert ";
char* ip = &insert;
Here you are assigning address of address of the char array. Not address of Char array. Due to that if you compile your program you will get warning as-
edit.c: In function ‘main’:
edit.c:9:15: warning: initialization from incompatible pointer type [enabled by default]
So try this change-
char insert[7] = "insert ";
char *ip = insert;
only assign the address of the char array. Not the address of address of char array
or
char *ip = "insert ";
This also will work for you!
Else as you mentioned you can follow this way also-
int main ()
{
char str[800];
char insert[7] = "insert ";
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str, insert);
strcat (str,"concatenated.");
printf("%s\n", str);
}
Upvotes: 0
Reputation: 6057
Remove char insert[7] = "insert ";
it is not required.
Change char *ip = &insert;
to char *ip = "insert "
Upvotes: 0
Reputation: 3496
$man strcat
char *strcat(char *dest, const char *src);
You can either:
strcat (str, ip);
Or
strcat(str, insert);
Provided you correctly initialized your string:
char insert[] = "insert ";
char * ip = insert;
Upvotes: 0
Reputation: 1791
There is basically one problem that makes the code not to work.
The problem is that in your code you assign the following way:
char* ip = &insert;
when it really should be:
char* ip = insert;
The variable insert
is not an array. It's a pointer to a spot allocated for the program. Basically - it's just a pointer to the first element in the array.
Means:
insert[0] = 'a';
is the same as:
*insert = 'a';
So the address of insert is a pointer TO THE POINTER that points to the array.
You could either just put a *
operator TWICE (**
- something like 'the content of the content of...[some variable which is usually a pointer]) before the ip
in the strcat
use OR you could just type strcat(str, insert);
because in this program there is no need for the variable ip
at all.
One more mistake is not saving room for the NULL terminator \0
that announces the end of a string.
So either you type char insert[] = "insert ";
which determines the size of the array according to the direct assignment OR you could do char insert[8] = "insert ";
which is basically the size of that string (it needs a NULL terminator so it's 'i', 'n', 's', 'e', 'r', 't', ' ' and '\0').
Upvotes: 0
Reputation: 48
When you use strcpy, according to manual page, it will copy the str starting at the adress you provide him in the first argument.
So instead of doing
strcpy(str, "strings");
try giving him the adress at which your string finish
strcpy(str + strlen(str), "strings ");
and so on
Upvotes: 0