1) to initialize a pointer I use:
int number, *Pnumber;
Pnumber=&number;
number=10;
Am I doing this right?
what about:
int *Pnumber;
*Pnumber=10;
When I compile it, I get:
RUN FAILED (exit value 1, total time: 858ms)
btw. do I need to use free(Pnumber) to free the memory?
Yes, you are.
Pnumberis an unitialized pointer. Dereferencing this pointer leads to an undefined behavior.Pnumbermust point to an allocated memory (either to a variable or a dynamically allocated memory area).As long as you don’t use
malloc, don’t usefree.