好吧我已经包括了我的结构以及我的指针,这是我想要弄清楚,我需要存储多达5个人的配置文件,但我不知道如何在我的数组中存储这些,同时使用指针 如果我没有指针,我可以这样做: 的strcpy(用户[0] .UserName, “whatevername”); 的strcpy(用户[0] .UserName, “whateverpwd”);
但是如何在使用指向我的结构的点时指定数组中我想要信息的位置...我希望这是有道理的我不认为我可以更好地解释它
struct profile
{
char First[15];
char Last[15];
char Pwd[10];
char UserName[10];
};
struct profile user[100];
struct profile *puser;
puser=&user[0];
void add_user(struct profile *puser)
{
int i = 0;
int j = 0;
int quit = 0;
char fname[30];
char lname[30];
char username[30];
char password[30];
do
{
printf("Enter the first name of the user:\n");
fgets((puser+i)->First,15, stdin);
printf("Enter the last name of the user:\n");
fgets((puser+i)->Last, 15, stdin);
printf("Enter the username:\n");
fgets((puser+i)->UserName, 30, stdin);
printf("Enter the password:\n");
fgets((puser+i)->Pwd, 30, stdin);
printf("the first name is: %s\n", (puser+i)->First);
printf("the last name is: %s\n", (puser+i)->Last);
printf("the user name is: %s\n", (puser+i)->UserName);
printf("the password name is: %s\n", (puser+i)->Pwd);
j++;
printf("enter 0 to exit 1 to continue:");
scanf("%d", &quit);
if(quit == 0)
printf("goodbye");
i++;
getchar();
}while(quit == 1);
}
答案 0 :(得分:1)
试试这个:
#include <stdio.h>
typedef struct
{
char First[15];
char Last[15];
char Pwd[10];
char UserName[10];
}profile;
profile user[100];
profile *puser = user;
void add_user(profile *puser);
void add_user(profile *puser)
{
int i = 0;
int j = 0;
int quit = 0;
char fname[30];
char lname[30];
char username[30];
char password[30];
do
{
printf("Enter the first name of the user:\n");
fgets(puser[i].First,15, stdin);
printf("Enter the last name of the user:\n");
fgets(puser[i].Last, 15, stdin);
printf("Enter the username:\n");
fgets(puser[i].UserName, 30, stdin);
printf("Enter the password:\n");
fgets(puser[i].Pwd, 30, stdin);
printf("enter 0 to exit 1 to continue:");
scanf("%d", &quit);
getchar();
i++;
}while(quit == 1);
for( j = 0; j < i; j++ )
{
printf("first name[%d] is: %s\n", j,(puser+j)->First);
printf("last name[%d] is: %s\n", j,(puser+j)->Last);
printf("user name[%d] is: %s\n", j,(puser+j)->UserName);
printf("password[%d] is: %s\n", j,(puser+j)->Pwd);
}
printf("Goodbye\n");
}
int main(int argc, char *argv[])
{
add_user(puser);
return 0;
}