我有这个结构,我想我可以设置条件,如果结构指向一个NULL值。通过一个数组结构循环
这里是我的这个例子结构简单:
typedef struct
{
char *name;
char *data;
} details_t;
details_t emp_details [] =
{
{ "peter", "lawyer" },
{ "john", NULL }, /* No data for john */
{ NULL, NULL }, /* Indicates last element in the array */
};
我想我应该递增emp_details阵列和取消引用指针,看它是否包含在第一个数组元素的NULL。但不知道我是否正朝着正确的方向前进。
for(i=i; *emp_details; i++)
{
printf("Name: [ %s ] [ %s ]\n", emp_details[i].name, emp_details[i].data);
}
我可以建议使用' - >'而不是'(*).'吗? – mouviciel 2009-10-30 08:49:39
除了使用' - >'代替'(*).'之外,在尝试使用它之前不要忘记检查'it-> data!= NULL'。否则“约翰”可能会导致'printf()'(或其他)废话。 – 2009-10-30 09:14:11
@Chris Jefferson为什么'int size_of_array = sizeof(emp_details)/ sizeof(details_t);'脆弱?你想分享的任何个人经验? – 2009-10-30 10:58:43