关于指针数组的有关问题 , 下面这个指针数组的应用为什么不能这么写
关于指针数组的问题 , 下面这个指针数组的应用为什么不能这么写?
------解决方案--------------------
indiv变量楼主定义的一维数组,而output函数的参数是数组指针,类型不一致呢。
------解决方案--------------------
# 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函数的参数是数组指针,类型不一致呢。
------解决方案--------------------
# 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);