initialization makes integer from pointer without a cast
In C, you have to be careful about keeping track of what's a pointer and what's an integer. C will warn you about operations that you probably don't want to do, such as setting an int equal to a string, setting a char equal to a string,confusing your variables and the addresses to the variables, or misspelling null.
Setting a char equal to a string
helloworld.c: In function ‘main’:
helloworld.c:5:12: warning: initialization makes integer from pointer without a cast [enabled by default]
1 #include <stdio.h>
2 #include <string.h>
3 int main()
4 {
5 char c = "character"
6 printf( "Hello world");
7 return 0;
8 }
A char should only be one ascii character, for example 'b' (the character b) or '\n' (the newline character). Combinations of characters form a const char* or a string.
1 #include <stdio.h>
2 #include <string.h>
3 int main()
4 {
5 char c[10] = "character"
6 printf( "Hello world");
7 return 0;
8 }
In this example, we should use a const char* or a string. This may seem like a strange error message for this situation. However, c interprets "character" as a const char * pointer, and a char is an integer type. Essentially, we're trying to set an integer equal to a pointer.
Setting an int equal to a string
helloworld.c: In function ‘main’:
helloworld.c:8:11: warning: initialization makes integer from pointer without a cast [enabled by default]
1 #include <stdio.h>
2 #include <string.h>
3
4 char* foo();
5
6 int main()
7 {
8 int x = foo();
9 return 0;
10 }
11
12 char* foo(){
13 return "foo";
14 }
Strings are pointers, so although C will allow you to set an int equal to a pointer, the int will hold the value of the pointer itself rather than what it's pointing to.
1 #include <stdio.h>
2 #include <string.h>
3
4 char* foo();
5
6 int main()
7 {
8 char* x = foo();
9 return 0;
10 }
11
12 char* foo(){
13 return "foo";
14 }
Although my code runs and compiles, "x" holds the value of the location of the pointer instead of the string "foo". When you set a variable equal to a function, make sure the type of the variable matches the type of the function's return value.
Incorrect use of null
helloworld.c: In function ‘main’:
helloworld.c:5:11: warning: initialization makes integer from pointer without a cast [enabled by default]
1 #include <stdio.h>
2
3 int main()
4 {
5 int x = NULL;
6 return 0;
7 }
NULL is defined as ((void *)0), so it should only be used for pointers.
1 #include <stdio.h>
2
3 int main()
4 {
5 int x = 0;
6 return 0;
7 }
In this example, I really just wanted to set x to 0.
Confusing addresses and variables
helloworld.c: In function ‘main’:
helloworld.c:6:14: warning: initialization makes pointer from integer without a cast [enabled by default]
1 #include <stdio.h>
2
3 int main()
4 {
5 int foo = 4;
6 int* bar = foo;
7 printf("bar stores location %d\n", bar);
8 printf("bar points to value %d\n", *bar);
9 printf("foo stores value %d\n", foo);
10 printf("foo is stored at %d\n", &foo);
11 return 0;
7 }
A pointer should be assigned the address of what you'd like it to point to, not the variable itself. You can get the address of a variable by putting an ampersand before the variable's name.
1 #include <stdio.h>
2
3 int main()
4 {
5 int foo = 4;
6 int* bar = &foo;
7 printf("bar stores location %d\n", bar);
8 printf("bar points to value %d\n", *bar);
9 printf("foo stores value %d\n", foo);
10 printf("foo is stored at %d\n", &foo);
11 return 0;
7 }
The ampersand before "foo" means that bar stores the address of foo, not foo itself.