Reputation: 193
Could anyone tell me what am i doing wrong over here? Why does my program segfault ?
I am trying to insert a third string between string1
and string2
.
#include <stdio.h>
int main (void)
{
char *string1 = "HELLO";
char *string2 = "WORLD";
char *stringX = "++++";
char *string3;
printf ("%s,%s\n",string1,string2);
sprintf(string3,"%s%s%s",string1,stringX,string2);
printf ("NewVar: %s",string3);
}
Why doesn't sprintf
store the resultant value at the memory address pointed by string3
? It works when i declare string3
as an ordinary array but not when its a pointer to char
array.
I thought string3
wasnt pointing to any memory location, but it does seem to when i do printf("%p",string3);
Output:
# ./concat
HELLO,WORLD,0x40042
Upvotes: 19
Views: 68858
Reputation: 126
suppose you define i
as int i
; in this level you tell that I will store integer number
but still there is not a meaningful number in i
variable.
like this when you define char *string3
you tell that string3
will store char pointer but still there is not meaningful address. so you must allocate memory to this variable
string3 = malloc(strlen(string1)+strlen(stringX)+strlen(string2)+1);
Upvotes: 0
Reputation: 39414
Imagine you have a pile of cash that you want to put in a briefcase. What do you need? You have to measure the size of the cash to know how big a briefcase to use, and you need a handle to conveniently carry the cash around.
The cash is your strings. The briefcase is memory space. The briefcase handle is the pointer.
strlen(string1) + strlen(string2) + strlen(stringX)
.
Call this "total".malloc(total+1)
string3
Cobbling all that together...
char *string3 = malloc(strlen(string1)+strlen(stringX)+strlen(string2)+1);
sprintf(string3, "%s%s%s", string1, stringX, string2);
So what was wrong with the first attempt? You had no briefcase. You have cash, and you have a handle, but no briefcase in the middle. It appeared to work, in a random kind of way, because the compiler gave you a dirty dumpster to hold the cash. Sometimes the dumpster has room, sometimes it doesn't. When it doesn't, we call that "segmentation fault".
Whenever you have data, you have to allocate space for that data. The compiler allocates space for your constant strings, like "HELLO"
. But you have to allocate space for strings built at run-time.
Upvotes: 42
Reputation: 12263
sprintf
does store the value there. The problem is that the pointer string3 has uninitialized value, so you're just overwriting random memory.
One option you have is to use static string buffer:
char string3[20];
snprintf(string3, sizeof(string3), "Hello!");
Or, you can use asprintf
on GNU libc-based systems to allocate proper space automatically:
char * string3;
asprintf(&string3, "Hello!");
// ... after use
free(string3); // free the allocated memory
Upvotes: 10
Reputation: 42175
sprintf
does not allocate memory for the string it writes. You have to provide a valid string for it to write into but are currently passing it an uninitialised pointer.
The easiest fix is to change
char *string3;
sprintf(string3,"%s%s%s",string1,stringX,string2);
to
char string3[200];
sprintf(string3,"%s%s%s",string1,stringX,string2);
You may want to guard against buffer overflows in this case by using snprintf
instead
char string3[200];
snprintf(string3,sizeof(string3),"%s%s%s",string1,stringX,string2);
Alternatively, you could also cope with larger lengths of source string by determining the size of string3
at runtime, taking care to free
this memory when you have finished with it.
char* string3 = malloc(strlen(string1) + strlen(stringX) + strlen(string2) + 1);
if (string3 == NULL) {
// handle out of memory
}
sprintf(string3,"%s%s%s",string1,stringX,string2);
...
free(string3);
Upvotes: 9
Reputation: 31204
you need to allocate space for string3
either with malloc
if you need it to be on the heap, or declare it as a character array if you don't.
Upvotes: 5