cast to pointer from integer of different size - c

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

Related

Print the addresses of all the bytes occupied by an integer variable

Here's the code -
#include <stdio.h>
int main()
{
char character_1 = '0';
int integer_1 = 12321;
char character_2 = '1';
char character_3 = '2';
printf("Integer occupies %zu byte(s) of space.\n",sizeof(int));
printf("Address of Integer 1: %p\n",(void*)&integer_1);
printf("\n");
printf("Character occupies %zu byte(s) of space.\n",sizeof(char));
printf("Address of Character 1: %p\n",(void*)&character_1);
printf("Address of Character 2: %p\n",(void*)&character_2);
printf("Address of Character 3: %p\n",(void*)&character_3);
printf("\n");
return 0;
}
and, the generated output -
Integer occupies 4 byte(s) of space.
Address of Integer 1: 000000000061FE18
Character occupies 1 byte(s) of space.
Address of Character 1: 000000000061FE1F
Address of Character 2: 000000000061FE17
Address of Character 3: 000000000061FE16
I want to print the addresses of all the four bytes of space occupied by the integer variable integer_1, which means print all four of these - 000000000061FE18, 000000000061FE19, 000000000061FE1A and 000000000061FE1B. How do I do it?
Is this what you are trying to do?
#include <stdio.h>
int main()
{
int integer_1 = 12321;
unsigned char* p = (unsigned char*)&integer_1;
for (int i=0; i<sizeof(int); i++){
printf("Address: %p -> Value: %02hhx\n", p+i, *(p+i));
}
return 0;
}
EDIT: As pointed out by KPCT, working with void* is indeed possible, just a bit more tedious if you are also interested in the value pointed, and not the address only.
For example, adapting my above solution to use void*, would result in something like this
#include <stdio.h>
int main()
{
int integer_1 = 12321;
void* p = (void*)&integer_1;
for (int i=0; i<sizeof(int); i++){
printf("Address: %p -> Value: %02hhx\n", p+i, *((char*) p+i));
}
return 0;
}
where you would have to go through the cast to char* anyway
You would need to cast the int pointer to a char pointer (or an int8_t pointer), then step through each of the bytes, something like this:
char *cp = (char*)&integer_1;
for (int i = 0; i < sizeof(integer_1); ++i)
printf("Address of integer_1, byte %d: %p\n",i,cp++);

A basic question trying to make a structure

What I'm trying to do is get student's name and score in three subjects in the form of a structure array and print their name and average score.
Where is my mistake?
#include <stdio.h>
typedef struct
{
char name[5];
int Kor; //yeah the three subjects
int Eng;
int Math;
}data;
double avg(int a, int b, int c) //to get the average
{
double m;
m = (a + b + c) / 3;
return m;
}
int main()
{
data group[3];
for (int i = 0; i < 3; i++)
{
scanf("%s %d %d %d", group[i].name, group[i].Kor, group[i].Eng, group[i].Math);
}
for (int j = 0; j < 3; j++)
{
printf("%s %lf\n", group[j].name, avg(group[j].Kor, group[j].Eng, group[j].Math));
}
return 0;
}
One thing you should make sure and do is compile with most/all compiler warning flags on. In your case, when I compiled your program with GCC, using the flags -W -Wall -Wextra, I got the following warnings:
<source>: In function 'main':
<source>:23:20: warning: format '%d' expects argument of type 'int *', but argument 3 has type 'int' [-Wformat=]
23 | scanf("%s %d %d %d", group[i].name, group[i].Kor, group[i].Eng, group[i].Math);
| ~^ ~~~~~~~~~~~~
| | |
| int * int
and the same warning for group[i].Eng and group[i].Math.
These compiler warnings are very often actually errors of yours, which, at runtime, will result in the program crashing or producing garbage. In your case, you need to pass the address of the value you want to read from the input.
This does not mean that's the only issue with your code, but you should really let the compiler help you before asking us for help.
I change the scanf, '&' operator is used to access to the address in the memory location. scanf("%d",&a) means that the value entered from the keyboard must be stored in the memory LOCATION where which is given the name 'a'.
and change the calculating function of the average because you are using the integer division version of operator/, which takes 2 ints and returns an int. In order to use the double version, which returns a double, at least one of the ints must be casted to a double.
#include <stdio.h>
typedef struct
{
char name[5];
int Kor; //yeah the three subjects
int Eng;
int Math;
}data;
double avg(int a, int b, int c) //to get the average
{
double m;
m = (a + b + c) / 3.0;
return m;
}
int main()
{
data group[3];
for (int i = 0; i < 3; i++)
{
printf("enter your name ,Kor grade ,Eng grade ,Math grade\n");
scanf("%s %d %d %d", group[i].name, &group[i].Kor, &group[i].Eng, &group[i].Math);
}
for (int j = 0; j < 3; j++)
{
printf("name:%s avg:%lf\n", group[j].name, avg(group[j].Kor, group[j].Eng, group[j].Math));
}
return 0;
}

C ,help me in understanding this ASCII prob

This code will print:
s = 1, i = 65537, f = 65537.000000, c = 1
I need help in understanding why is it printing c=1.
The code:
#include <stdio.h> // Standard input-output library
#include <stdlib.h> // Standard general utilities library
int main(void) {
int i = 65537;
unsigned short s = (unsigned short)i;
float f = (float)i;
char c = (char)i;
printf("s = %u, i = %d, f = %f, c = %d\n", s,i,f,c);
system("PAUSE");
return (0);
}
If to output the variable i in hex representation like this
#include <stdio.h>
int main(void)
{
int i = 65537;
printf( "%#0x\n", i );
return 0;
}
then you will see that it looks like
0x10001
Type char always occupies one byte. Thus in this assignment
char c = (char)i;
one byte of the object i
0x10001
^^
is stored in the variable c.
In the printf statement in your program
printf("s = %u, i = %d, f = %f, c = %d\n", s,i,f,c);
^^^^^^
it is outputted using type specifier %d. So its internal value is outputted as an integer of type int.
Usually objects of the type unsigned short occupy two bytes. Thus in this assignment
unsigned short s = (unsigned short)i;
the value in the first two bytes of object i
0x10001
^^^^
will be assigned to variable s.
So c and s the both will have value 1.

iso c90 forbids mixed declarations and code

This is my coding and I am getting the above error in the line where i declared int *intpointer. Please help me in solving this.
#include <stdio.h>
int main()
{
int intarray[5] = {10,20,30,40,50};
int i;
for(i = 0; i < 5; i++)
printf("intarray[%d] has value: %d - and address # %x\n", i, intarray[i], &intarray[i]);
int *intpointer = &intarray[4];
printf("address: %x - has value %d\n", intpointer, *intpointer);
intpointer--;
printf("address: %x - has value %d\n", intpointer, *intpointer);
return 0;
}
Place this declaration
int *intpointer = &intarray[4];
in the beginning of the function code block after the declaration of intarray.
As error message reports you compile your code as C90 code that requires that declarations would be before other statements.

How can I test this C function

I got a weird question to do as an exercise :
Write a function which take a pointer of a pointer of a pointer of a pointer of a pointer of a pointer of a pointer of a pointer of a pointer of an int as a parameter and assign a value to it.
I think the function I wrote is right (please correct if it's not) but how can I test it ?
void function(int *********anInt)
{
*********anInt = 5;
}
I tried :
int main(void) {
int *********nbr = malloc(sizeof(int));
function(nbr);
printf("%d", *********nbr);
}
But I get a segfault, I just learned about malloc and pointers so I don't fully understand it.
Of course, you can test it, although it looks weird.
#include <stdio.h>
void function(int *********anInt)
{
*********anInt = 5;
}
int main()
{
int n = 0;
int *p1 = &n;
int **p2 = &p1;
int ***p3 = &p2;
int ****p4 = &p3;
int *****p5 = &p4;
int ******p6 = &p5;
int *******p7 = &p6;
int ********p8 = &p7;
function(&p8);
printf("%d\n", n);
return 0;
}
Try
int main() {
int *********nbr;
nbr = malloc(sizeof(int********));
*nbr = malloc(sizeof(int*******));
**nbr = malloc(sizeof(int******));
***nbr = malloc(sizeof(int*****));
****nbr = malloc(sizeof(int****));
*****nbr = malloc(sizeof(int***));
******nbr = malloc(sizeof(int**));
*******nbr = malloc(sizeof(int*));
********nbr = malloc(sizeof(int));
function(nbr);
printf("%d", *********nbr);
}
You'll need a ridiculous main program to go with the assignment from hell!
int main(void)
{
int l0 = 0;
int *l1 = &l0;
int **l2 = &l1;
int ***l3 = &l2;
int ****l4 = &l3;
int *****l5 = &l4;
int ******l6 = &l5;
int *******l7 = &l6;
int ********l8 = &l7;
printf("%d %d %d %d %d %d %d %d %d\n", l0, *l1, **l2, ***l3, ****l4, *****l5,
******l6, *******l7, ********l8);
function(&l8);
printf("%d %d %d %d %d %d %d %d %d\n", l0, *l1, **l2, ***l3, ****l4, *****l5,
******l6, *******l7, ********l8);
return 0;
}
Untested: maybe I didn't count something right, but the general idea is about correct. This is a torture test — for innocent C programmers and for compilers.
An int** is a pointer that points to a pointer:
int myInt;
int* pInt = &myInt;
int** ppInt = &pInt;
An int*** is a pointer that points to a pointer that points to a pointer:
int*** pppInt = &ppInt;
To test your function, you need to carry this on and on, the right number of times.
See md5's solution, however it lacks explaination
Explained:
The reason your test program didn't work is because malloc returns a void* which is simply a memory address (a pointer). You assigned this to an int*****... which means when the program tries to dereference down to the actual int what it's doing is first taking the memory address of the int and dereferencing it (which is okay) but after this since your value (5) is now the value it then derefences that, which should come back with your segfault.
Think of the assignment as nested dereferences:
int ********p8 = *anInt; // p8 == 5
int *******p7 = *p8; // This breaks since dereferencing memory
// address 5 results in a segfault
What was done to avoid this was we actually nested the pointers that way when dereferencing for assignment we have memory addresses (pointers) to dereference to eventually get to the memory address which stores the value.

Resources