assignment makes pointer from integer without a cast

Discussion in 'C Programming' started by Zach, May 5, 2009.

  1. Zach

    Zach Guest

    This runs with the expected results, but I would like to understand
    this warning and eliminate it. If I change lines 38 and 39 to be:

    test.prev = *a;
    test.next = *b;

    Then it compiles with no warnings but it prints out garbage values for
    a and b after I call get_token().

    Zach

    chaos@cobalt:~/myprojects/netrek-log-parser$ gcc -o ll ll.c
    ll.c: In function ‘get_token’:
    ll.c:38: warning: assignment makes pointer from integer without a cast
    ll.c:39: warning: assignment makes pointer from integer without a cast
    chaos@cobalt:~/myprojects/netrek-log-parser$ ./ll
    Test 1 2
    Test 1 2


    #include <stdio.h>
    #include <stdlib.h>

    typedef struct
    {
    char *data;
    int *prev, *next;
    }node;

    node test;

    node get_token(char *, int *, int *);

    int main(void)
    {

    char *p = "Test";

    int *a = malloc(sizeof(int));

    int *b = malloc(sizeof(int));

    *a = 1;
    *b = 2;

    printf("%s %d %d\n",p,*a,*b);

    get_token(p,a,b);

    printf("%s %d %d\n",test.data,test.prev,test.next);

    return EXIT_SUCCESS;
    }

    node get_token(char *p, int *a, int *b)
    {
    test.data = p;
    test.prev = *a;
    test.next = *b;

    free(a);
    free(b);

    return test;
    }
    Zach, May 5, 2009
    #1
    1. Advertising

  2. Zach

    Zach Uram Guest

    On May 5, 7:36 am, Zach <> wrote:
    > This runs with the expected results, but I would like to understand
    > this warning and eliminate it. If I change lines 38 and 39 to be:
    >
    > test.prev = *a;
    > test.next = *b;


    Sorry I meant if I change them to be:

    test.prev = a;
    test.next = b;

    Zach
    Zach Uram, May 5, 2009
    #2
    1. Advertising

  3. Zach

    James Kuyper Guest

    Zach wrote:
    > This runs with the expected results, but I would like to understand
    > this warning and eliminate it. If I change lines 38 and 39 to be:
    >
    > test.prev = *a;
    > test.next = *b;
    >
    > Then it compiles with no warnings but it prints out garbage values for
    > a and b after I call get_token().
    >
    > Zach
    >
    > chaos@cobalt:~/myprojects/netrek-log-parser$ gcc -o ll ll.c
    > ll.c: In function ‘get_token’:
    > ll.c:38: warning: assignment makes pointer from integer without a cast
    > ll.c:39: warning: assignment makes pointer from integer without a cast
    > chaos@cobalt:~/myprojects/netrek-log-parser$ ./ll
    > Test 1 2
    > Test 1 2
    >
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > typedef struct
    > {
    > char *data;
    > int *prev, *next;
    > }node;
    >
    > node test;
    >
    > node get_token(char *, int *, int *);
    >
    > int main(void)
    > {
    >
    > char *p = "Test";
    >
    > int *a = malloc(sizeof(int));
    >
    > int *b = malloc(sizeof(int));


    Always check to make sure that any value returned by malloc() is not
    null. If either a or b are null pointers at this point, the following
    lines cause your program to have undefined behavior:

    > *a = 1;
    > *b = 2;
    >
    > printf("%s %d %d\n",p,*a,*b);
    >
    > get_token(p,a,b);


    The following line has problems due to what get_token() has done. I'll
    explain further when I get to the definition of get_token.

    > printf("%s %d %d\n",test.data,test.prev,test.next);


    However, this line also has it's own purely internal problems. %d is for
    printing integers. test.prev and test.next are pointers to int. If
    you're trying to print the value that test.prev points at, use %d and
    *test.prev. If you want to print out the value of the pointer test.prev,
    use %p and (void*)test.prev.

    > return EXIT_SUCCESS;
    > }
    >
    > node get_token(char *p, int *a, int *b)
    > {
    > test.data = p;
    > test.prev = *a;


    test.prev is a pointer to int. *a is an int. That's your first key
    problem, and that's why your solution worked.

    > test.next = *b;


    Similarly.

    > free(a);
    > free(b);


    That's you're second key problem.
    Calling free(a) deallocates the block of allocated memory pointed at by
    a. You've just discarded the numbers you wrote into that memory.

    Also, calling free(a) renders the value all pointers into that block of
    memory indeterminate. That means that there no longer anything you can
    usefully do with the pointers that you've so carefully stored in
    test.prev and test.next. You can't dereference them. You can't compare
    them for equality with other pointers. Almost the only useful thing you
    can do with them is replace them with a new, valid pointer value.

    You should never free() memory until you're done using it, and you
    apparently want to continue using it at this point.

    > return test;
    > }
    James Kuyper, May 5, 2009
    #3
  4. Zach

    Guest

    On 5 May, 12:36, Zach <> wrote:
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > typedef struct
    > {
    >     char *data;
    >     int *prev, *next;


    You need to decide whether you really want to store pointers to ints
    here, or what you actually want to store are the ints themselves. If
    you do indeed want pointers, then James Kuyper has explained what you
    need to do. If you actually want ints (which I am guessing is the
    case, but I could be wrong) then you want to change the line above to:

    int prev, next;

    which at a quick glance seems to solve all the other problems (except
    checking the return value from malloc).

    > }node;
    >
    > node test;
    >
    > node get_token(char *, int *, int *);
    >
    > int main(void)
    > {
    >
    > char *p = "Test";
    >
    > int *a = malloc(sizeof(int));
    >
    > int *b = malloc(sizeof(int));
    >
    > *a = 1;
    > *b = 2;
    >
    > printf("%s %d %d\n",p,*a,*b);
    >
    > get_token(p,a,b);
    >
    > printf("%s %d %d\n",test.data,test.prev,test.next);
    >
    > return EXIT_SUCCESS;
    >
    > }
    >
    > node get_token(char *p, int *a, int *b)
    > {
    > test.data = p;
    > test.prev = *a;
    > test.next = *b;
    >
    > free(a);
    > free(b);
    >
    > return test;
    > }
    , May 5, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rocke Robertson
    Replies:
    14
    Views:
    12,977
    Dan Pop
    Oct 2, 2003
  2. Markus Dehmann
    Replies:
    14
    Views:
    921
    Irrwahn Grausewitz
    Jun 28, 2004
  3. Dawn Minnis
    Replies:
    4
    Views:
    1,533
    Dawn Minnis
    Feb 20, 2005
  4. Replies:
    2
    Views:
    1,226
    Martin Ambuhl
    Apr 1, 2005
  5. Juggernaut
    Replies:
    10
    Views:
    1,594
    CBFalconer
    Apr 9, 2005
Loading...

Share This Page