【问题标题】:Difficult with structures C结构困难 C
【发布时间】:2015-03-25 20:36:57
【问题描述】:

我正在处理大量使用结构来访问多个 void 函数中的变量的大代码。我试图用程序的基本结构编写代码只是为了知道如何在不破坏它的情况下处理它。

我写了下面的代码有一些问题你们能帮我吗?

#include<stdlib.h>
#include<stdio.h>

typedef struct useful_stuff
{

    int loop_var;  /* I want to use the loop_var in multiple void 
                * functions to iterate my loops
            */

}useful_stuff;

typedef struct leo_stuff
{

    int the_stuff;/* the stuff is a member I will use and modify
               *during the execution of the two void functions
               */

}leo_stuff;

void define_loop(useful_stuff u_s)  // This variable I want to use during the code
{
    u_s.loop_var = 10;   
}

void use_leo_stuff(useful_stuff u_s,leo_stuff *l_s)
{
    int i,loop;
    loop = u_s.loop_var;

    printf("the loop var is: %d\n", loop);

    for(i=0; i < loop; ++loop)
    {
        l_s.the_stuff = i + 1000;
        //(*l_s).the_stuff = i + 1000;  Is this more correct ?
        printf("l_s stuff from stuff1 is: %d\n",l_s.the_stuff);
        //here I'm expecting to see 1001,1002,1003.....
    }

}

//why he choosed to call a struct with simple declaration or with a pointer ?
void use_leo_stuff_again(useful_stuff u_s,leo_stuff *l_s)  
{
    int i,loop;
    loop = u_s.loop_var;

    printf("The loop var from again is: %d\n",loop);

    for(i=0; i<loop; ++i)
    { 
        l_s.the_stuff = i+1000;
        printf("the_stuff from again is: %d\n", the_stuff);
        //here I expect to see 2011,2012,2013 ...
    }

}

int main()
{
    useful_stuff u_s;  // Is this the correct way to call functions with 
                   //   structures in main ??
    leo_stuff *l_s;

    define_loop(useful_stuff u_s);

    use_leo_stuff(useful_stuff u_s,leo_stuff *l_s);

        use_leo_stuff_again(useful_stuff u_s,leo_stuff *l_s);

    return 0;
}

【问题讨论】:

    标签: c function pointers structure


    【解决方案1】:

    如果您想在其他函数中使用useful_stuff 的修改值,您需要使用指向define_loop 的指针来传递它们。

    leo_stuff变量需要声明为普通变量,然后使用运算符&的地址传递。

    【讨论】:

      【解决方案2】:

      您遇到的问题与其说是由于结构的困难,不如说是在理解传递变量或变量地址之间的区别。

      当您将变量作为函数参数传递时,函数会收到变量的副本,这样所做的任何更改都只在函数中可见。调用函数中的变量值(即main())保持不变。

      要让函数真正改变内存中变量的值,您必须将变量 ADDRESS(即指针)作为参数传递给函数。然后,对该地址的值所做的任何更改也可以在main()(或调用函数)中使用。

      以下是反映此原则的代码调整。无论您传递给函数的数据类型如何,它都适用。如果您有任何问题,请告诉我。

      #include<stdlib.h>
      #include<stdio.h>
      
      typedef struct useful_stuff 
      {
          int loop_var;
      
      } useful_stuff;
      
      typedef struct leo_stuff
      {
          int the_stuff;
      
      } leo_stuff;
      
      /* to change the value in main(), you must pass the address
      of u_s (i.e. a pointer to u_s) otherwise, the function just
      gets a COPY of u_s, and the value in main() is unchanged
      */
      void define_loop (useful_stuff *u_s)
      {
          u_s->loop_var = 10;   
      }
      
      
      void use_leo_stuff (useful_stuff u_s, leo_stuff *l_s)
      {
          int i = 0;
          int loop = 0;
      
          loop = u_s.loop_var;
      
          printf ("\nThe loop var is: %d\n\n", loop);
      
          for(i=0; i < loop; ++i)
          {
              l_s->the_stuff = i + 1000;
      
              printf("  l_s stuff from stuff1 is: %d\n",l_s->the_stuff);
          }
      }
      
      void use_leo_stuff_again (useful_stuff u_s, leo_stuff *l_s)  
      {
          int i = 0;
          int loop = 0;
      
          loop = u_s.loop_var;
      
          printf("\nThe loop var from again is: %d\n\n",loop);
      
          /* l_s->the_stuff is still 1009 from the first funciton 
          let's do something different here, like reduce it to
          the original value
          */
          for(i=0; i<loop; ++i)
          { 
              printf("  the_stuff from again is: %d\n", l_s->the_stuff);
              l_s->the_stuff -= 1;
          }
      
      }
      
      int main()
      {
          useful_stuff u_s;           /* declare an instance of u_s & stuff       */
          leo_stuff stuff;
      
          leo_stuff *l_s = &stuff;    /* then create your l_s pointer to stuff    */
      
          define_loop (&u_s);         /* pass the address to preserve the change  */
      
          use_leo_stuff (u_s, l_s);   /* call your functions as normal    */
      
          use_leo_stuff_again (u_s, l_s);
      
          printf ("\n");
      
          return 0;
      }
      

      输出

      $ ./bin/struct_ptrs
      
        l_s stuff from stuff1 is: 1000
        l_s stuff from stuff1 is: 1001
        l_s stuff from stuff1 is: 1002
        l_s stuff from stuff1 is: 1003
        l_s stuff from stuff1 is: 1004
        l_s stuff from stuff1 is: 1005
        l_s stuff from stuff1 is: 1006
        l_s stuff from stuff1 is: 1007
        l_s stuff from stuff1 is: 1008
        l_s stuff from stuff1 is: 1009
      
      The loop var from again is: 10
      
        the_stuff from again is: 1009
        the_stuff from again is: 1008
        the_stuff from again is: 1007
        the_stuff from again is: 1006
        the_stuff from again is: 1005
        the_stuff from again is: 1004
        the_stuff from again is: 1003
        the_stuff from again is: 1002
        the_stuff from again is: 1001
        the_stuff from again is: 1000
      

      【讨论】:

      • 等一下,按 Tab 输入太快了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-28
      相关资源
      最近更新 更多