LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   pass the variable contain (https://www.linuxquestions.org/questions/programming-9/pass-the-variable-contain-635750/)

lrios 04-16-2008 10:50 AM

pass the variable contain
 
Hi all,
I try to pass the variable's contain for != functions.
I have an structure

Code:

struct struc_user {
        int *number_user;       
        char username[100];       
               
} users[MAX_USER];

with store the input of the users.
Like

Code:

printf("Which is your nick? \n");
gets(nick);

And my questions is how pass the variable contain, in my case char username, for != functions for example

Code:

room();
minus();
etc();
.
.

This structure will stored the input users.

jlinkels 04-17-2008 08:07 AM

Irios, I suppose English is not your native language, but this is really difficult to understand.

I assume with 'contain' you mean 'contents'. The value of where the pointer point at.

But what is != function. Can you try to explain that with words and examples?

jlinkels

lrios 04-17-2008 04:06 PM

Well, when I write != i mean different(like c lol). In other words i want to pass value variable in differents functions. I write in my first thread some examples of pseudo-code.

I need help !

Nylex 04-17-2008 05:28 PM

Do you want to pass the contents of, say, "username" for a particular user (i.e. element of your users array) to some function? You use the same array syntax as normal to access elements of the array and the . operator to access members of the structure. Here's an example:

Code:

  struct struct_user
  {
    int x;
  } users[10];

  users[0].x = 1;
  users[1].x = 2;
  printf("%d\n", users[0].x);
  printf("%d\n", users[1].x);


lrios 04-17-2008 07:58 PM

Quote:

Originally Posted by Nylex (Post 3124559)
Do you want to pass the contents of, say, "username" for a particular user (i.e. element of your users array) to some function? You use the same array syntax as normal to access elements of the array and the . operator to access members of the structure. Here's an example:

Code:

  struct struct_user
  {
    int x;
  } users[10];

  users[0].x = 1;
  users[1].x = 2;
  printf("%d\n", users[0].x);
  printf("%d\n", users[1].x);


Ok. this is true Nylex, but now my question is:

If I have this structure

Code:

int n_user;
struct struc_user {
        int *number_user;       
        char username[20];       
               
} users[100];

right, and I stored each username with

Code:

puts("Which is your username?");
fgets(users[n_user].username, sizeof(users[n_user].username), stdin);

-How can I identify which username is (logon) ?
username[1]="kevin"
username[2]="bob"
username[3]="mark"
.
.
-and how can i call each username in different functions, I mean:
room();
read_nick();
.
.

jlinkels 04-17-2008 08:14 PM

Quote:

Originally Posted by lrios (Post 3124663)
username[1]="kevin"
username[2]="bob"
username[3]="mark"
.

Not sure what you mean, but you access each username with:
Code:

users[n_user].username
and you can do a strcmp on each element to see if it matches. username[3] is really nothing, at most it is a record, but no string.

Quote:

Originally Posted by lrios (Post 3124663)
Ok. this is true Nylex, but now my question is:
-and how can i call each username in different functions, I mean:
room();
read_nick();

You could pass a pointer to the function:
Code:

room (&users[1].username)
read_nick (&users[2].username)

jlinkels

lrios 04-18-2008 11:57 AM

users[n_user].username = "kevin"

users[n_user].username = "root"


Is ok this way ???

jlinkels 04-18-2008 05:56 PM

It looks good. n_user is an integer < 100 of course


All times are GMT -5. The time now is 11:04 AM.