C4047 warning in a project

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mullhead
    New Member
    • Nov 2007
    • 1

    C4047 warning in a project

    This warning keeps popping up...

    box.c(50) : warning C4047: '=' : 'box *' differs in levels of indirection from 'size_t'

    49 pBox = malloc(sizeof(b ox)*lineCount-1);
    50 assert(pBox = (sizeof(box)*li neCount-1));


    typedef struct
    {
    double width;
    double length;
    double height;
    double weight;
    double volume;
    char contents[32];
    } box;

    box box1[10] = {0};
    box* pBox = box1;

    here is my declaration and the problem with the allocation. I needed to read in from a file assign the text to an array of structs and dynamically allocate memory for the array.
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    assert(pBox = (sizeof(box)*li neCount-1));
    Ehhh? pBox is a pointer. sizeof.... gets you a size_t value, which is comparable to a long int or the like. It's basically an unsigned integral value. See the problem in your assertion? If you want to check if malloc worked, check if pBox is NULl after the call to malloc. If so, malloc failed.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by mullhead
      50 assert(pBox = (sizeof(box)*li neCount-1));
      You have fallen victim to decay of array. That is, from a pointer's point of view, all you have is an address. The number of elements has been lost. You have to remember that manually.

      Comment

      Working...