简体   繁体   中英

How to return a struct without using global declaration of struct in C

Hello i am trying to return a struct from a function but i cant find a way to do so without declaring the struct as global. How can this be done? Here is the code (THIS WORKS AS IT IS)

...
void log_in();
struct node
{
    char name_log[20];
    int passlog;
    int user_point;
}tmp;

int main()
{
  ...
  else if(sel=='2')
  {
     log_in();
     if (tmp.passlog==TRUE)
     logged_in(tmp.name_log,tmp.user_point);  //and here i want to use the retun values
  }

void log_in()
{
   ... //make the changes in the struct
}
...

What i want to achieve is to place the struct node declaration within main but sadly it wont work. So here is what i am trying to do: (THIS DOESN'T WORK)

...
struct node log_in();

int main() {
    ...
    else if(sel=='2') {
        struct node //here is where i want to declare
        {
            char name_log[20];
            int passlog;
            int user_point;
        }tmp;

        log_in();
        if (tmp.passlog==TRUE)
            logged_in(tmp.name_log,tmp.user_point); //and here i want to use the retun values
    }

    struct node log_in()
    {
        ...
        return tmp;
    }
    ...
 else if(sel=='2')            //or within this block but I don't know how.
  {  struct node tmp;
     tmp=log_in();
     if (tmp.passlog==TRUE)
     logged_in(tmp.name_log,tmp.user_point);  //and here I want to use the return values
  }

and inside the function log_in()

struct node log_in()
{ 
 struct tmp
 ...
 return tmp;
}

use a local variable inside the function and return this variable. Assign it to another variable inside main().

Declare the structure first, then create the variable temp . Like this:

struct node
{
    char name_log[20];
    int passlog;
    int user_point;
};

Then you can create the local variable like

struct node tmp;

Pass a pointer to the struct node to your log_in function and have it return a boolean value so the caller can check whether logging in succeeded or didn't. (Note I'm trying to guess what you want to achieve, and I might be guessing wrong.)

#include <stdio.h>
#include <string.h>

struct node {
    char name_log[20];
    int passlog;
    int user_point;
};

int log_in(char, struct node *);

int log_in(char sel, struct node * tmp) {
    int ret = 0;
    if (sel == '2') {
        ret = 1;
        strcpy( tmp->name_log, "Gonzo" );
        tmp->passlog = 33;
        tmp->user_point = 99;
    }
    return ret;
}

int main(int argc, char ** argv) {
    struct node tmp;
    char sel = argv[1][0];
    if ( log_in(sel, &tmp) ) {
        // tmp initialized
        printf( "%s, %d, %d\n", tmp.name_log, tmp.passlog, tmp.user_point );
    }
    else {
        // tmp not initialized
    }
}

Call passing 2 on the command line. (If you don't, undefined behaviour.)

If you want to use some struct in 2 different routines - you must declare it outside of both of them since they both have to see how this struct is structured. BTW - you invoke log_in but do not use its return value.

You can't operate on a type that is unknown. If log_in() doesn't know the definition of struct node , it can't use it directly. The only thing it can do is somehow receive a pointer to a variable of this type and then either treat it as raw data (sequence of bytes) or cast said pointer to a pointer to a known to log_in() type and work with that.

You can also redefine struct node inside of log_in() , which is a way of making log_in() operate on a known type:

void log_in(void*);
void logged_in(char*, int);

int main(void)
{
    int sel = '2';

    if (sel == '2')
    {
        struct node
        {
            char name_log[20];
            int passlog;
            int user_point;
        } tmp;

        log_in(&tmp);
        if (tmp.passlog)
            logged_in(tmp.name_log, tmp.user_point);
    }

    return 0;
}

void log_in(void* n)
{
    struct node
    {
        char name_log[20];
        int passlog;
        int user_point;
    } *p = n;

    p->passlog = 1;
}

void logged_in(char* name, int point)
{
}

If you don't want to pass tmp by a formal reference into log_in() , you must make it available globally. For example like this:

void log_in(void);
void logged_in(char*, int);

void* pTmp;

int main(void)
{
    int sel = '2';

    if (sel == '2')
    {
        struct node
        {
            char name_log[20];
            int passlog;
            int user_point;
        } tmp;

        pTmp = &tmp;
        log_in();
        if (tmp.passlog)
            logged_in(tmp.name_log, tmp.user_point);
    }

    return 0;
}

void log_in(void)
{
    struct node
    {
        char name_log[20];
        int passlog;
        int user_point;
    } *p = pTmp;

    p->passlog = 1;
}

void logged_in(char* name, int point)
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM