assignment makes pointer from integer without a cast

Z

Zach

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;
}
 
Z

Zach Uram

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
 
J

James Kuyper

Zach said:
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.
 
G

gw7rib

#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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top