delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/10/13/09:46:33

From: Joe Wright <conserv3 AT infi DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Pointer Usage
Date: Tue, 13 Oct 1998 09:40:19 -0400
Organization: PC Access
Lines: 63
Message-ID: <36235843.1189@infi.net>
References: <3622bf31 DOT 0 AT news1 DOT tm DOT net DOT my>
NNTP-Posting-Host: pm4-1-s3-152.orf.infi.net
Mime-Version: 1.0
X-Mailer: Mozilla 3.03Gold (Win95; I)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Earlblunt wrote:
> 
> Hello,
> 
> I am new in C programming and now learning about pointers.
> Could anyone check for me the codes below for the CORRECT usage of pointers?
> 

I don't know how you got this to compile.  There are several errors.

> int main()
> {
> char crispin[15] , *zunliang[15] ,  *fauzi[15];

crispin is an array of char but zunliang is an array of pointers to
char, as is fauzi.

> int cris ,  *liew , *ogy;

cris is an int while liew and ogy are pointers to int.
> 
> strcpy(crispin, "He's in 2A.");

this is ok.

> strcpy(zunliang, crispin);
> strcpy(fauzi, zunliang);

these are not. in this context while crispin is pointer to char,
zunliang is a pointer to a pointer to char.  type mis-match and
requuires a compiler diagnostic.  
> 
> printf("%s\n", crispin);
> printf("%s\n", zunliang);
> printf("%s\n", fauzi);
> 
> cris = 14;
> liew = cris;
> ogy = cris;
> 
> printf("Info on Crispin: %s and he is %d years old.\n", crispin, cris);
> printf("Info on Liew Zunliang: %s and he is %d years old.\n", zunliang,
> liew);
> printf("Info on Mohd. Fauzi: %s and he is %d years old.\n", fauzi, ogy);
> 
> return 0;
> }
> 
> /* OUTPUT:
> 
> He's in 2A.
> He's in 2A.
> He's in 2A.
> Info on Crispin: He's in 2A. and he is 14 years old.
> Info on Liew Zunliang: He's in 2A. and he is 14 years old.
> Info on Mohd. Fauzi: He's in 2A. and he is 14 years old.
> 
> */

-- 
Joe Wright                                 mailto:conserv3 AT infi DOT net
"Everything should be made as simple as possible, but not simpler."
                    --- Albert Einstein ---

- Raw text -


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