Reputation: 73
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.
Upvotes: 0
Views: 548
Reputation: 60007
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
.
Upvotes: 3
Reputation: 2031
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.
Upvotes: 0