Help understanding pointer assignment in c

after some years of pause, im returning to c.

char *varname = "asd";
int *number = 4;

the above code is wrong, because its assigning a value to an unreserved space, or because its changing the address the pointer is pointing ?

thanks for the replys!!

The pointer number is undefined. You could do this, however:

int numberStorage;
int *number = &numberStorage;
*number = 4;

That example basically sets numberStorage to 4.

The string example is okay because the double quotes allocate static storage automatically.

Julian

hmmm, so, with a "asdasd", the compiler takes care of alocating the memory

with the integer example, i have to use malloc, or create a normal integer, and point the pointer to the real integer

thanks for the clarification

broli,

C does not hide memory pointer information.

"asdf" is a "string literal."

when initializing a char*, "asdf" is seen by the program as a pointer to that location in memory.

The same is not true for other basic types.

int *Bogus = { 0, 1, 2, 3, 4}; - does not work
int Correct[5] = { 0, 1, 2, 3, 4}; does work

malloc() allocates memory on a global heap, and requires a type cast.
using [] allocates memory locally, and the size of this chunk is limited, so don't use brackets for really big arrays.

Two operators will come in handy all the time... "*" and "&"