0
#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".

5
  • Or char * ip = &insert[0]; Commented Sep 1, 2014 at 13:10
  • why use char* ip = &insert; instead of just insert if you are not doing any arithmetic with the pointer? Commented Sep 1, 2014 at 13:15
  • thanks its working now. I somehow complicated it in my mind... Commented Sep 1, 2014 at 13:16
  • 1
    Best get into the habit of using strncat and strncpy whenever you can, it's safer Commented Sep 1, 2014 at 13:48
  • 1
    the line: char insert[7] = "insert "; will fail during a strcat operation as strcat expects the added string to be '\0' terminated and the string "insert " needs 8 characters to include the '\0', so the line strcat( str, ip ); will not stop adding characters until a '\0' is encountered. Commented Sep 2, 2014 at 6:07

6 Answers 6

1

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).

Sign up to request clarification or add additional context in comments.

8 Comments

Do you mean char *ip = "insert "?
No, insert is the name of the char array the OP declared.
Then it gives segmentation fault!
Interesting. How did you declare insert?
Then it should be fine. What did you use ip or insert for afterwards?
|
0

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

}

Comments

0

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

1 Comment

True but the question is about strcat
0

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').

Comments

0
$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;

Comments

0

Remove char insert[7] = "insert "; it is not required.

Change char *ip = &insert; to char *ip = "insert "

1 Comment

@Coconop: Made the changes :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.