Hi, it's me again...
I've some problems understanding why my program hang up; I've this code:

#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.

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 !!

commented: false information. -1
commented: equalizer, as jephthah requisted :) +6

its running fine on my side.

Output: boobar.

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.


.

commented: Well said!! - ssharish +2

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

#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;
}

.

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.

.

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

commented: yeah, you were right... good post +4

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.