Sehui
Sehui

Reputation: 11

Pass string literal as char* via argument

I made a function which change string, see the following code.

void Test(char* str, char c) {
    str[1] = c;
}

int main(){
    Test("Hi", '2');
}

I notice it made some run time error. I know how to prevent the error.

char buff[3] = "Hi";
Test(buff,'2');

but I don't know why the first example made run time error. I guess, if I pass string directly, it becomes const char. Does anyone explain what happened exactly?

ps. what if I use char* str = "hi", then pass it into the argument?

char* buff = "Hi";
Test(buff,'2');

like this. Can I modify buff?

Upvotes: 1

Views: 3743

Answers (5)

barak manos
barak manos

Reputation: 30146

Runtime Error:

char* buff = "Hi"; // buff points to an address in the code-section, which is a Read-Only section
buff[1] = 'x';     // Illegal memory access violation

Compilation Error:

const char* buff = "Hi"; // This is a correct declaration, which will prevent the runtime error above
buff[1] = 'x';           // The compiler will not allow this

All Good:

char buff[] = "Hi"; // buff points to an address in the stack or the data-section, which are both Read-Write sections
buff[1] = 'x';      // Works OK

Notes:

  1. In all cases, a string "Hi" is placed in the code-section of the program.

  2. In the last example, the contents of that string are copied into the buff array.

  3. In the last example, the buff array is located in the stack if buff is a non-static local variable, and in the data-section of the program otherwise.

Upvotes: 0

Siva Krishna Aleti
Siva Krishna Aleti

Reputation: 603

When you don't explicitly allocate memory for strings, compiler stores them in read-only memory. So, any modification to such strings result in run time error.

Test("Hi", '2');  

Here in the above case "Hi" string is stored in read-only memory.

 char *buff = "Hi";  
 Test(buff,'2');

Here also "Hi" is stored in the read-only memory and the starting address is returned to buff character pointer, which is same as above. You can overcome such errors by allocating memory for the string and then pass that reference. Like

 char buff[3] = "Hi";  
 Test(buff,'2');

or

char *buff = (char *)malloc(SIZE);  
strcpy(buff, "Hi");
Test(buff,'2');

Please refer to this link http://www.geeksforgeeks.org/memory-layout-of-c-program/

Upvotes: 1

Kiril Kirov
Kiril Kirov

Reputation: 38173

Because "Hi" is string literal and it's not allowed to be modified, they are read-only (the type of string literal is const char[n]).

Modifying it is undefined behavior.


Regarding your edit: char* str = "hi" is invalid, it should be const char* str = "hi". Which is pointer to const char. Again, modifying it is disallowed.

Upvotes: 3

Barmar
Barmar

Reputation: 782285

Literal strings are not modifiable. When I compile your code with GCC I get the warning:

testptr.cpp:6: warning: deprecated conversion from string constant to 'char*'

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122458

Often string constants are in read-only memory, causing a runtime error when you attempt to modify it.

In your second example, you put the string into a buffer on the stack, so it can be updated without error.

Upvotes: 0

Related Questions