Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用数组指针打印stuct成员?_C_Arrays_Function_Pointers_Struct - Fatal编程技术网

如何使用数组指针打印stuct成员?

如何使用数组指针打印stuct成员?,c,arrays,function,pointers,struct,C,Arrays,Function,Pointers,Struct,到目前为止,我编写的代码如下,没有库。基本上我使用了stdio.h和stdlib.h typedef struct ID{ char firstName[21]; char lastName[21]; char phoneNumber[11]; }ID; ID PrintList(ID *ptr,int i){ int l=1; printf(" # First Name Last Name Phone Number

到目前为止,我编写的代码如下,没有库。基本上我使用了stdio.h和stdlib.h

typedef struct ID{
     char firstName[21];
     char lastName[21];
     char phoneNumber[11];
}ID;

ID PrintList(ID *ptr,int i){
     int l=1;
     printf(" #   First Name       Last Name       Phone Number\n");
     while(l<i+1)
     {
        printf(" %d.   %s",l,&ptr[l]);
        l++;
    }
} 

ID addNew(ID *ptr,int i){
    char fname[21],lname[21],phone[11];
    if(i<3)
     {  
        ID user;
        ptr[i] = user;
        printf("enter the first name:   ");
        scanf("%s",&fname);
        *ptr->firstName= fname;
        printf("enter the last name:    ");
        scanf("%s",&lname);
        *ptr->lastName = lname;
        printf("enter the phone number: "); 
        scanf("%s",&phone);
        *ptr->phoneNumber = phone;
    }
    else
    {
        printf("sorry but you have reach max capacity\n");  
    }
} 

int main(int argc, char *argv[]) {
    int answere,i=0;
    printf("******* WELCOME TO PHONEBOOK **********\n\n\n");
    printf("***************************************\n");
    printf("*                MENU                 *\n");
    printf("*                                     *\n");
    printf("* 1.Add New   2.Print list     3.Exit *\n\n");
    printf("***************************************\n\n");

    ID* ptr=(int*)malloc(21*sizeof(ID));
    do
    {
        printf("Please select (1, 2 or 3): ");
        scanf("%d",&answere);
        if(answere!=1 && answere!=2 && answere!=3)
        {
            printf("...Error... \ngive me a correct answere\n");
        }
        if(answere == 1)
        {
            i++;
            addNew(ptr,i);
        }
        else if(answere==2)
        {
            PrintList(ptr,i);
        }
    }while(answere!=3); 

    return 0;
}
typedef结构ID{
charfirstname[21];
char lastName[21];
字符电话号码[11];
}身份证;
ID打印列表(ID*ptr,int i){
int l=1;
printf(“#名字姓氏电话号码”);
而(llastName=lname;
printf(“输入电话号码:”);
scanf(“%s”和电话);
*ptr->phoneNumber=电话;
}
其他的
{
printf(“很抱歉,您已达到最大容量\n”);
}
} 
int main(int argc,char*argv[]){
int answere,i=0;
printf(“*******欢迎使用电话簿************\n\n\n”);
printf(“*********************************************************\n”);
printf(“*菜单*\n”);
printf(“***\n”);
printf(“*1.添加新2.打印列表3.退出*\n\n”);
printf(“***************************************************************\n\n”);
ID*ptr=(int*)malloc(21*sizeof(ID));
做
{
printf(“请选择(1、2或3):”;
scanf(“%d”&answere);
如果(Answare!=1&&Answare!=2&&Answare!=3)
{
printf(“…错误…\n请给我一个正确的答案\n”);
}
如果(Answare==1)
{
i++;
addNew(ptr,i);
}
else if(answere==2)
{
打印列表(ptr,i);
}
}而(Answare!=3);
返回0;
}
正如我所说的,我的问题是我无法打印结构的成员,因为我需要使用指针数组来打印它们。我认为我写的东西不正确,就像在
printf
中有点逻辑错误一样。
我遇到的唯一障碍是需要在
main

中创建指针数组。您似乎完全迷路了

一些基础知识 您正在向数组的第一项(第一项的地址)传递指针/引用 要在该地址获取值(取消引用),可以执行两项操作:

  • *ptr//但我不建议将其用于结构
  • ptr[0]
由于您使用的结构通常比较大,所以希望您使用指针,因此存在所谓的“箭头”运算符,可用于访问结构指针的成员

  • ptr->firstName
因此,您的函数如下所示:

    void PrintList(ID *ptr, int l)
    {
         int i;
         printf(" #   First Name       Last Name       Phone Number\n");
         for(i = 0; i < l; i++)
         {
            printf("%2d.   %s       %s       %s\n", 
i, ptr[i].firstName, ptr[i].firstName, ptr[i].phoneNumber);
        }
    }
void打印列表(ID*ptr,int l)
{
int i;
printf(“#名字姓氏电话号码”);
对于(i=0;i

此外,我建议使用“\t”而不是空格来对齐列。

printf(“%s\n”,ptr[I].firstName);
是但不工作,以哪种方式“不工作”?崩溃?挂起?无输出?错误输出?无休止的输出?编译器错误?编译器警告(您确实使用了严格的警告,不是吗)?Segfault?请描述发生了什么,与您预期的情况相反?我想您得到了每个名字,但想要所有结构成员,这是正确的吗?没有输出,它只是显示消息的第一部分,当它出现在变量的部分时,它只是像它不在那里一样继续。另外,我在ad中有一些编译器警告dnew及其显示3次[Warning]赋值从指针生成整数,而不在main[Warning]中强制转换从不兼容的指针类型初始化是的,我有一点问题,也是的,我的af丢失了,但我的问题是它打印出#名字姓氏电话号码0。Ρh MEDRIVE=C:Users\user 1.LOCALAPPDATA=C:\Users\user\AppData\Local\user\AppData\Local OGONSERVER=\\user-PC 2\\user-PC处理器=4 C:\Users\user\OneDrive 3.er\OneDrive s\NT Files(x86)\Dev Cpp\MinGW64\bin;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\Wbem;C:\WINDOWS\system32\OpenSSH\;C:\Users\user…我认为最简单的方法是:void addNew(ID*ptr,int-ID){while(getchar()!='\n');//确保从新行scanf(“%s”,ptr[ID].firstName)开始;while(getchar()!='\n');scanf(“%s”,ptr[ID].lastName);while(getchar()!='\n');scanf(“%s”,ptr[ID].phoneNumber);}