Pointer declaration and initialization
Hi,
I'm wondering what actually happens when you declare a pointer and initialize it on the same line.
Does it set the pointer's value or the value the pointer points to?
Which of the following is correct?
int *a = 1;
or
int *b = malloc(4);
Thanks,
Cedric
This must be it, I think!
I don´t know about that Malloc thing, but this is how I do it:
int number = 0;
int* pNumber = 0;
// sets the pointer pNumber to point at the adress of number
pNumber = &number;
*pNumber = 5; // gives the value 5 to the variable number
cout << "number is: " << number << endl;
output: number is: 5
This must be it, I think!
I don´t know about that Malloc thing, but this is how I do it:
int main()
{
int number = 0;
int* pNumber = 0;
// sets the pointer pNumber to point at the adress of number
pNumber = &number;
*pNumber = 5; // gives the value 5 to the variable number
cout << "number is: " << number << endl;
return 0;
}
output: number is: 5