![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programmer
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4
![]() |
Reference, address. Same thing.
|
|
|
|
|
|
#12 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
>Reference, address. Same thing.
It depends on what you are talking about. In theory, pointers, addresses, and references are all basically the same concept. In C++, a pointer is an address, but a reference has different semantics. Saying that they are the same thing would only confuse the issue more. >But the & sign in addition to standing for an address, can also be a reference. Or even bitwise AND. ![]() |
|
|
|
|
|
#13 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
Hmm I think im still having trouble understanding
Is & and * the same thing? So when you use * it defines it to the address of a variable? So why do you use it when you declare a variable? Wouldnt you only do it when you define a variables name... like whats the point of doing this char* bloh; ? What does it do diffrently than char bloh; ? And this is the last question I have, I think it doesnt work but I dont know... char one = "One"; char two = *one; char one = "Two"; cout << two; Would that ouput two? or nothing? or how could I make it output two without drasticaly changing the code? I really dont get this... I thought I did but now I dont... Last edited by brokenhope; Apr 25th, 2005 at 7:15 PM. |
|
|
|
|
|
#14 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
int * px; <--declares a pointer of type int (points to an int) x=1; <-- just a regular variable px = &x; <--assigns the address of x to px's value now px is assigned the address of x. if you were to cout<<px<<endl cout<<&x<<endl; if you cout<<*px<<endl; you use the * operator to declare a pointer. that's why a lot of people like to put it next to the type like this: int* px; px is the pointer, it is the address of x. *px is the value pointed to. &x is the address of x. the * is called the dereference operator because when you use it on a pointer you "dereference" the pointer which means to get the value of what it is referencing. the & and * symbols are unfortunately used in some different ways which can lead to confusion. as to your pseudo-code char one = "One"; char two = *one; char one = "Two"; cout << two; here is what you want char one = 'o'; char * two = &one; one = 't'; // <-- or *two = 't'; either way cout<<*two<<endl; i didn't declare the whole words because you can't do it like you did you have to use an array like char one[] = "One"; and this will severely complicate this issue for you. i suggest you play with ints or something else right now for messing around with pointers.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. Last edited by bl00dninja; Apr 25th, 2005 at 7:39 PM. |
|
|
|
|
|
#15 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
hmm I think im starting to understand, so what happens in an example where y points to x, and the x variable changes several times, does the y variable change with the x variable? Like
int* y; int x = 4; cout << y << endl; y = &x; x = x*2; cout << y << endl; Would that output: 4 8 or what? |
|
|
|
|
|
#16 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You need to output *y instead of y. Plain ol' vanilla "y" is the address of the variable x; "*y" points to the value of it.
|
|
|
|
|
|
#17 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
Ah, now I understand.. when I compile it and try to run it theres nothing on the screen, and a warning error coming up saying that the program encountered a problem and needs to close comes up...
#include <iostream.h>
#include <stdlib.h>
using namespace std;
int main()
{
int* y;
int x = 4;
cout << *y << endl;
y = &x;
x = x*2;
cout << *y << endl;
system("PAUSE");
return 0;
} |
|
|
|
|
|
#18 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
#include <iostream.h>
#include <stdlib.h>
using namespace std;
int main()
{
int* y;
int x = 4;
cout << *y << endl; //<--y isn't pointing to anything yet...stray pointer!
y = &x; //<-- move this line up one and you should be good
x = x*2;
cout << *y << endl;
system("PAUSE");
return 0;
}now corrected #include <iostream.h>
#include <stdlib.h>
using namespace std;
int main()
{
int* y;
int x = 4;
y = &x;
cout << *y << endl;
x = x*2;
cout << *y << endl;
system("PAUSE");
return 0;
}tada!!! wasn't that easy?
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#19 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
Oh that was just a mistake of not paying attention to where I added the *, thanks a lot I think I finally understand this.
|
|
|
|
|
|
#20 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
feel free to ask more questions in the future.
good luck!
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|