Thread: array of pointer question

  1. #1
    Unregistered
    Guest

    Question array of pointer question

    Hi, could someone please tell me why this produces warning

    'warning assignment makes integer from pointer without cast' on line 25

    all its supposed to do is set up an array of pointers to integers and then initialise them all to NULL. :-(

    code:

    int main(void)
    {

    int *c,n;

    c = (int *) malloc(sizeof(int *) * 5);

    for(n=0; n<5; n++)
    {
    c[n] = NULL;
    }

    }


    cheers Bob;

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    NULL is defined as a void pointer to 0, for example:
    #define NULL ( ( void * ) 0 )

    To remove the warning you can either cast NULL as (int), or just use the constant 0 instead of the macro, or even the NUL character '\0' if you want.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    Ok, I see how that works by casting the NULL pointer to an integer. However I dont understand why I cant cast the NULL pointer to anything (as it is void?)

    The following code should initalise an array of these structures to NULL but I get the error
    'incompatible types in assignment'
    when making the array slots equal NULL

    As NULL points to 0 can it not only be an int pointer?

    struct id
    {
    int id;
    struct id *next; /* Pointer to next record */
    };


    int main(void)
    {

    struct id *c;
    int n;

    c = (struct id *) malloc(sizeof(struct id *) * 5);

    for(n=0; n<4; n++)
    {
    c[n] = (struct id *) NULL;
    }

    }

  4. #4
    Unregistered
    Guest
    Ok think I understand now, is it because maloc sets up an array of pointers. To initialise them to null you have to make the pointers go to null (not the arrray slots)
    so it would be

    for(n=0; n<4; n++)
    {
    c[n] = *((struct id *) NULL);
    }

    this compiles but could someone tell me if the thinking behind it is right, cheers

    Bob

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >c = (struct id *) malloc(sizeof(struct id *) * 5);

    Sure this is right? I assume you want an array of 5 elements where each element is of type struct id. If I am correct that case you need to do:

    c = (struct id *) malloc (sizeof (struct id) * 5);

    Since sizeof (struct id *) is the size of the pointer and you want the size of the structure.

    >for(n=0; n<4; n++)
    >{
    > c[n] = (struct id *) NULL;
    >}

    I can imagine someone assigning a pointer to NULL, but not a variable like c]n].

  6. #6
    Unregistered
    Guest
    Hi,

    Thanks for the reply,

    >Sure this is right? I assume you want an array of 5 elements
    >where each element is of type struct id. If I am correct that case
    >you need to do:


    I am after an array of pointers to the structure. This is because I dont want to have to allocate memory for the structures in a very large array (that program was just an example, the final one uses a larger array) as it will take up valuable resources I need for something else!

    The idea was that I could then malloc memory for the structures as I needed them and make the array pointer equal to the return from malloc.

    Could someone tell me if the method I'm using in the code up above is correct or if I'm making it up as I go along, (cause it feels like that :-)

    Cheers

    Bob.

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    If you really want to use memory only when necessary, you shouldn't use an array, but a linked list. You allocate memory for a node when needed and free memory when the node is not needed anymore.

  8. #8
    Unregistered
    Guest
    Hi again,

    It's the start of a hashing table!!! which involves linked lists :-)

    Unfortunately the actuall structures that will be put in it are very large so trying to do them as efficently as possible (dodgy statitistics!)

    I was just wondering if my reasoning behind the pointers was right for this implementation!

    if you could suggest a better way to optimise this it would be much appreciated, (or if this method is correct?)

    Cheers again

    Bob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer of array of class
    By 11moshiko11 in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2008, 09:58 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. array pointer question
    By Hoser83 in forum C Programming
    Replies: 5
    Last Post: 02-03-2006, 11:19 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. A question between Array and Pointer
    By Unregistered in forum C++ Programming
    Replies: 16
    Last Post: 08-05-2002, 09:13 AM