I have this structure and using pointers , I am trying to print the values of different structure variables , but for int variables it prints location address instead of value and for char variables the result is correct.
#include <stdio.h>
#include <stdlib.h>
struct calc{
int row;
int col;
int menu_name[20];
int sub_menu_name[20];
};
int main()
{
int count = 0, i = 0;
struct calc *P_calc[2];
//p_calc = (struct calc *)malloc(sizeof(struct calc)*2);
for(count; count<1; count++)
{
P_calc[count] = (struct calc *)malloc(sizeof(struct calc));
printf("Please enter the row cordinates: \n");
scanf("%d",P_calc[i]->row);
printf("Please enter the col cordinates: \n");
scanf("%d",P_calc[i]->col);
printf("Please enter the menu_name: \n");
scanf("%s",P_calc[i]->menu_name);
printf("Please enter the sub_menu_name: \n");
scanf("%s",P_calc[i]->sub_menu_name);
}
for(i; i<1; i++)
{
printf("row : %d\n",P_calc[i]->row);
printf("col :%d\n",P_calc[i]->col);
printf("menu_name: %s\n",P_calc[i]->menu_name);
printf("sub_menu_name :%s\n",P_calc[i]->sub_menu_name);
}
system("PAUSE");
return 0;
}
Please Help Me.
Thanks in Advance.
Here are your problems:
The structure should be
struct calc{ int row; int col; char menu_name[20]; char sub_menu_name[20]; };
scanf("%d",P_calc[i]->row);
should be scanf("%d",&P_calc[i]->row);
scanf("%d",P_calc[i]->col);
should be scanf("%d",&P_calc[i]->col);
It is also a good idea to check the return value from scanf
.
The scanf statement should be
scanf("%d",&P_calc[i]->row);
The address needs to be passed to scanf. Similarly while reading the col variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.