Home > Mobile >  Question about strcpy using char pointer in C
Question about strcpy using char pointer in C

Time:03-29

I'm doing some programming with C, and I have a minor problem using strcpy.

char* file="It has something inside"
int size= sizeof(file);
char* file_save = malloc(sizeof(char)*size);
strcpy(file_save,file);

My code stopped working in the last line. What can be the problem here?

It seems like something went wrong outside this part of the code. It works perfectly well if I change sizeof into strlen in the online gdb, but it still stops in the strcpy line on my computer's VS code. Thank you for helping me.

CodePudding user response:

As comments suggest, the size is the size of variable char pointer which is probably 8 if you are working on a 64-bit machine. So you need the size of string the file is pointing to. You can get that like below:

int size = strlen(file)   1;

The strlen returns the number of bytes this string has. Every string is finished by a null terminator \0. While you are writing in a file it doesn't matter which bytes are for the string and which is the null terminator so you need to count both.

CodePudding user response:

The variable file has the pointer type char *

char* file="It has something inside";

It would be even better to declare the pointer with the qualifier const because you may not change the string literal pointed to by the pointer

const char *file = "It has something inside";

Its size returned by the operator sizeof

int size= sizeof(file);

does not depend on what string the pointer points to and usually is equal to 4 or 8 bytes.

As you are going to copy the pointed string literal you need to determine its length. To do this you can use standard string function strlen. It returns the number of characters in a string until the terminating zero character '\0' is encountered.

So you need to write

size_t size = strlen( file );

Now you need to allocate memory for size characters plus one for the terminating zero character '\0' of the string literal.

char* file_save = malloc( size   1 );

So all is ready to copy the source string provided that the memory was allocated successfully.

strcpy( file_save, file );

CodePudding user response:

What can be the problem here?

A pointer is not an array and not a string. An array is not a pointer. A string is not a pointer.

Below, file is a pointer.

char* file = "It has something inside";
int size = sizeof(file);

sizeof(file) is the size of a pointer, not the size of array nor the size of a string.

Later code needs to use the size of a string, not the size of a pointer.


To find the size of a string, use the pointer file, which points to a string and then call strlen().

// `length` is the string length: characters up to, but not including the null character.
size_t length = strlen(file);

// `size` is the string size: characters up to, and including the null character.
size_t size = length   1;

Use size to determine allocation needs for a copy of the string.

  • Related