Javier wrote:
Hi
I'm trying to learn how to use pointers with a little example.
I did:
void main(int argc, char* argv[])
{
char algo[] = "Algo mas";
char * p = algo;
char otra[256] = " ";
otra = *p;
printf("%s", otra);
}
I tried to assign a value to algo, then tried to assign algo to a
pointer p and then tried to assign the value pointed by p to otra.
Program compiles ok
Which compiler?
I am just asking, because if your compiler indeed compiled the above
program without an error, it has a serious bug.
*** You can't assign to an array ***
otra is an array, thus the assignment
otra = *p;
is illegal!
but when I try to run it, hangs up :(
What did you do:
First the pgm creates an array and initializes it with the characters
'A' 'l' 'g' 'o' ' ' 'm' 'a' 's' '\0'
char algo[] = "Algo mas";
algo
+---+---+---+---+---+---+---+---+---+
| A | l | g | o | | m | a | s | \0|
+---+---+---+---+---+---+---+---+---+
Then a pointer is created, which points to the beginning of algo.
This works, because in that context the name of the array alon ('algo')
depractes to the address of the first array element.
char * p = algo;
algo
+---+---+---+---+---+---+---+---+---+
| A | l | g | o | | m | a | s | \0|
+---+---+---+---+---+---+---+---+---+
^
|
+------------+
|
p |
+-------+ |
| o----------+
+-------+
Next another array is created, this time it has a length of 256 characters
and is initialized with the characters ' ' '\0'. Note: since this are only
2 characters, this also means that the remaining 254 characters are not
touched. They contain garbage.
char otra[256] = " ";
algo
+---+---+---+---+---+---+---+---+---+
| A | l | g | o | | m | a | s | \0|
+---+---+---+---+---+---+---+---+---+
^
|
+------------+
|
p |
+-------+ |
| o----------+
+-------+
otra
+---+---+---+-- ... ---+---+---+
| | \0| xx| x xx| xx| xx|
+---+---+---+-- ... ---+---+---+
Then comes the illegal statement:
otra = *p;
I don't know what your compiler did with that, lets just say
you wrote:
otra[0] = *p;
Then the following would happen:
left side of assignment: Thats easy, just look up 'otra' and identify
the element with index 0. This element will get a new value.
right side of assignment: Look up 'p'. There is an array emitting from
it. The '*' in *p tells the program to follow that arrow und use whatever
is at the end of that arrow. So the Arrow is pointing to the character A.
Thus otra[0] will get this as new value:
algo
+---+---+---+---+---+---+---+---+---+
| A | l | g | o | | m | a | s | \0|
+---+---+---+---+---+---+---+---+---+
^
|
+------------+
|
p |
+-------+ |
| o----------+
+-------+
otra
+---+---+---+-- ... ---+---+---+
| A | \0| xx| x xx| xx| xx|
+---+---+---+-- ... ---+---+---+
printf( "%s", otra );
This outputs "A", since otra contains a valid C style string, which
consists of the characters 'A' and '\0'. printf starts its output at
otra[0] and stops it at otra[1], since there is a '\0' character.
But: double check the posted program with the one you compiled. The
version you posted is illegal and no compiler should compile it without
an error message.
--
Karl Heinz Buchegger
kb******@gascad.at