Benjamin Juang (ibneko) wrote,
Benjamin Juang
ibneko

C malloc/pointer question

What's the difference between:

image_cover_t* new_cover; /* the new cover *
/* Allocate the new image cover. */
if (NULL == (new_cover = malloc (sizeof (*new_cover)))) {...


and


int *iptr;
iptr = (int *)malloc(10 * sizeof(int));
if (iptr == NULL){...


Namely, the malloc line... I don't understand why one uses sizeof(address-of-variable) and another uses sizeof(variable-type)? Help?

(Yeah, this is probably absurdly basic, but.. I dunno. This is what I get for learning Java before C, and not really fully understanding memory)

Like.. would, "if (NULL == (new_cover = malloc (sizeof (image_cover_t)))) {..." work too...?
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 3 comments