delorie.com/archives/browse.cgi | search |
From: | "Marp" <marp AT 0 DOT 0 DOT 0 DOT 0> |
Newsgroups: | comp.os.msdos.djgpp |
Subject: | Re: Type casting and pointers |
Date: | Wed, 1 Aug 2001 17:59:40 -0400 |
Organization: | MindSpring Enterprises |
Lines: | 58 |
Message-ID: | <9k9u34$d9c$1@slb7.atl.mindspring.net> |
References: | <wyZ97.2407$n_3 DOT 3163465 AT typhoon DOT ne DOT mediaone DOT net> |
NNTP-Posting-Host: | 9e.fc.1f.18 |
X-Server-Date: | 1 Aug 2001 21:59:00 GMT |
X-Priority: | 3 |
X-MSMail-Priority: | Normal |
X-Newsreader: | Microsoft Outlook Express 5.50.4522.1200 |
X-MimeOLE: | Produced By Microsoft MimeOLE V5.50.4522.1200 |
To: | djgpp AT delorie DOT com |
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
Reply-To: | djgpp AT delorie DOT com |
The only purpose of typecasting in the example given is to suppress a compiler warning. It's not a good example :-) Normally you would use typecasting to override the compiler's default behavior in promoting data types. Given this: int a = 1; double b = 2.5; double c; And this: c = a + b; The variable c will hold 3.5 because the compiler promotes a to double for the purposes of the addition. But this: c = a + (int)b; The variable c will hold 3 since you told it to demote b to int (which causes it to use the truncated value of b for addition). BTW, you get a warning in the book's example without a cast because you are assigning a float ** to a float *. They are not the same thing. Hope this helps. - Marp "Steve Dondley" <stevedondley AT mediaone DOT net> wrote in message news:wyZ97.2407$n_3 DOT 3163465 AT typhoon DOT ne DOT mediaone DOT net... > Hello, > > I've got a newbie C question. I've got a beginner's C book that doesn't > explain something very well. Example code from the book: > > float *p; > float balance[10][5]; > ...assignment stuff here... > p = (float *) balance; > > My code works just fine without the (float *) type cast, but I do get a > warning. Why exactly do you need it? Doesn't declaring *p as a float > pointer at the top of the source take care of that? The book also never > explains what exactly the asterisk in (float *) is. I'm just assuming it > type casts the pointer. > > Can someone please provide some elucidation? Thanks! > > ---Steve > > >
webmaster | delorie software privacy |
Copyright © 2019 by DJ Delorie | Updated Jul 2019 |