You use ted = &var to say ted points to address of var
You use *ted to say value pointed by ted
You use * var to declare a pointer of the type that precedes it.
I don't understand the arrays part...
I use the code:[php]
char array[] = "hello there";
[/php]
and it works, so why wouldn't
[php]
array = "hello there";
[/php]
not be valid? I don't understand...
Because its an array. Char only holds 1 character
Originally Posted by Koreans
Because its an array. Char only holds 1 character
ok...
how bout
int numbers [20];
int * p;
int = p;
why would that not be valid... int holds up to 60k integers...
Originally Posted by VvITylerIvV
ok...
how bout
int numbers [20];
int * p;
int = p;
why would that not be valid... int holds up to 60k integers...
ints can hold a VALUE of around 65k.
And
[php]int = p;[/php]
is illegal. thats like saying int is p.
Originally Posted by Koreans
ints can have a VALUE of around 65k.
And
[php]int = p;[/php]
is illegal. thats like saying Int is p.
no, int = "p"; would be saying it is p
int = p is saying int = value of p...
Originally Posted by VvITylerIvV
no, int = "p"; would be saying it is p
int = p is saying int = value of p...
int is a variable type
you cant assign a value to it
Koreans speaks the truth. The compiler won't allow that, ever...
Here is a simple program that creates a pointer to an integer
[php]#include <iostream>
using namespace std;
int main()
{
int apple = 5;
int *pApple = &apple;
cout << *pApple << '\n';
return 0;
}
[/php]
Output:
Code:
5
Since pApple points to apple, when we [php]cout << *pApple;[/php] were really getting the value of apple
REMEMBER: Initialize a pointer to something or else youll have a wild pointer and can cause unwanted behavior. Even if youre not using it right away, initialize it to 0
Originally Posted by Koreans
int is a variable type
you cant assign a value to it
Originally Posted by Void
Koreans speaks the truth. The compiler won't allow that, ever...
Well, I was just trying to ask why can't I assign a value from a variable to an array.
Originally Posted by Koreans
Here is a simple program that creates a pointer to an integer
[php]#include <iostream>
using namespace std;
int main()
{
int apple = 5;
int *pApple = &apple;
cout << *pApple << '\n';
return 0;
}
[/php]
REMEMBER: Initialize a pointer to something or else youll have a wild pointer and can cause unwanted behavior. Even if youre not using it right away, initialize it to 0
I know how to use pointers, it was the array part that confused me... I don't understand it.
Originally Posted by Koreans
Here is a simple program that creates a pointer to an integer
[php]#include <iostream>
using namespace std;
int main()
{
int apple = 5;
int *pApple = &apple;
cout << *pApple << '\n';
return 0;
}
[/php]
Output:
Code:
5
Since pApple points to apple, when we [php]cout << *pApple;[/php] were really getting the value of apple
REMEMBER: Initialize a pointer to something or else youll have a wild pointer and can cause unwanted behavior. Even if youre not using it right away, initialize it to 0
Right, 'cause reading at address 0 definitely won't give you access violation errors.
Then why did you ask me why cant you do [php]int = p[/php]?
Originally Posted by Void
Right, 'cause reading at address 0 definitely won't give you access violation errors.
You re-assign it when youre going to use it
amiright /?
Originally Posted by Koreans
Then why did you ask me why cant you do [php]int = p[/php]?
You re-assign it when youre going to use it
because I didn't know how to explain my question then...
Originally Posted by Koreans
Then why did you ask me why cant you do [php]int = p[/php]?
You re-assign it when youre going to use it
amiright /?
Then just leave the default value wild and re-assign it later. Same results.. |:
Anyways, arrays are basically pointers..
[php]
int array[] = {1,2,3,4,5};
[/php]
To access the last integer in the array, you can either use the brackets ( arrays ).
[php]
cout << array[4];
[/php]
Or you can use pointers.
[php]
cout << *(array+4);
[/php]
Remember, the first element is always 0, so if you have 5, the last one is going to be 4. Incase you didn't already know.
Originally Posted by Void
Then just leave the default value wild and re-assign it later. Same results.. |:
Anyways, arrays are basically pointers..
[php]
int array[] = {1,2,3,4,5};
[/php]
To access the last integer in the array, you can either use the brackets ( arrays ).
[php]
cout << array[4];
[/php]
Or you can use pointers.
[php]
cout << *(array+4);
[/php]
Remember, the first element is always 0, so if you have 5, the last one is going to be 4. Incase you didn't already know.
I'm just restating what mah bewk said
Originally Posted by Void
Then just leave the default value wild and re-assign it later. Same results.. |:
Anyways, arrays are basically pointers..
[php]
int array[] = {1,2,3,4,5};
[/php]
To access the last integer in the array, you can either use the brackets ( arrays ).
[php]
cout << array[4];
[/php]
Or you can use pointers.
[php]
cout << *(array+4);
[/php]
Remember, the first element is always 0, so if you have 5, the last one is going to be 4. Incase you didn't already know.
Ok, so in this case...
[php]
array [3]
[/php]
points to the address of array [3], doesn't actually hold a value?