Hi,
I would like to make a database with users.
But I take errors in the function

Could someone help me please?

My code is :

typedef struct user user;

int id2=0;

struct user
{
   char name[20];
  char last[20];
  int id;
};

int function(user* users, int id, char name[], char surname[]){
user* u = users+id2;
strcpy(u->name,name1);
strcpy(u->surname,surname1);
u->id=id2;
}

main()
{
  char name1[20];
  char surname1[20];
  users[50];

if(function(users, id,name1,surname1))
    ++id2;

// and then I would like to print name,surname,id form created struct ..... ??
}

Thanks a lot

Recommended Answers

All 3 Replies

typedef struct user user;

int id2=0;

struct user
{
   char name[20];
  char last[20];
  int id;
};

int function(user* users, int id, char name[], char surname[]){
user* u = users+id2;
strcpy(u->name,name);
strcpy(u->last,surname);
u->id=id2;
}

main()
{
  char name1[20];
  char surname1[20];
  users[50];

if(function(users, id,name1,surname1))
    ++id2;

}

I found my errors.

But how can I print name?

I am trying something like --> printf("%s",users[0].name);
but doe's not work (problem something like encoding)

Maybe is wrong the line strcpy(u->name,name);

why the pointers?

int function(user* users, int id, char name[], char surname[]){
strcpy(users[id].name,name);
strcpy(users[id].last,surname);
users[id].id=id2;
}

And in main() you need to initialize the strings and int before passing them on to that function. If you don't then your program will just be so much crap.


It will also make is much more clearer that users is an array of user structures instead of a pointer to one structure if you code this: int function(user users[] ...

why the pointers?

int function(user* users, int id, char name[], char surname[]){
strcpy(users[id].name,name);
strcpy(users[id].last,surname);
users[id].id=id2;
}

And in main() you need to initialize the strings and int before passing them on to that function. If you don't then your program will just be so much crap.


It will also make is much more clearer that users is an array of user structures instead of a pointer to one structure if you code this: int function(user users[] ...

Thank you for your advice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.