delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/30/04:34:49

From: "Dave Bird---ARS HakeMonger ,,,,><_'>.,,,<_\",,,," <dave AT xemu DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Q: How does a pointer work and when do you need it?
Date: Sat, 29 May 1999 20:39:19 +0100
Organization: very little
Message-ID: <mD1r14AnJEU3Ewh7@xemu.demon.co.uk>
References: <7iof2p$l3e$1 AT news DOT news-service DOT com>
NNTP-Posting-Host: xemu.demon.co.uk
X-NNTP-Posting-Host: xemu.demon.co.uk:158.152.196.209
X-Trace: news.demon.co.uk 928050350 nnrp-11:19873 NO-IDENT xemu.demon.co.uk:158.152.196.209
X-Complaints-To: abuse AT demon DOT net
MIME-Version: 1.0
X-Newsreader: Turnpike (32) Version 4.01 <dQumtnY$x4rJ2u5tL5fS$n2vuP>
Lines: 60
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

In article <7iof2p$l3e$1 AT news DOT news-service DOT com>, SufMan <SufMan AT Suf DOT NL>
writes
>Please.. Can someone help me??
>Q: How does a pointer work and when do you need it?

 That is a big subject, and you might want to try a textbook.



 An ordinary, say, integer variable contains an integer value:

 int x1;   x1=10000;

 address of x1 is FF208 ----  FF208 [ 10000 ]
                              FF20C      \
                              ff210     value contained there is 10000 .



 If it is a pointer to integer, it contains the address of 
 another variable:

 int *px;    px = &x1;

 address of px is FF340 ---  FF340  [FF208 ]
                             FF344       \
                             FF348        value contained there is FF208
                                          the address of x1.

 If you write  *px=27;  the value 27 is put at the address contained
 in px, that is it is stored in x1.



 Normally parameters are passed to functions BY VALUE.  If you want
 y=sqrt(x1), when x1 is 10000, then sqrt() is passed a  copy of the 
 value 10000 and returns the answer 100 to be stored in y. It does
 not need to know about the location of x1, it does not need to, it
 cannot change what is stored there.

 If you want a function to, say, double the contents of a specified
 variable, then you have to send the address of it:
 void doubleVar(int *px){           (*px) = (*px) * 2;         };
 doubleVar(&x1);  // this will double the value in x1.
 The same if you want to tell your function to put results in  3 or 4
 different variables specified by the caller, you have to send 
 their addresses.



 There are other uses concerning arrays & strings, but a textbook
 is the place top look for them.

 

--  
 Dave Bird, an official ARS HakeMonger                        ><_'>  <_"
(licensed to mung pelagic fish and clams of all kinds upon the Internet)
"If prozac be the food of clams, Lay on MacErrel and, by damn, 
    Cry HADDOCK  and let loose the cods of war!"            Wm Skatesfin

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019