![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() |
Setting value to char pointer element....
It seems like this shoud be very basic, what i'm trying to do is this...
if i have str: char *str = "Hello, world!"; and i want to change the first H to an h for example, how could that be done? I've come up with this solution after lots of frustration, but it seems like there should be an easier way to accomplish this. I have tried memset, etc, always gives a seg fault... [php] /* Output: *ello, world! H*llo, world! He*lo, world! Hel*o, world! Hell*, world! Hello* world! Hello,*world! Hello, *orld! Hello, w*rld! Hello, wo*ld! Hello, wor*d! Hello, worl*! Hello, world* */ #include <stdio.h> #include <stdlib.h> char *strchr_rep(char *buf, char ch, int pos) { int i; char tmp[2] = " ", *nbuf = (char *)malloc(strlen(buf)); if(pos >= strlen(buf) || pos < 0) return NULL; for(i=0;i<pos;i++) { tmp[0] = buf[i]; strcat(nbuf, tmp); } tmp[0] = ch; if(pos == 0) strcpy(nbuf, tmp); else strcat(nbuf, tmp); for(i=pos+1;i<strlen(buf);i++) { tmp[0] = buf[i]; strcat(nbuf, tmp); } return nbuf; } int main(int argc, char* argv) { char *str = "Hello, world!", i; for(i=0;i<strlen(str);i++) printf("%s\n", strchr_rep(str, '*', i)); return EXIT_SUCCESS; } [/php]
__________________
|
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
why not use a char array instead of a char pointer? It makes it easier to manipulate single letters.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
flexibility...
__________________
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
I believe "Hello World!" is a string constant in this sense.
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Oct 4th, 2005 at 4:45 PM. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
Back to basics.
Some parts of memory allocated to a program allow read/write. Othere segments allow read-only. Constant value like char *blah="hi there" Segfaults occurs when you try to write to read only memory. therefore: #include <string.h>
int main()
{
char str[24]={0x0};
char *p=NULL;
int i=0;
strcpy(str,"Hello world");
printf("%s\n", str);
for(p=str,i=0;i<strlen(str);i++,p++)
{
*p='*'; /* place * character in str */
printf("%s\n", str);
}
return 0;
} |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You can let the compiler handle it for you, in this way:
char * aString = "Hello, World"; // will put the string in write protected (usually) memory, seg faultitis char bString [] = "Hello, World"; // will put the string in local memory, writes fine. The problem with the second method is the compiler will allot just enough to hold the initializing string, plus the terminator. Overwriting it with a longer string will lead to trouble (destroy something else, possibly with drastic consequences).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
read-only data would explain it, guess i read over that part..
__________________
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 3
![]() |
Sorry to sound like a newbie but why is the string read-only? I thought that you had to use the const modifier to make a variable read-only.
const char *string = "Hello World"; Can someone explain this to me? |
|
|
|
|
|
#9 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The const modifier only tells the compiler that it shouldn't allow you to write code that modifies that value. It's a convention, an agreement between you and the compiler that you are a screw-up, and the compiler is allowed to warn you when you do it. Declaring something const does NOT make it actually write protected (normally; one could build a system that did that, in cooperation with the compiler).
In Tempest's case, he is on an OS that has actually flagged that memory as read-only. Trying to write to it causes a system fault. Had he realized that many modern systems would do that, and realized that it WAS write protected, he could have declared it const so that the compiler would remind him if he forgot. That isn't germane. It isn't that he forgot, it's merely that he was unaware of the restrictions imposed on him by his particular implementation. A declaration like that is not, defacto, write protected. It just is in many implementations.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|