-3

I have this program:

#include<stdio.h>

void copy_string(char string1[], char string2[]){
    int counter=0;
    while(string1[counter]!='\0'){
        string2[counter] = string1[counter];
        counter++;
    }
    string2[counter] = '\0';
}

int main() {
   char* myString = "Hello there!";
   char* myStringCopy;
   copy_string(myString, myStringCopy);
   printf("%s", myStringCopy);
}

My question is, why isn't it working unless I declare myStringCopy as a fixed-size variable (char myStringCopy[12];)? Shouldn't it work if I add a \0 character after the copy as I'm doing?

CarlosMorente
  • 775
  • 1
  • 7
  • 17
  • 1
    The problem is that ther is no space allocated for `myStringCopy`. Its just an unitnitzialized pointer which points to no valid memery location, therefore you can not write to it. – Osiris Aug 01 '18 at 14:35

4 Answers4

4

It can work by doing char* myStringCopy as long as you allocate memory space for it.

for example

char* myStringCopy
myStringCopy = malloc(sizeof(char) * (strlen(myString)+1))

I might be mistaken about the +1 but I think it is like this.

Miguel
  • 1,295
  • 12
  • 25
  • 1
    [Dont cast malloc return](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – H.S. Aug 01 '18 at 15:15
3

char myStringCopy[12]; tells the compiler to create an array of 12 char. When myStringCopy is passed to copy_string, this array is automatically converted to a pointer to its first element, so copy_string receives a pointer to the characters.

char *myStringCopy; tells the compiler to create a pointer to char. The compiler creates this pointer, including providing memory for it, but it does not set the value of the pointer. When this pointer is passed to copy_string, copy_string does not receive a valid value.

To make char *myStringCopy; work, you must allocate memory (which you can do with malloc). For example, you could use:

char *myStringCopy;
myStringCopy = malloc(13 * sizeof *myStringCopy);
if (myStringCopy == NULL)
{
    fprintf(stderr, "Error, the malloc did not work.\n");
    exit(EXIT_FAILURE);
}

Also, note that 12 is not enough. The string “Hello there!” contains 12 characters, but it also includes a terminating null character. You must provide space for the null character. char myStringCopy[12]; appeared to work, but copy_string was actually writing a thirteenth character beyond the array, damaging something else in your program.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • And how could I do it without knowing the string length? – CarlosMorente Aug 01 '18 at 14:43
  • 1
    @CarlosMorente: You can find the string length by counting the characters in the source string, up to and including the terminating null character. Also, if you want `copy_string` to do the allocation, then it must return the newly allocated address to the caller. – Eric Postpischil Aug 01 '18 at 14:44
2

The problem is that you don't have room for mystringCopy

You need to reserve space first:

   char* myString = "Hello there!";
   char* myStringCopy = malloc(strlen(myString) + 1);
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
1
char* myStringCopy;

This is only pointer to char*. You must first allocate memory for myStringCopy, before start copy. When you declare it like this:

char myStringCopy[12];

compiler allocate enough memory in stack.

Dnii Volna
  • 31
  • 3