|
-
Oct 23rd, 2001, 07:10 PM
#1
Pointer c++
I just want to know what is the utility of a pointer ? I just started the chapter about it and after the second page I do not understand why I have to learn that ? Please give me just the utility about it ( I will continue to read to know how to use it but I really want real person explication ).
-
Oct 23rd, 2001, 07:18 PM
#2
ok now I am lost why should I need to use : & before a variable ? That give me only a strange number...
-
Oct 23rd, 2001, 07:34 PM
#3
Frenzied Member
The clue to what pointers are is in the name.
A pointer is a variable that can be used to point to another variable. Which variable it points to can be changed at runtime.
& in the context you are talking about, when you put it before a variable identifier, will yield the address of the variable in memory.
For instance:
Code:
char *p;
char A;
char B;
A = 'a';
B = 'b';
p = &A; // read as "p equals the address of A"
At this point, p points to A. You can now use *p just like you'd use A. the * is the indirection operator. It allows you to use the variable the pointer is pointing to. *p evaluates to 'a' here.
later in the code you could do this:At this point, p points to B, and *p evaluates to 'b' now.
This functionality is very useful. Honest
Keep reading about pointers, they're very important. VB hides them from you to make your life simpler but still uses them behind the scenes virtually all the time. C and C++ are lower level so you get to deal with these things yourself.
Harry.
"From one thing, know ten thousand things."
-
Oct 23rd, 2001, 07:45 PM
#4
Fanatic Member
So would this print 5?
Code:
p = &B;
*p = 5;
cout<<B;
-
Oct 23rd, 2001, 08:07 PM
#5
Lively Member
Originally posted by Wynd
So would this print 5?
Code:
p = &B;
*p = 5;
cout<<B;
Yes, but thats not what you want to do in most cases
That says "p points to B. The value of the variable that p points to is 5. (That makes B 5) and then print out B."
Most likely, you'll see this:
PHP Code:
B=5;
p=&B; //To get the address of B
cout<<*p;
Think of pointers as a regular variable that holds the address of the variable, that is, where it is in memory. To get the address of a varaible, put a & in front of its name.
if(GetWindowLong(hwnd,GWL_ID)==IDC_MICROSOFT_APPLICATION)
{
SetWindowText(hwnd,"I suck.");
SendMessage(hwnd,WM_START_SUCKING,0,0);
SendMessage(hwnd,WM_CRASH,0,0);
}
-
Oct 23rd, 2001, 08:19 PM
#6
Give me an exemple because if I need the variable B will use B and not a pointer....
I think I do not understand sorry
-
Oct 23rd, 2001, 09:17 PM
#7
Hyperactive Member
Code:
//Will not work
void myfunc(int x);//function declaration
main()
{
int x=5;
myfunc(x); //Only a copy of the value is pass(variable passed by value)
cout<<x; //prints 5;
return 0;
}
void myfunc(int x)
{
x=8;
return;
}
Code:
//Will work
void myfunc(int *x);//function declaration
main()
{
int x=5;
myfunc(&x); //a copy of the address is pass(variable passed by reference)
cout<<x; //prints 8;
return 0;
}
void myfunc(int *x)
{
*x=8;
return;
}
Code:
//Will work(another version)
void myfunc(int *x);//function declaration
main()
{
int x=5;
int *xp=&x;
myfunc(xp); //No * on the left of xp
cout<<x; //prints 8;
return 0;
}
void myfunc(int *x)
{
*x=8;
return;
}
-
Oct 23rd, 2001, 09:22 PM
#8
Hyperactive Member
Pointers are usually used in and passed to functions which need to change the value of the original variables. return can be used if you only want to return one value.
int myfunc(int ...., int....., int......)
{
............
x=8
return x;// can only return one value
}
hope it helps.
-
Oct 24th, 2001, 11:30 AM
#9
Code:
void myfunc(int i)
{
// nothing
}
void main()
{
int val = 3;
myfunc(val);
}
this creates a copy, as already said. That's no disaster, since an int is only 4 bytes. But now imagine:
Code:
typedef struct _bigstruct // did you learn about structs? They are just a collection of variables that are grouped together under a new name
{
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
int j;
int k;
} BIGSTRUCT;
void myfunc(BIGSTRUCT bs)
{ // still nothing
}
void main()
{
BIGSTRUCT big;
// initialize values
myfunc(big);
}
With every int taking 4 bytes, this structure takes a full 44 bytes. 44 bytes that have to be allocated and copied every time the function is called. But look at this:
Code:
typedef struct _bigstruct
{
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
int j;
int k;
} BIGSTRUCT;
void myfunc(BIGSTRUCT* pbs)
{ // still nothing
}
void main()
{
BIGSTRUCT big;
// initialize values
myfunc(&big);
}
Voila, only 4 bytes (pointer size) to copy. This is much faster code. Just watch out when changing the values of this struct. They will be changed in the original object. But you can tell everyone that you do not intend to change the values:
void myfunc(const BIGSTRUCT* pbs);
This also tells the compiler to generate errors if you accidently change the values.
I hope I didn't confuse you...
Last edited by CornedBee; Oct 24th, 2001 at 12:04 PM.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 24th, 2001, 05:25 PM
#10
I think i understand maybe I will have some other question later about it.
-
Oct 26th, 2001, 10:49 AM
#11
Addicted Member
Maybe this will help maybe not. A pointer points to the place in memory where the varible is stored. All varible are stored in memory so this way you do not have to pass the byte of the varible to the function just the address. It keep you overhead(fat of the program) down and allow the program to run faster.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|