Mail Archives: djgpp/1998/10/13/09:46:33
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 -