The first ++ tried to modify what p points to.
Since p points to a string constant, your program dies because it tried to modify something in read-only memory. Read More
> char *p = &q;
Did you try it?
&q has a different type to q, so any good compiler would complain about incompatible pointer assignments. Read More
[QUOTE=jephthah;915989]yes, you're right.
even though it works for this trivial example, not casting it properly is a bad habit to get into.
the correct way to initialize it is:
char *p = [color=red][b](char *)[/b][/color]&q;
So you get the wrong type, then cast it to the right type … Read More
[QUOTE=tux4life;916334]A little addition on:
[QUOTE]Array names become pointers[/QUOTE]
Yes, an array name is a constant pointer to the first element of that array.[/QUOTE]
An array name is the name of an array object, nothing more, nothing less. When used as a value, it is converted to a pointer to the … Read More
It seems that most people other than Tom Gunn need to swing by here and have a nice long read.
[url]http://c-faq.com/aryptr/index.html[/url] Read More
> NOte that here break 1 (p++) and break2 (*p++ ) doing the same thing.
Better add someone else to the reading list....
No they're not the same, one merely increments the pointer, the other one ALSO dereferences it at some point.
The second being equivalent to
[ICODE]someImaginaryDummyWhichGetsThrownAway = *p++;[/ICODE] … Read More
[QUOTE=Salem;917789]Better add someone else to the reading list[/quote]
this thread is a disaster. :)
you are a patient man, Salem.
. Read More
Well to be pedantic again, you should have written
const char *p="abc";
then the compiler itself would have complained about the attempt to modify a constant.
Because so much existing code (prior to C89) used this construct before the idea of putting "strings" in read-only memory came along, it was … Read More
[char *ptr = "abc"]is the same as
[const char * ptr], so the compiler's assigns memory which is only readable and not write"able". i guess that is what is meant by Read only memory as stated above :) Read More
[QUOTE]3) wrong, you only create a pointer of type char, this can't hold a string.[/QUOTE]
The next line assigns the pointer to a string constant:
There is no problem here, and the string is read only. It is the same to initializing directly:
char … Read More