Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to modify a string of char in C?

I have been struggling for a few hours with all sorts of C tutorials and books related to pointers but what I really want to know is if it's possible to change a char pointer once it's been created.

This is what I have tried:

char *a = "This is a string"; char *b = "new string";  a[2] = b[1]; // Causes a segment fault  *b[2] = b[1]; // This almost seems like it would work but the compiler throws an error. 

So is there any way to change the values inside the strings rather than the pointer addresses?

like image 491
Matthew Stopa Avatar asked Jun 18 '09 08:06

Matthew Stopa


People also ask

Can you modify a string in C?

The only difference is that you cannot modify string literals, whereas you can modify arrays. Functions that take a C-style string will be just as happy to accept string literals unless they modify the string (in which case your program will crash).

Can we modify string?

Strings are immutable. Once you have created a string you cannot later change that string object.

Can you convert a string to a char in C?

The c_str() and strcpy() function in C++ C++ c_str() function along with C++ String strcpy() function can be used to convert a string to char array easily. The c_str() method represents the sequence of characters in an array of string followed by a null character ('\0').

How do you change a letter in a string C?

To change a specific character in a string to another value, we refer to the index number position of the character in the string and use single quotation marks ( ' ' ). To access a character of a string in C++, we simply specify the index position of the character in the string in square brackets [] .


2 Answers

When you write a "string" in your source code, it gets written directly into the executable because that value needs to be known at compile time (there are tools available to pull software apart and find all the plain text strings in them). When you write char *a = "This is a string", the location of "This is a string" is in the executable, and the location a points to, is in the executable. The data in the executable image is read-only.

What you need to do (as the other answers have pointed out) is create that memory in a location that is not read only--on the heap, or in the stack frame. If you declare a local array, then space is made on the stack for each element of that array, and the string literal (which is stored in the executable) is copied to that space in the stack.

char a[] = "This is a string"; 

you can also copy that data manually by allocating some memory on the heap, and then using strcpy() to copy a string literal into that space.

char *a = malloc(256); strcpy(a, "This is a string"); 

Whenever you allocate space using malloc() remember to call free() when you are finished with it (read: memory leak).

Basically, you have to keep track of where your data is. Whenever you write a string in your source, that string is read only (otherwise you would be potentially changing the behavior of the executable--imagine if you wrote char *a = "hello"; and then changed a[0] to 'c'. Then somewhere else wrote printf("hello");. If you were allowed to change the first character of "hello", and your compiler only stored it once (it should), then printf("hello"); would output cello!)

like image 145
Carson Myers Avatar answered Sep 21 '22 01:09

Carson Myers


No, you cannot modify it, as the string can be stored in read-only memory. If you want to modify it, you can use an array instead e.g.

char a[] = "This is a string"; 

Or alternately, you could allocate memory using malloc e.g.

char *a = malloc(100); strcpy(a, "This is a string"); free(a); // deallocate memory once you've done 
like image 20
Jonathan Maddison Avatar answered Sep 19 '22 01:09

Jonathan Maddison