1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
lin ki
  • 39
  • 4
  • 7
    The size of a pointer (which is what `sizeof(file)` will give you) is the size of the pointer itself, not what it points to. You want to use `strlen(file)` to get the length of the string. And remember to add one when allocating, to account for the string terminator. – Some programmer dude Mar 28 '22 at 08:16
  • 1
    Change `int size= sizeof(file);` to `int size= strlen(file)+1`. The +1 is for the null char. – selbie Mar 28 '22 at 08:17
  • @selbie Why you added `1` to the `strlen` result? – Amir reza Riahi Mar 28 '22 at 08:28
  • @AmirrezaRiahi for the null terminator – Jabberwocky Mar 28 '22 at 08:29
  • 2
    `char* file` -> `char file[]` or you can't use `sizeof`. – Lundin Mar 28 '22 at 08:30
  • @Someprogrammerdude Yes, I changed my code to strlen. Thank you for this part. But there must be other problems besides that. My code still does not work in the strcpy line. – lin ki Mar 28 '22 at 08:35
  • Then you have some other problem with the code, possibly with code you don't show us. Please try to create a [mre] and [edit] your question to show it. – Some programmer dude Mar 28 '22 at 08:39
  • It seems like something went wrong outside this part of the code. It works perfectly well in the online gdb, but it stops in the strcpy line on my computer's VS code. Thank you for helping me. – lin ki Mar 28 '22 at 08:49
  • You could also use `const char file[] = “…”;` and then `sizeof(file)` would work. – Jonathan Leffler Mar 28 '22 at 09:36

3 Answers3

2

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.

Amir reza Riahi
  • 1,540
  • 2
  • 8
  • 34
  • 1
    Maybe mention the reason of the +1. – Jabberwocky Mar 28 '22 at 08:31
  • 1
    Yes, I changed my code to strlen. Thank you for this part. But there must be other problems besides that. My code still does not work in the strcpy line. – lin ki Mar 28 '22 at 08:32
  • would you provide more detail? what happen? the program crashes with segmentation fault? @linki – Amir reza Riahi Mar 28 '22 at 08:38
  • 1
    @linki You can edit your question and **add** the new version, and don't forget to include the error description. – the busybee Mar 28 '22 at 08:50
  • It seems like something went wrong outside this part of the code. It works perfectly well in the online gdb, but it stops in the strcpy line on my computer's VS code. Thank you for helping me. – lin ki Mar 28 '22 at 08:50
0

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 );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256