我是指针的新手,并尝试使用指向结构的指针。我需要使用指针访问结构参数。请帮助我。
以下是该计划:
#include<stdio.h>
typedef struct{
int a;
int b;
int c;
}time;
typedef struct{
int a;
int b;
time record;
}myarray;
typedef struct{
myarray *ptrtoarray
}access;
myarray n_my_array_first[2] =
{
{1 ,2 , {1 , 2 ,2}},
{100 ,121 , {123,322,65535}}
};
myarray n_my_array_third[2] =
{
{23,44,{1,43,22}},
{23,48,{455,666,999}}
};
access n_access[5] =
{
{&n_myarray_first},
{((void *)0)},
{((void *)0)},
{&n_myarray_third},
{((void *)0)}
};
int main()
{
/* access the record.c parameter of second element of n_my_array_third array.
whose value is 999
I have tried it as :
time v = n_access[3].(n_myarray_third + 1) -> record.c ;
But I am getting an error */
return 0;
}
问题1)解释访问字段可能做些什么。
问2)提出任何更简单的方法来实现同样的事情(通过使用结构内的结构) 如果可能的话。谢谢
答案 0 :(得分:0)
请改为:
time v = (n_access[3].ptrtoarray + 1)->record;
int c = v.c;
为避免复制time
结构,请使用指针
time *v = &(n_access[3].ptrtoarray + 1)->record;
int c = v->c;
更简单的方法?不要一行完成
access* my_access = &n_access[3];
myarray* array = &my_access->ptrtoarray[1];
time *v = &array->record;
int c = v->c;