Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 4th, 2005, 4:11 PM   #1
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
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]
__________________

tempest is offline   Reply With Quote
Old Oct 4th, 2005, 4:28 PM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
why not use a char array instead of a char pointer? It makes it easier to manipulate single letters.
OpenLoop is offline   Reply With Quote
Old Oct 4th, 2005, 4:30 PM   #3
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
flexibility...
__________________

tempest is offline   Reply With Quote
Old Oct 4th, 2005, 4:32 PM   #4
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
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.
stevengs is offline   Reply With Quote
Old Oct 4th, 2005, 4:40 PM   #5
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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"
live in read only memory.
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;
}
jim mcnamara is offline   Reply With Quote
Old Oct 4th, 2005, 4:46 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Oct 4th, 2005, 4:46 PM   #7
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
read-only data would explain it, guess i read over that part..
__________________

tempest is offline   Reply With Quote
Old Oct 5th, 2005, 1:40 PM   #8
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 3 aznluvsmc is on a distinguished road
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?
aznluvsmc is offline   Reply With Quote
Old Oct 5th, 2005, 2:02 PM   #9
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by aznluvsmc
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?
"Hello World" is contained in the read-only part of the program in memory. It doesn't matter you use const or not, since you will only make the pointer to the string constant, but not the string itself.
Polyphemus_ is offline   Reply With Quote
Old Oct 5th, 2005, 2:03 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC