array of pointers in struct

This is a discussion on array of pointers in struct within the C Programming forums, part of the General Programming Boards category; Here's a small piece of code that I'm having some trouble with. Code: typedef struct my_struct { ... int *current_items; ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    69

    array of pointers in struct

    Here's a small piece of code that I'm having some trouble with.

    Code:
    typedef struct my_struct {
        ... 
        int *current_items;
    } my_struct;
    
    my_struct *s = malloc(sizeof(my_struct));
    s->current_items = malloc(5 * sizeof(int));
    I get a compile error stating that assignment makes integer from pointer without a cast. Is this happening because I'm not casting malloc? I've seen comments on this board instructing people not to cast from malloc, so I'm a little bit lost as to what I'm doing wrong here.

    Also, when I attempt to compare a pointer to a NULL value in the following manner:
    Code:
    if (s->current_items[1] == NULL) {
        ....
    }
    Or this:

    Code:
    s->current_items[1] = *p;
    I also get compile errors. Do I need to cast here, or am I totally off?

    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    You get "makes pointer from integer without a cast" from a malloc call, typicially by failing to include stdlib.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    The boards are right in that you shouldn't cast malloc, because it typically hides the warning that you're doing an implicit call to malloc which is very bad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    if (s->current_items[1] == NULL) {
        ....
    }
    Well, yes this would give compiler warning. Since you are comparing a NULL pointer with the integer. And the second example as well.

    Code:
    s->current_items[1] = *p;
    You are trying to assign a pointer to a integer array element. Which is wrong again unless the array was declared as an array of integer pointers.

    ssharish

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    OK - I'm a little bit confused. Why do I get a compiler warning with the following code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int
    main(int argc, char *argv[]) {
      
      int foo[5];
      int *i = malloc(sizeof(int));
    
      foo[0] = (int) i;
      printf("%d\n", foo[0]);
    
      return 0;
    }
    warning: assignment makes integer from pointer without a cast

    When I explicitly cast the pointer to an integer, the warning goes away. Isn't the pointer supposed to be an integer? Why does this happen?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    No, a pointer is not an integer. It's a pointer, and a pointer is a variable that contains an address. Do not cast anything unless you know what you're doing.
    You need to dereference the pointer to get the value at the address:
    foo[0] = *i;
    http://cpwiki.sf.net/A_pointer_on_pointers
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Isn't the pointer supposed to be an integer?
    No, a pointer is not an integer. It does not make sense to cast a pointer to int. What are you trying to do?

    Note that you did not free() what you malloc()ed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    Quote Originally Posted by laserlight View Post
    No, a pointer is not an integer. It does not make sense to cast a pointer to int. What are you trying to do?
    I'm trying to create an array of pointers. I'm obviously way off base here.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    But that's easy.
    Code:
    int* foo[5];
    A pointer is a type and you can create an array of any type (except void).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Notice all of the malloc calls, Elysia? . . . .

    Maybe you want something like this.
    Code:
    int **data = malloc(N * sizeof(*data));
    Now data[0] through data[N-1] are valid pointers. If you want to make each of those pointers point to dynamically allocated memory as well, you could use something like this:
    Code:
    int **data = malloc(N * sizeof(*data));
    size_t x;
    
    for(x = 0; x < N; x ++) {
        data[x] = malloc(sizeof(**data));
    }
    Note that sizeof(*data) is just like sizeof(int *), because data is an int **; and sizeof(**data) is like sizeof(int). It's a bit easier to change the type of a variable if you use it in the sizeof, however, instead of the type itself directly.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  3. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 10:50 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21