Reputation: 1484
I'm running into a bus error trying to do a manual concatenation of two strings without library functions (school assignment). Code is as follows:
#include <stdio.h>
char *strcattest(char string1[ ], char string2[ ]);
int main() {
printf("*****STRING CONCATENATION*****\n");
printf("Hello plus Hello: %s\n", strcattest("Hello","Hello"));
printf("Hello plus Hellp: %s\n", strcattest("Hello","Hellp"));
printf("Helo plus Hello: %s\n", strcattest("Helo","Hello"));
printf("Hello plus Helo: %s\n", strcattest("Hello","Helo"));
return(0);
}
char *strcattest(char string1[ ], char string2[ ]) {
int counter = 0;
while(string1[counter]!='\0') {
counter++;
}
int str2counter = 0;
while(string2[str2counter]!='\0') {
string1[counter] = string2[str2counter];
str2counter++;
counter++;
}
string1[counter]='\0';
return string1;
}
I've done some research and I think I understand conceptually why this does not work. (I'm calling trying to modify a variable -- string1 -- that is based on a static string.) However, I'm not sure how to rectify this so that it behaves itself. I tried introducing a local variable in the strcattest function that would behave as a copy of string1 but that got the compiler yelling at me for returning a local variable from a function.
Any help is much appreciated. Thanks!
Upvotes: 0
Views: 452
Reputation: 416
The "Hello" argument you pass to the function actually resides in the .data section of your program. You are not allowed to modify this section. Any attempts to do so can give you errors (bus error in your case).
You are only allowed to manipulate that part of your memory which is in the stack (local variables and arrays) or heap (memory allocated using [mc]alloc() functions).
Upvotes: 1
Reputation: 179552
You are correct in that you are trying to modify a static (read-only) string; doing this is what the C standard terms "undefined behaviour" (which in your case manifests as a crash).
To fix this, you need to allocate a character array that you can modify. You can do this either statically (on the stack) or dynamically (using malloc
).
Statically, to initialize a character array with specific content, you can do
char mystring[1024] = "Hello!";
This will initialize the character array with the string contents, copying them (note that the similar-looking char *mystring = "Hello!";
is not an array initialization, and will instead make mystring
point to the static string). You can place that declaration either at global scope (outside of any function, not recommended unless you have a specific reason to use a global), or in your main
method (then you can pass it to strcattest
).
Dynamically, the declaration looks like
char *mystring = malloc(1024);
to get an uninitialized buffer big enough to hold 1024 characters (counting the null terminator). If you malloc
something, though, you have to free
it later, or you'll end up with a memory leak. To put something in it, do
strcpy(mystring, "Hello!");
Note that mystring = "Hello!"
in this case won't work: why not?
Upvotes: 1
Reputation: 206566
The problem:
printf("Hello plus Hello: %s\n", strcattest("Hello","Hello"));
The strings "Hello"
here are string literals and are placed in implementation defined (read only) memory. You cannot modify them.Modifying them would lead to Undefined Behavior. So you cannot append a string literal to another simply, because they are not meant to be modifyable.
The Solution:
If you want to get an string sppended with the two string literals then you need to do the following:
Upvotes: 1
Reputation: 409346
You try concatenate a string literal to another string literal. Those string literals are, besides being too short, read only.
Upvotes: 1