[英]passing structs into function by reference in C code (compilation error)
Is there anything wrong with this? 这有什么问题吗? According to the compiler there is an error in this line
根据编译器,这行有错误
void memory_allocation(struct a *s1,struct b *s2,struct c *s3,int size)
main looks like 主要看起来像
main()
{
struct mystack
{
int head;
void **stack;
void **stackcopy;
int stack_size;
} *ptr ;
struct professor
{
char flag;
char surname[50];
char coursbhe[50];
} *prof;
struct student
{
char flag;
char surname[50];
int semester;
} *stud;
(*ptr).stack_size=getstacksize();
memory_allocation(&ptr,&prof,&stud,(*ptr).stack_size);
}
Your types are local to the function ( main()
) in which they are defined. 您的类型在定义它们的函数(
main()
)中是本地的。 That means you cannot reliably pass variables of those types to any other function. 这意味着您不能将那些类型的变量可靠地传递给任何其他函数。 You should define the types outside of
main()
so that other functions can use the same types. 您应该在
main()
之外定义类型,以便其他函数可以使用相同的类型。 There is a concept of type equivalence, but there's also a concept called DRY — Don't Repeat Yourself. 有一个类型等效的概念,但也有一个称为DRY的概念-不要重复自己。 The only way another function could use those types is by writing them out again, repeating yourself.
另一个函数可以使用这些类型的唯一方法是再次将它们写出,重复一次。 Don't!
别!
Your types need to be available outside the function. 您的类型需要在功能之外可用。 You don't have to define any global variables when you define the types.
定义类型时,不必定义任何全局变量。 For example, you could use:
例如,您可以使用:
struct mystack
{
int head;
void **stack;
void **stackcopy;
int stack_size;
};
struct professor
{
char flag;
char surname[50];
char coursbhe[50];
};
struct student
{
char flag;
char surname[50];
int semester;
};
extern int getstacksize(void);
extern void memory_allocation(struct stack **s1, struct professor **s2,
struct student **s3, int size);
extern void memory_release(struct stack *stack, struct professor *prof,
struct student *stud);
int main(void)
{
struct mystack *ptr = 0;
struct professor *prof = 0;
struct student *stud = 0;
int stack_size = getstacksize();
memory_allocation(&ptr, &prof, &stud, stack_size);
memory_release(ptr, prof, stud);
return 0;
}
Note that the memory allocation function cannot do anything useful unless: 请注意,内存分配功能不能做任何有用的事情,除非:
main()
. main()
的实际指针。 The additional variable stack_size
is needed because ptr
doesn't point to anything when getstacksize()
is called, so referencing (*ptr).stack_size
will probably crash and definitely not work correctly. 需要额外的变量
stack_size
,因为调用getstacksize()
时ptr
不会指向任何内容,因此引用(*ptr).stack_size
可能会崩溃,并且绝对不能正常工作。 Alternatively, you could change the definition of the variable to stack mystack stack;
另外,您可以将变量的定义更改为
stack mystack stack;
and then use stack.stack_size = getstacksize();
然后使用
stack.stack_size = getstacksize();
and pass stack.stack_size
to the allocation function, but then you don't need the memory allocator to allocate the stack as a whole any more. 并将
stack.stack_size
传递给分配函数,但是您不再需要内存分配器来分配整个堆栈。 Separation of duty suggests that there will be a separate function to allocate the stack internals. 职责分离表明,将有一个单独的函数来分配堆栈内部。
All the material from the first struct mystack
line down to but not including int main(void)
is eligible to be put into a header so that the implementation of the functions can see the same type and function definitions as the main()
program can see. 从第一个
struct mystack
行直到int main(void)
但不包括int main(void)
所有材料都可以放入标头,以便函数的实现可以看到与main()
程序可以看到的相同类型和函数定义。
Here is the solution.... use your prototype of function as below 这是解决方案。...使用您的函数原型如下
void memory_allocation(struct mystack *s1 , struct professor *s2 , struct student *s3 , int d) void memory_allocation(struct mystack * s1,struct Professor * s2,struct student * s3,int d)
because you are calling with defined struct not other and call should be like 因为您正在使用定义的结构而不是其他进行调用并且调用应该像
memory_allocation(ptr,prof,stud,(*ptr).stack_size); memory_allocation(PTR,教授,双头螺栓,(* PTR).stack_size);
remove "&" sign or remove *s1 to s1(have to pass address ,not address of address) 删除“&”符号或删除* s1到s1(必须传递地址,而不是地址的地址)
please change both things, it will work,my side its working.. 请改变两者,它会起作用,我这边会起作用..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.