简体   繁体   English

通过C代码中的引用将结构传递给函数(编译错误)

[英]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: 请注意,内存分配功能不能做任何有用的事情,除非:

  1. It knows about the types of the structures it is to allocate. 它知道要分配的结构的类型。
  2. It is passed pointers to the pointers so it can modify the actual pointers in the main() . 它将指针传递给指针,以便它可以修改main()的实际指针。
  3. The memory release function doesn't need pointer to pointer syntax, though it could be used and if it was used, the memory release function should set the pointed at pointers to NULL when the memory is released to minimize the risk of abusing already freed memory. 内存释放功能虽然可以使用指针语法,但不需要指针语法,如果使用了内存释放功能,则应在释放内存时将指向指针的指针设置为NULL,以最大程度地减少滥用已释放内存的风险。

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.

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