User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,839 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,753 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 388 | Replies: 9
Reply
Join Date: Jun 2008
Posts: 7
Reputation: NiNTENDU is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
NiNTENDU NiNTENDU is offline Offline
Newbie Poster

Modify some char* characters

  #1  
Jun 29th, 2008
Hi, it's me again...
I've some problems understanding why my program hang up; I've this code:

  1. #include <stdio.h>
  2.  
  3. void foo ( char **bar )
  4. {
  5. *bar = "foobar"; /* Runs perfectly */
  6. **bar = 'b'; /* Cause a crash */
  7. }
  8.  
  9. int main()
  10. {
  11. char *bar;
  12. foo ( &bar );
  13. printf( "\n%s\n", bar );
  14.  
  15. return 0;
  16. }

Why? Help me understand, please

Greetings,

NiNTENDU.
Last edited by NiNTENDU : Jun 29th, 2008 at 10:47 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2007
Location: Mumbai(Bombay), India
Posts: 246
Reputation: stephen84s will become famous soon enough stephen84s will become famous soon enough 
Rep Power: 3
Solved Threads: 34
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Posting Whiz in Training

Re: Modify some char* characters

  #2  
Jun 29th, 2008
When you declare a "char" array as char *abc="alpha" , the string is not editable i.e. readonly,
You need to declare it as char abc[]="alpha"; , if you wish to edit by using the abc[index]=''; method,

Why this difference exists however I am not aware of !!
Last edited by stephen84s : Jun 29th, 2008 at 10:57 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
Reply With Quote  
Join Date: Aug 2006
Location: Noida, India
Posts: 158
Reputation: Luckychap is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 17
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Junior Poster

Re: Modify some char* characters

  #3  
Jun 29th, 2008
its running fine on my side.

Output: boobar.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: Modify some char* characters

  #4  
Jun 29th, 2008
Originally Posted by stephen84s View Post
When you declare a "char" array as char *abc="alpha" , the string is not editable i.e. readonly, You need to declare it as char abc[]="alpha"; , if you wish to edit by using the abc[index]=''; method,

Why this difference exists however I am not aware of !!


well, y'see... hmmm... okay, i guess i don't really know WTF you're talking about

because to declare an array of chars as either char * myString or a char myString[] is the same effect. in either case, "myString" is a pointer to the address containing the first character of that array of chars.


.
Last edited by jephthah : Jun 29th, 2008 at 5:45 pm.
Why so serious?
Reply With Quote  
Join Date: Apr 2004
Posts: 3,649
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 144
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: Modify some char* characters

  #6  
Jun 29th, 2008
Originally Posted by NiNTENDU View Post
Hi, it's me again...
I've some problems understanding why my program hang up



what do you mean, its not running? the code as you posted works. do you mean that if you have the first line in "void foo" by itself it works, but the other line (by itself) doesnt? well, that's true, then.

but both lines together in "void foo" work as you would expect.

here's what's happening:

in "main", you declare a pointer to an undefined region of memory, and you call it "bar", and you further declare that all elements starting at that location will be of type "char". in other words, "bar" is a variable of type "char pointer" that contains an address to a location of as-yet-undefined memory space that is expected to hold character elements.

then you pass the address of that pointer to the function "foo"... it expects a pointer to a pointer, so it's happy

from "foo"'s point of view, it gets a pointer to a pointer to a location of memory that is type char. in other words, a pointer to an array of chars.

you then define that location to contain the string of chars "foobar" and because it's so nice, it tacks a NULL character at the end of the string for you.

you then define the first element of the array of chars now contain the single character 'b'. the program doesn't care, but it just so happens to overwrite the 'f' that was previously there.

when you return from "foo", printf is asked to display a string of characters starting at the address pointed to by "bar"... which contains the string of characters "boobar" followed by a NULL character.

Im sure this sounds completely confusing.

sorry.

here's maybe a better way to look at it. notice the format specifier "%p" which prints the hex representation of the address held by the pointer variable.

compile this and run it. tell me if you can't compile it for some reason... and what compiler you're using

  1. #include <stdio.h>
  2.  
  3. void foo ( char **bar )
  4. {
  5. printf( "\n foo: (%p) %p %s -- %c\n", bar, *bar, *bar, **bar );
  6.  
  7. **bar = 'b';
  8.  
  9. printf( " foo: (%p) %p %s -- %c\n", bar, *bar, *bar, **bar );
  10. }
  11.  
  12. int main()
  13. {
  14. char *bar = "foobar";
  15.  
  16. printf( "\nmain: (%p) %p %s -- %c\n", &bar, bar, bar, *bar );
  17.  
  18. foo ( &bar );
  19.  
  20. printf( "\nmain: (%p) %p %s -- %c\n", &bar, bar, bar, *bar );
  21.  
  22. return 0;
  23. }



.
Last edited by jephthah : Jun 29th, 2008 at 6:30 pm.
Why so serious?
Reply With Quote  
Join Date: Apr 2004
Posts: 3,649
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 144
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Modify some char* characters

  #7  
Jun 29th, 2008
http://c-faq.com/decl/strlitinit.html

(I shoulda found that one first.)
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: Modify some char* characters

  #8  
Jun 29th, 2008
hmm. that's interesting to know...

so Stephen84s was not incorrect after all... at least as much as the answers are compiler-dependent.

now i feel like a turd for giving him a rotten cookie. someone should give him a green.

or me a red.

or both.

.
Last edited by jephthah : Jun 29th, 2008 at 6:53 pm.
Why so serious?
Reply With Quote  
Join Date: Jul 2007
Location: Mumbai(Bombay), India
Posts: 246
Reputation: stephen84s will become famous soon enough stephen84s will become famous soon enough 
Rep Power: 3
Solved Threads: 34
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Posting Whiz in Training

Re: Modify some char* characters

  #9  
Jun 30th, 2008
Originally Posted by jephthah View Post
well, y'see... hmmm... okay, i guess i don't really know WTF you're talking about

because to declare an array of chars as either char * myString or a char myString[] is the same effect. in either case, "myString" is a pointer to the address containing the first character of that array of chars.


.


I had the same belief before, but some time ago Radical Edward had informed me in a different thread that (char * and char[]) are equivalent only when used in function declarations (eg void foo(char *abc) is same as void foo(char abc[]) ).

But I guess the link from Dave clears everything.
And I tested the above in GCC 4.1.3
Last edited by stephen84s : Jun 30th, 2008 at 3:08 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
Reply With Quote  
Join Date: Jun 2008
Posts: 7
Reputation: NiNTENDU is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
NiNTENDU NiNTENDU is offline Offline
Newbie Poster

Re: Modify some char* characters

  #10  
Jun 30th, 2008
First of all, thanks and sorry at the same time because I'm too busy at work for post something.

Anyway I've done it with jephthah help but I send many thanks to all of you that find some time to respond to me!

Cheers and thumbs up

NiNTENDU.
Last edited by NiNTENDU : Jun 30th, 2008 at 4:50 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 3:06 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC