嗨 friend ,我是新来的,正在尝试学习结构...这里我在结构计算中声明了结构日期...但不知道如何从日期访问元素。我通过使用 malloc 为父结构计算保留了内存。这对于日期结构也足够吗? .请指导我...谢谢!
#include <stdio.h>
#include <stdlib.h>
struct date{
int day;
int month;
int year;
};
struct calc{
int row;
int col;
char menu_name[20];
char sub_menu_name[20];
struct date dob;
};
int main()
{
int count = 0, i;
struct calc *my_calc[2];
//here unable to understand that where i need to resever seprate memory for date?
my_calc[0] = (struct calc *)malloc(sizeof(struct calc));
//trying to asign the date value
for(count; count<2; count++)
{
printf("Please enter day: ");
scanf("%d",&my_calc[count]->date.day);
printf("Please enter month: ");
scanf("%d",&my_calc[count]->date.month);
printf("Please enter Year: ");
scanf("%d",&my_calc[count]->date.year);
}
//trying to print the date value
printf("Day: %d\t Month: %d\t Year: %d\n ",my_calc[0]->date.day,my_calc[0]->date.month,my_calc[0]->date.year);
system("PAUSE");
return 0;
}
最佳答案
您声明的是dob
而不是date
。 &my_calc[count]->dob.day
关于c - 在结构中使用结构..无法访问内部结构元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15383227/