Pointers/addresses

Posts 1–15 of 47 · Page 1 of 4
Pointers/addresses
Pointers

I read this and I kind of understand it...

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
Quote Originally Posted by Auxilium View Post
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...
Quote Originally Posted by VvITylerIvV View Post
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.
Quote Originally Posted by Auxilium View Post


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...
Quote Originally Posted by VvITylerIvV View Post
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
Quote Originally Posted by Auxilium View Post


int is a variable type

you cant assign a value to it
Quote Originally Posted by Void View Post
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.

Quote Originally Posted by Auxilium View Post
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.
Quote Originally Posted by Auxilium View Post
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]?

Quote Originally Posted by Void View Post
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 /?
Quote Originally Posted by Auxilium View Post
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...
Quote Originally Posted by Auxilium View Post
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.
Quote Originally Posted by Void View Post
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
Quote Originally Posted by Void View Post
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?
Posts 1–15 of 47 · Page 1 of 4

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us