Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #5388

Re: Questions Regarding Null and Casting

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: Questions Regarding Null and Casting
Date 2011-06-04 10:13 +1200
Message-ID <94t4kkFbjiU13@mid.individual.net> (permalink)
References <isbl3d$lvh$1@speranza.aioe.org>

Show all headers | View raw


On 06/ 4/11 09:52 AM, BigChuck19 wrote:
> My textbook asserts that when checking if sufficient memory was allocated
> for a
> dynamic variable using malloc(), a test should be made to determine
> whether
> sufficient memory was actually allocated.  I understand the concept of
> checking
> if the memory was allocated; the code confuses me, however.  E.g., a
> fragment
> of code from the book is:
>
> grades = (int *) malloc(numgrades * sizeof(int));

grades = malloc(numgrades * sizeof(*)grades);

Would have been better.

> if (grades == (int *) NULL) {
>     printf("Failed to allocate grades array");
>     exit(1);
>
> }
>
> My question concerns the use of the cast (int *).  The book states that
> one
> must write " if (grades == (int *) NULL) " because if there is
> insufficient
> memory available, malloc() returns NULL, which is then cast into a
> pointer to
> an integer by the (int *) preceding the call to malloc().

The cast is both superfluous and in that case, confusing.

if (grades == (NULL)

is all that is required.

<snip>

> The use of a cast on NULL raises a few more questions.  Say I create a
> character array called array.  After initializing all its entries, I then
> want
> to clear the entire array by making each entry hold the null character.  I
> could write something like the following to accomplish this:
>
> for(i = 0; i<  MAXTENTRIES; i++)
>      array[i] = '\0';
>
> Does the following loop perform the identical operation?
>
> for(i = 0; i<  MAXENTRIES; i++)
>     array[i] = (char) NULL;

In practice, yes.  But it is a confusing way of writing (and the cast is 
superfluous again).

> And I assume if the preceding code is valid, then one can cast NULL as
> any data
> type; for instance,
>
> for(i = 0; i<  MAXENTRIES; i++)
>     array[i] = (int) NULL;
>
> would assign each entry the value of zero?

There could exist systems where NULL isn't zero...

> I am confused about the definition of NULL and the use of NULL; is it
> only used
> when using pointers?

That is is idiomatic use.  Use NULL for pointers and 0 elsewhere.

I am not sure, since the use of a cast on NULL
> seems to
> indicate that one can make NULL any data type.

A cast is the C equivalent of a sledge hammer.  If you want to force a 
square peg in round hole, use a big hammer.

> My final question concerns the use of NULL with a structure.
> If I define a structure, say,
>
> typedef struct house {
>     int floors;
>     char owner[MAXSIZE];
>
> } House;
>
> and then declare h to be a variable of type house
>
> House h;
>
> Now does
>
> h = (House) NULL;
>
> have any meaning??

No, it isn't valid.

-- 
Ian Collins

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Questions Regarding Null and Casting BigChuck19 <bc19@mailinator.com> - 2011-06-03 21:52 +0000
  Re: Questions Regarding Null and Casting Ian Collins <ian-news@hotmail.com> - 2011-06-04 10:13 +1200
    Re: Questions Regarding Null and Casting Ian Collins <ian-news@hotmail.com> - 2011-06-04 10:16 +1200
  Re: Questions Regarding Null and Casting Keith Thompson <kst-u@mib.org> - 2011-06-03 15:25 -0700
    Re: Questions Regarding Null and Casting Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-03 20:45 -0700
  Re: Questions Regarding Null and Casting pete <pfiland@mindspring.com> - 2011-06-03 18:38 -0400
  Re: Questions Regarding Null and Casting Angel <angel+news@spamcop.net> - 2011-06-03 23:23 +0000
    Re: Questions Regarding Null and Casting Keith Thompson <kst-u@mib.org> - 2011-06-03 17:24 -0700
  Re: Questions Regarding Null and Casting "J. J. Farrell" <jjf@bcs.org.uk> - 2011-06-04 00:45 +0100
  Re: Questions Regarding Null and Casting Keith Thompson <kst-u@mib.org> - 2011-06-03 17:27 -0700
  Re: Questions Regarding Null and Casting pete <pfiland@mindspring.com> - 2011-06-03 21:12 -0400
  Re: Questions Regarding Null and Casting Shao Miller <sha0.miller@gmail.com> - 2011-06-06 20:11 -0500

csiph-web