0

好的,我已经包含了我的结构和指针,这就是我想要弄清楚的,我需要存储多达 5 个人资料,但我不知道如何在使用指针时将这些存储在我的数组中,如果我没有指针我可以这样做:strcpy(user[0].UserName,"whatevername"); strcpy(user[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);
}
4

1 回答 1

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;
}
于 2012-04-22T23:58:55.283 回答