0

我有这个结构并使用指针,我试图打印不同结构变量的值,但是对于 int 变量,它打印位置地址而不是值,对于 char 变量,结果是正确的。

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

请帮我。

提前致谢。

4

2 回答 2

3

以下是您的问题:

  1. 结构应该是

    struct calc{
    
    int row;
    int col;
    char menu_name[20];
    char sub_menu_name[20]; 
    
    };
    
  2. scanf("%d",P_calc[i]->row);应该scanf("%d",&P_calc[i]->row);

  3. scanf("%d",P_calc[i]->col);应该scanf("%d",&P_calc[i]->col);

检查来自 的返回值也是一个好主意scanf

于 2013-03-13T05:50:45.280 回答
0

scanf 语句应该是

scanf("%d",&P_calc[i]->row);

地址需要传递给scanf。同样在读取 col 变量时。

于 2013-03-13T05:49:37.633 回答