我正在处理大量代码,这些代码大量使用结构来访问多个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;
}
答案 0 :(得分:2)
如果您想在其他函数中使用useful_stuff
的修改值,则需要使用指向define_loop
的指针传递它们。
leo_stuff
变量需要声明为普通变量,然后使用运算符&amp;的地址传递。
答案 1 :(得分:1)
你遇到的问题不是由于结构的困难,而是在理解传递变量或变量地址之间的区别。
当您将变量作为函数参数传递时,该函数会接收变量的COPY,以便所做的任何更改仅在函数中可见。调用函数中的变量值(即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