简体   繁体   中英

Copying into char** from char*

I couldn't think of a better way to phrase the question title, but basically, I'm trying to copy from a sting into a string-pointer (Ideally I wouldn't use pointer-to-pointer but it's an assignment)

As an example, I want to know how to do this:

strcpy(*string_pointer, string);

But, when I try to, it gives me a segmentation fault.

If this is too vague, I'll post the whole code, though I know for sure that's the one bit that isn't working (debugger and whatnot) and I simply don't know how to do it otherwise. I've tried all "conventional" methods of copying a string, and I know the string is initialized, since if I try to do this:

EDIT: I guess it's just easier to post the code as it currently is.

int main(void) 
{
   char* string = "test";
   reverse(&string);
   return 1;
}

void reverse(char** string_pointer)
{
   int i=0, max, y=0;
   max=strlen(*string_pointer);
   char string1[max], string2[max];
   strcpy(string1, *string_pointer);
   for ( y=max-1; y>=0; --y)
   {
         string2[i] = string1[y];
         ++i;
   }
   string2[i]='\0';
   printf("%s\n",string2);  // works until here, printf was to test that
   strcpy(*string_pointer, string);
}

Above is the test program I've created to try and figure out a solution without messing around in my main program too much. All variables, declarations and everything else pertaining this particular function are exactly as they are in the main code.

I apologize for not being able to post more than this.

You need to make sure string_pointer points to a valid char * .

char array[20];
char **string_pointer = &array;
char *string = "str";

strcpy(*string_pointer, string);

The code above works because string_pointer points to the start of a array of char, which can be target of a strcpy , and there is enough room there to receive the copy.

Your problem after all is that you are trying to write to a constant string. Try changing main to:

int main(void) 
{
   char string[] = "test";
   reverse(&string);
   return 1;
}

and it should work.

With this change, the memory space for string will be allocated on the stack. The way you had it, the string was allocated by the compiler in a read-only area of the executable.

PS You don't need to make a copy of the string to reverse it, it is possible (and more efficient) just to loop over the string itself, stopping at half its length.

Is there enough room in string_pointer. The problem is that you get segfault because the process is trying to write to memory that it does not have access to.

use strncpy to be safe also, and do sanity checks.

The problem is with your last strcpy statement in your reverse function. A string literal in C is not const qualified so attempting to overwrite it won't give any warning or compile error. However, doing so leads to undefined behaviour and most likely segmentation fault. You should either dynamically allocate a char buffer in reverse and then return a pointer to it to main and free it after use, or pass a char buffer allocated in main to reverse .

The prototype of standard library function strcpy is

char *strcpy(char *dest, const char *src);

dest must be large enough to receive the copy. Also note that strcpy copies the terminating null byte from the src to dest , so your dest must accommodate for this.

void reverse(char **string_pointer, char strbuf[]);

// in main function
// C99 variable-length array, +1 for accommodating the terminating null byte

char *string = "test";
char strbuf[strlen(string) + 1]

reverse(&string, strbuf);

Or you can also do

char *reverse(char **string_pointer);

// in reverse function

char *string2 = malloc(strlen(*string_pointer) + 1);
// do stuff
return string2;

// in main

char *string = "test";
char *reversed_string = reverse(&string);

// do stuff

free(reversed_string);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM