首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于指针数组的有关问题 , 下面这个指针数组的应用为什么不能这么写

2013-04-26 
关于指针数组的问题 , 下面这个指针数组的应用为什么不能这么写?# include stdio.h# include ctype.h#

关于指针数组的问题 , 下面这个指针数组的应用为什么不能这么写?

# include <stdio.h>
# include <ctype.h>
# define LEN 40

struct name
{
char fname[40];
char mname[40];
char lname[40];
};

struct person
{
long int secnum;
struct name indiv;
};

void output (struct person * hot[], int num);
int main (void)
{
struct person indiv[5] = {
{2342, "hiwe", "wer", "dfsf"},
{2342, "fie", "dwr", "dwr"},
{3452, "dhfie", "fe", "fwf"},
{23, "dw", "dw", "fw"},
{23, "ds", "as", "dwa"}
};
output (indiv, 5);

return 0;
}

void output (struct person * hot[], int num)
{
int i;
for (i = 0; i < num; i++)
{
hot[i]->indiv.fname[1] = toupper (hot[i]->indiv.fname[1]);
hot[i]->indiv.lname[1] = toupper (hot[i]->indiv.lname[1]);
if ( isalpha ( hot[i]->indiv.mname[1] ) )
hot[i]->indiv.mname[1] = toupper (hot[i]->indiv.mname[1]);
printf ("%s, %s %c. - %d", hot[i]->indiv.fname, hot[i]->indiv.lname, 
hot[i]->indiv.mname[1], hot[i]->secnum);
}
return ;

}

[解决办法]
struct person indiv[5]; void output (struct person * hot[], int num)

indiv变量楼主定义的一维数组,而output函数的参数是数组指针,类型不一致呢。
[解决办法]
引用:
引用:因为类型不一样,output的形参是person*类型的数组,而你传递的实参为person类型的数组。
那怎样修改了?大神。。


# include <stdio.h>
# include <ctype.h>
# define LEN 40

struct name
{
char fname[40];
char mname[40];
char lname[40];
};

struct person
{
long int secnum;
struct name indiv;
};

void output (struct person * hot[], int num);
int main (void)
{
struct person indiv[5] = {
{2342, "hiwe", "wer", "dfsf"},
{2342, "fie", "dwr", "dwr"},
{3452, "dhfie", "fe", "fwf"},
{23, "dw", "dw", "fw"},
{23, "ds", "as", "dwa"}
};
struct person *a[5] = {&indiv[0],&indiv[1],&indiv[2],&indiv[3],&indiv[4]};
output (a, 5);

return 0;
}

void output (struct person * hot[], int num)
{
int i;
for (i = 0; i < num; i++)
{
hot[i]->indiv.fname[1] = toupper (hot[i]->indiv.fname[1]);
hot[i]->indiv.lname[1] = toupper (hot[i]->indiv.lname[1]);
if ( isalpha ( hot[i]->indiv.mname[1] ) )
hot[i]->indiv.mname[1] = toupper (hot[i]->indiv.mname[1]);
printf ("%s, %s %c. - %d", hot[i]->indiv.fname, hot[i]->indiv.lname, 
hot[i]->indiv.mname[1], hot[i]->secnum);


}
return ;

}

热点排行