Thread: Just a simple warning...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    20

    Just a simple warning...

    I have this warning on a bit of code I can't seem to correct. The code compiles just find but I'd rather not have any warnings. Can anyone correct this?

    198 [Warning] assignment makes pointer from integer without a cast

    Code:
    struct ot_entry {
         char  ot_mnemonic[OP_LENGTH + 1];
         int   ot_format;  
         char  ot_opcode;
    };
    struct  ot_entry  *optbl[OT_SIZE];
    int     ot_count;
    
    int main ( void )
    {
            ...
    
           optbl[ot_count] = malloc ( sizeof ( struct ot_entry ) );    /* line getting the warning */
    
            ...
    
           return 0;
    }

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Put this at the top of your file.
    Code:
    #include <stdlib.h>
    <stdlib.h> contains the prototype for malloc(). The default return type for a function without a prototype is an int.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    I don't understand what your trying to do. you already have
    Code:
    struct  ot_entry  *optbl[OT_SIZE];
    that is an array of OT_SIZE of ptrs to struct ot_entry. so you already have OT_SIZE records to manipulate.

    why do you try to allocate more space - which is really another pointer to the struct - by using the same array you've aready declared
    Code:
    optbl[ot_count] = malloc ( sizeof ( struct ot_entry ) );
    and changing its size?

    post your code, or explain what your trying to do please.

    [edit] if you were just implicitly declaring malloc then....*shrug*[/edit]
    Last edited by caroundw5h; 11-15-2004 at 12:06 PM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I don't understand what your trying to do. you already have
    Code:

    struct ot_entry *optbl[OT_SIZE];


    that is an array of OT_SIZE of ptrs to struct ot_entry. so you already have OT_SIZE records to manipulate.

    why do you try to allocate more space - which is really another pointer to the struct - by using the same array you've aready declared
    Code:

    optbl[ot_count] = malloc ( sizeof ( struct ot_entry ) );

    and changing its size?

    post your code, or explain what your trying to do please.

    [edit] if you were just implicitly declaring malloc then....*shrug*[/edit]
    In the first line he's declaring an array of pointers. In the second, he's presumabely in a loop that is cycling through each of those pointers and setting them equal to the address returned by malloc.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Beaten, but may still be of use. caroundw5h, the code is valid.
    Code:
    struct  ot_entry  *optbl[OT_SIZE];
    This declares an array of pointers to struct ot_entry. These pointers do not point to allocated memory and an attempt to dereference one will result in very bad things.
    Code:
    optbl[ot_count] = malloc ( sizeof ( struct ot_entry ) );
    This allocates enough memory for a struct ot_entry and assigns its address to a pointer variable for later use.

    Arrays can be extremely confusing. Thumper333's code is the array equivalent to:
    Code:
    int* p; /* Declare a pointer. Does not point to valid memory at this point. */
    p = malloc(sizeof(int)); /* Allocate enough memory for an int and assign its address to p. */

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    Thanks anonytmouse, for some odd reason i felt like leaving out the header. That worked though...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The biggest project I've worked on: ASCIIpOrtal
    By guesst in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 07-21-2009, 04:42 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM