Hello, I'm slightly confused with Address aspect.
^ This means that *pi = 3Code:int i=3 int *pi = &i
^ For above, *i would mean the address?Code:int i=3 *i
My question is, what does the * mean? Thanks in advance, Air.
Hello, I'm slightly confused with Address aspect.
^ This means that *pi = 3Code:int i=3 int *pi = &i
^ For above, *i would mean the address?Code:int i=3 *i
My question is, what does the * mean? Thanks in advance, Air.
2 different meanings.
In declaration, * following the type means you are declaring means declare a pointer to that type.
A * in front of a pointer means dereference the pointer.
Code:int i = 3; int *ptr = &i; //declares a pointer to an int, and assign the address of i to it printf("%d", *i); //prints 3 (value stored at the address pointed to by the pointer)
*i is "undefined" -- it is the value of the int in memory address 3, which may not even exist, and who knows what's in it even if it does.
No, it's illegal, because i is not a pointer.
Consequently, pointers can also be declared as:
(Note the position of the *.)Code:int i = 3; int* ptr = &i; //declares a pointer to an int, and assign the address of i to it printf("%d", *i); //prints 3 (value stored at the address pointed to by the pointer)
I prefer to have it my way and I think that makes most sense... but let's not argue about it in this thread and confuse the OP.
To OP: Either mine or Elysia's code will work. For now just pick one and stick to it. It's just a stylistic issue and doesn't actually affect the functionality of the code.