cast to pointer from integer of different size

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

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.


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);
}
链接地址: http://www.djcxy.com/p/28388.html

上一篇: ghc armv7 binary + cabal? 非法指令

下一篇: 从不同大小的整数转换为指针