0

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

ah ok, i was not quit sure what was causing the warning and how i was supposed to resolve the issue. thank you
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.