Difference between char *str="STRING" and char str[] = "STRING"?

34,026

Solution 1

The two declarations are not the same.

char ptr[] = "string"; declares a char array of size 7 and initializes it with the characters
s ,t,r,i,n,g and \0. You are allowed to modify the contents of this array.

char *ptr = "string"; declares ptr as a char pointer and initializes it with address of string literal "string" which is read-only. Modifying a string literal is an undefined behavior. What you saw(seg fault) is one manifestation of the undefined behavior.

Solution 2

Strictly speaking a declaration of char *ptr only guarantees you a pointer to the character type. It is not unusual for the string to form part of the code segment of the compiled application which would be set read-only by some operating systems. The problem lies in the fact that you are making an assumption about the nature of the pre-defined string (that it is writeable) when, in fact, you never explicitly created memory for that string yourself. It is possible that some implementations of compiler and operating system will allow you to do what you've attempted to do.

On the other hand the declaration of char test[], by definition, actually allocates readable-and-writeable memory for the entire array of characters on the stack in this case.

Solution 3

As far as I remember

char ptr[] = "string";

creates a copy of "string" on the stack, so this one is mutable.

The form

char *ptr = "string";

is just backwards compatibility for

const char *ptr = "string";

and you are not allowed (in terms of undefined behavior) to modify it's content. The compiler may place such strings in a read only section of memory.

Solution 4

char *test = "string test"; is wrong, it should have been const char*. This code compiles just because of backward comptability reasons. The memory pointed by const char* is a read-only memory and whenever you try to write to it, it will invoke undefined behavior. On the other hand char test[] = "string test" creates a writable character array on stack. This like any other regualr local variable to which you can write.

Share:
34,026
Gui13
Author by

Gui13

Updated on December 20, 2020

Comments

  • Gui13
    Gui13 over 3 years

    While coding a simple function to remove a particular character from a string, I fell on this strange issue:

    void str_remove_chars( char *str, char to_remove)
    {
        if(str && to_remove)
        {
           char *ptr = str;
           char *cur = str;
           while(*ptr != '\0')
           {
               if(*ptr != to_remove)
               {
                   if(ptr != cur)
                   {
                       cur[0] = ptr[0];
                   }
                   cur++;
               }
               ptr++;
           }
           cur[0] = '\0';
        }
    }
    int main()
    {
        setbuf(stdout, NULL);
        {
            char test[] = "string test"; // stack allocation?
            printf("Test: %s\n", test);
            str_remove_chars(test, ' '); // works
            printf("After: %s\n",test);
        }
        {
            char *test = "string test";  // non-writable?
            printf("Test: %s\n", test);
            str_remove_chars(test, ' '); // crash!!
            printf("After: %s\n",test);
        }
    
        return 0;
    }
    

    What I don't get is why the second test fails? To me it looks like the first notation char *ptr = "string"; is equivalent to this one: char ptr[] = "string";.

    Isn't it the case?