•
•
•
•
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
![]() |
•
•
Join Date: Jun 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
Hi, it's me again...
I've some problems understanding why my program hang up; I've this code:
Why? Help me understand, please
Greetings,
NiNTENDU.
I've some problems understanding why my program hang up; I've this code:
C Syntax (Toggle Plain Text)
#include <stdio.h> void foo ( char **bar ) { *bar = "foobar"; /* Runs perfectly */ **bar = 'b'; /* Cause a crash */ } int main() { char *bar; foo ( &bar ); printf( "\n%s\n", bar ); return 0; }
Why? Help me understand, please

Greetings,
NiNTENDU.
Last edited by NiNTENDU : Jun 29th, 2008 at 10:47 am.
•
•
Join Date: Jul 2007
Location: Mumbai(Bombay), India
Posts: 246
Reputation:
Rep Power: 3
Solved Threads: 34
When you declare a "char" array as
You need to declare it as
Why this difference exists however I am not aware of !!
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."
•
•
Join Date: Aug 2006
Location: Noida, India
Posts: 158
Reputation:
Rep Power: 3
Solved Threads: 17
•
•
•
•
When you declare a "char" array aschar *abc="alpha", the string is not editable i.e. readonly, You need to declare it aschar abc[]="alpha";, if you wish to edit by using theabc[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?
•
•
•
•
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
c Syntax (Toggle Plain Text)
#include <stdio.h> void foo ( char **bar ) { printf( "\n foo: (%p) %p %s -- %c\n", bar, *bar, *bar, **bar ); **bar = 'b'; printf( " foo: (%p) %p %s -- %c\n", bar, *bar, *bar, **bar ); } int main() { char *bar = "foobar"; printf( "\nmain: (%p) %p %s -- %c\n", &bar, bar, bar, *bar ); foo ( &bar ); printf( "\nmain: (%p) %p %s -- %c\n", &bar, bar, bar, *bar ); return 0; }
.
Last edited by jephthah : Jun 29th, 2008 at 6:30 pm.
Why so serious?
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.
.
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?
•
•
Join Date: Jul 2007
Location: Mumbai(Bombay), India
Posts: 246
Reputation:
Rep Power: 3
Solved Threads: 34
•
•
•
•
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 eitherchar * myStringor achar 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."
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Send data on a serial port (C++)
- Counting specific characters in a string (C)
- i need help to convert characters (C)
- chars and floats (C)
Other Threads in the C Forum
- Previous Thread: Sentence separation..
- Next Thread: can u?



Linear Mode