【发布时间】: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