Reputation: 267
I am trying to concatenate two string literal pointers, and store them in a third string which dynamically allocate memory to fit the two string and finally add and print out the string char. I could not print the string outside the function.
//Here is the function
void join(char ptr1, char ptr2, char **ptr3) {
char tempStr[strlen(strPtr1) + strlen(strPtr2)+1];
strcpy(tempStr, ptr1);
strcat(tempStr, ptr2);
*ptr3 = (char*)malloc(strlen(tempStr));
strcpy(ptr3, tempStr);
printf("%s\n", ptr3);
}
in the main I have these variables
char *str1 = "print "; char *str2= "out"; char *str3;
join(str1,str2,&(str3));
printf("%s",str3)://segmentation fault occur here
I have asked similar question about pointer manipulation, and I still struggle with such concepts.
Upvotes: 1
Views: 146
Reputation: 58848
You have two immediate problems here:
You are not allocating space for the null terminator at the end of the resulting string:
*ptr3 = (char*)malloc(strlen(tempStr));
should be
*ptr3 = (char*)malloc(strlen(tempStr) + 1);
You are passing the wrong thing to strcpy.
strcpy(ptr3, tempStr);
should be
strcpy(*ptr3, tempStr);
I suspect the second one is what is directly causing your crash, since it will likely result in the string pointer (str3
in main
) getting overwritten with the start of the string, and also overwrite other random stack memory.
Upvotes: 0
Reputation: 153348
Compiler warnings are not fully enabled. With a good compiler and warnings well enabled, the compiler would have warned. This saves you time and avoids unnecessary posts.
void join(char *ptr1, char *ptr2, char **ptr3) {
...
// warning: passing argument 1 of 'strcpy' from incompatible pointer type
strcpy(ptr3, tempStr);
// Code should have been
strcpy(*ptr3, tempStr);
Some coding ideas: Consider the following that takes advantage of code paying the price to find the string lengths once, no need to do so again.
// As ptr1,ptr2 not changed, use const, it's less restrictive calling
// and potential for additional optimizations.
void join(const char *ptr1, const char *ptr2, char **ptr3) {
size_t len1 = strlen(l1);
size_t size2 = strlen(l2) + 1;
*ptr3 = malloc(len1 + size2);
// Check for allocation success
if (*ptr3)) {
memcpy(*ptr3, ptr1, len1);
memcpy(*ptr3 + len1, ptr2, size2);
printf("%s\n", *ptr3);
}
}
Upvotes: 0
Reputation: 206567
The problem in your posted code is that the line
*ptr3 = (char*)malloc(strlen(tempStr));
should be
*ptr3 = (char*)malloc(strlen(tempStr) + 1);
The reason for that is the same as why you needed to use +1
in the line:
char tempStr[strlen(ptr1) + strlen(ptr2)+1];
You can simplify the function by not creating tempStr
as a temporary object.
void join(char *ptr1, char *ptr2, char **ptr3) {
*ptr3 = malloc(strlen(ptr1) + strlen(ptr2) + 1);
strcpy(*ptr3, ptr1);
strcat(*ptr3, ptr2);
}
Upvotes: 1
Reputation: 117
One issue you're having is that you are declaring your ptr1 ad ptr2 as chars instead of pointers.
Any reason you can't use sprintf()?
char join(char *s1, char *s2){
char new_string[80]; // Assuming you know the max size
sprintf(new_string, "%s%s", s1, s2);
puts(new_string);
}
Upvotes: 0
Reputation: 86
If this is C, you are neglecting to account for the '\0' at the end of the string. Try this
void join(char* ptr1, char* ptr2, char** ptr3)
{
char* result = (char*)malloc(strlen(ptr1) + strlen(ptr2) + 1);
strcpy(result, ptr1);
strcat(result, ptr2);
*ptr3 = result;
}
Also why not just return the new string instead of passing in a reference to ptr3?
Upvotes: 1