Reputation: 21
I keep getting this warning
c:9:80: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
printf("Char= %c ASCII = %i hex = %x pointer = %p \n", i, i, i , (void*)i );
Code
#include<stdio.h>
int main (void) {
int *i;
for (int i = 75; i < 75 + 26; i++) {
printf("Char= %c ASCII = %i hex = %x pointer = %p \n", i, i, i , (void*)i );
}
return(0);
}
Upvotes: 0
Views: 1374
Reputation:
You’re getting the warning because the variable “i” is declared twice in the same scope. The memory address of ‘i’ in your loop doesn’t change so what do you need the pointer outside the loop for?
#include<stdio.h>
int main (void) {
for (int i = 75; i < 75 + 26; i++) {
printf("Char= %c ASCII = %i hex = %x pointer = %p \n", i, i, i , &i );
}
return(0);
}
or yet still if you still want to have two variables.
#include<stdio.h>
int i;
int *j = &i;
int main (void) {
for ( i = 75; i < 75 + 26; i++) {
printf("Char= %c ASCII = %i hex = %x pointer = %p \n", i, i, i , (void *)j );
}
return(0);
}
Upvotes: 0
Reputation: 8573
I fail to see what the question might be that is not answered by the compiler warning. You've got a variable "i" of type int
(32 bit on 64 bit platforms), shadowing another variable called "i" in the main program.
You're casting the int
variable to void*
, and the compiler says you can't do that, because you are 32 bit short. Rename one of the two variables called i
in your program to resolve.
Upvotes: 2