今天我在教几个朋友如何使用C结构体。其中一个问你是否可以从函数返回结构体,我回答说:“不能!你会返回指向动态错误结构的指针。”

来自一个主要做c++的人,我期望不能按值返回结构。在c++中,你可以重载对象的操作符=,使用函数按值返回对象是完全有意义的。然而,在C语言中,你没有这个选项,所以它让我思考编译器实际上在做什么。考虑以下几点:

struct MyObj{
    double x, y;
};

struct MyObj foo(){
    struct MyObj a;
    
    a.x = 10;
    a.y = 10;
    
    return a;
}        

int main () {

    struct MyObj a;
    
    a = foo();    // This DOES work
    struct b = a; // This does not work
      
    return 0;
}    

我明白为什么struct b = a;不应该工作——您不能重载数据类型的operator =。为什么a = foo();编译好了吗?它的意思是不是不是struct b = a;?也许要问的问题是:结合=符号的return语句到底有什么作用?


当前回答

#include <stdio.h>

struct emp {
    int id;
    char *name; /* This must point to valid memory, or replace with an array  that holes the data, like this char name[128] */
};

struct emp bad() {
    static char name[] = {'J', 'o', 'h', 'n', '\0'}; /* static enforces this array to be stored globally and not in the local stack which would not be valid after the function returns */
    struct emp e1 = {404, name};
    return (e1);
}

int main() {
    struct emp e2 = bad();
    printf("%s\n", e2.name);
}

其他回答

据我所知,第一个版本的C只允许返回一个值 可以装进处理器寄存器,这意味着你只能返回一个指针 一个结构体。同样的限制也适用于函数参数。

最近的版本允许传递更大的数据对象,如结构体。 我认为这个功能在80年代或90年代早期已经很普遍了。

然而,数组仍然只能作为指针传递和返回。

struct emp {
    int id;
    char *name;
};

struct emp get() {
    char *name = "John";

    struct emp e1 = {100, name};

    return (e1);
}

int main() {

    struct emp e2 = get();

    printf("%s\n", e2.name);
}

适用于较新版本的编译器。 就像id一样,name的内容被复制到赋值的结构变量中。

结构体b行不起作用,因为这是一个语法错误。如果你将它扩展到包含该类型,它将工作得很好

struct MyObj b = a;  // Runs fine

C在这里所做的实际上是从源结构体到目标结构体的memcpy。对于struct值的赋值和返回值(以及C中的其他所有值)都是如此。

是的,我们可以通过结构,也可以返回结构。你是对的,但你实际上没有传递数据类型,应该像这个结构MyObj b = a。

实际上,当我试图找到一个更好的解决方案,在不使用指针或全局变量的情况下为函数返回多个值时,我也知道了。

下面是同样的例子,计算出一个学生平均分数的偏差。

#include<stdio.h>
struct marks{
    int maths;
    int physics;
    int chem;
};

struct marks deviation(struct marks student1 , struct marks student2 );

int main(){

    struct marks student;
    student.maths= 87;
    student.chem = 67;
    student.physics=96;

    struct marks avg;
    avg.maths= 55;
    avg.chem = 45;
    avg.physics=34;
    //struct marks dev;
    struct marks dev= deviation(student, avg );
    printf("%d %d %d" ,dev.maths,dev.chem,dev.physics);

    return 0;
 }

struct marks deviation(struct marks student , struct marks student2 ){
    struct marks dev;

    dev.maths = student.maths-student2.maths;
    dev.chem = student.chem-student2.chem;
    dev.physics = student.physics-student2.physics; 

    return dev;
}

你可以在c中为struct赋值a = b;是有效的语法。

您只是在行中省略了类型的一部分——struct标记——这是不起作用的。