Pointers(Again)

Started by
10 comments, last by Tazel 21 years, 4 months ago
Thanks to whoever posted the link to pointers. But it didn''t help much. I already knew how to use them syntax wise and that you used them for linked lists and an unknown number of memory size needed. But I still can''t think of a time where it would be useful to use it!!! Maybe I''m just stupid.... tcache Contact Me ----------- Games don''t affect kids. If Pac-man had affected us, we''d all be sitting in darkened rooms munching pills and listening to repetitive electronic music. -- Kristian Wilson(Nintendo Inc., 1989)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Advertisement
Hi Tazel!

Let''s take advantage of an old easy to understand example.
Suppose you have two integer variables a and b and you want to swap the values between them and you want to do that a million times in your code. Well, let''s make a function that can do just that..


void swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
}

Great, that was easy, but wait!! nothing has changed. a and be are still the same. Well.. if we could just send the adresses of our variables to the function and manipulate them inside.. Wouldn''t that be great?? Now we can use pointers instead...

void swap(int* a, int*b)
{
int temp;
temp=a*;
a*=b*;
b*=temp;
}


This is just what we wanted. We called the function with pointers to the variables and could manipulate just the a and b variables, not copies of them as in the first example.

This was juts an easy example but it''s one good example when to use pointers. Hope it''s clearer now..
oops.. sorry bout that... C-blackout, don''t know why I wrote it that way... the code of course should be as follows

void swap(int* a, int*b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
Why does the compiler send copies in the first place?

tcache
Contact Me
-----------
Games don''t affect kids. If Pac-man had affected us, we''d all be sitting in darkened rooms munching pills and
listening to repetitive electronic music. -- Kristian Wilson(Nintendo Inc., 1989)

tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Because the function argument is taking an instance of the class, namely a new int. So in a function like this:

int aplusplus(int a)
{
a++;
return a;
}

and by calling it like
int c = 0;
cout << aplusplus(c) << endl;

it makes a new int for the function call, a, and then copies c into it. c is never changed during the whole process. By putting a reference in there, however, we can change c and avoid the object copy. The new function def would be the same except it would read
int aplusplus(int &a), where it is now using a as a reference to some object.

Brendan
Brendan"Mathematics is the Queen of the Sciences, and Arithmetic the Queen of Mathematics" -Gauss
quote:Original post by Tazel
Why does the compiler send copies in the first place?


Call by value vs. call by reference.


quote:
I already knew how to use them syntax wise and that you used them for linked lists and an unknown number of memory size needed. But I still can''t think of a time where it would be useful to use it!!! Maybe I''m just stupid....


Don''t think that because pointers exist you must use them everytime. If you need to implement a linked list or allocate memory dynamically, then yes, you use pointers. Or if you need to pass arguments by reference you can use pointers or references.

If you understand how a pointer works, move on, don''t get stuck on it because you expect something more. As you progress, you''ll learn various applications of pointers.
All right then. Thanks, Arkainium and the rest of you. If I get stuck, I know where to turn to...

tcache
Contact Me
-----------
Games don''t affect kids. If Pac-man had affected us, we''d all be sitting in darkened rooms munching pills and
listening to repetitive electronic music. -- Kristian Wilson(Nintendo Inc., 1989)

tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
A pointer is just an address that points to the start of a memory block.

------

With kind regards,

Sander Aerts aka DrSnugels

Crap
---------------------------
mov ax, 013h; int 10h; mov ax, 0xa000h;
mov es, ax; mov di, 3360; mov al, 15;
stosb; mov ax, 0002h; int 21h;
mov ax, 03h; int 10h;

[edited by - Sander Aerts on December 9, 2002 11:59:58 PM]
------With kind regards,Sander Aerts aka DrSnugelsCrap :)---------------------------mov ax, 013h; int 10h; mov ax, 0xa000h;mov es, ax; mov di, 3360; mov al, 15;stosb; mov ax, 0002h; int 21h;mov ax, 03h; int 10h;
Hey Tazel contact me:

Email - absoluteability@aol.com
AIM - Absolute Ability
ICQ - 177254473

Thanks.

~ Solid Metics

"You probably use those fat crayons made for retarded kids." - warbux
"You probably use those fat crayons made for retarded kids." - warbux
So basically pointers are useful for linked lists and handling big stuff like big arrays?

tcache
Contact Me
-----------
Games don''t affect kids. If Pac-man had affected us, we''d all be sitting in darkened rooms munching pills and
listening to repetitive electronic music. -- Kristian Wilson(Nintendo Inc., 1989)

tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)

This topic is closed to new replies.

Advertisement