【问题标题】:How to create a struct on the stack in C?如何在C中的堆栈上创建一个结构?
【发布时间】:2012-06-10 14:54:43
【问题描述】:

我了解如何使用malloc 在堆上创建struct。正在寻找一些关于在堆栈上用 C 创建 struct 的文档,但所有文档。似乎只在堆上谈论结构创建。

【问题讨论】:

    标签: c heap-memory stack-memory


    【解决方案1】:

    要在堆栈上声明一个结构,只需将其声明为普通/非指针值

    typedef struct { 
      int field1;
      int field2;
    } C;
    
    void foo() { 
      C local;
      local.field1 = 42;
    }
    

    【讨论】:

    • 它必须是一个非静态函数局部变量(就像很多变量一样)才能进入堆栈。
    【解决方案2】:

    与在堆栈上声明任何变量的方式相同:

    struct my_struct {...};
    
    int main(int argc, char **argv)
    {
        struct my_struct my_variable;     // Declare struct on stack
        .
        .
        .
    }
    

    【讨论】:

      【解决方案3】:

      我让它这样工作:

      #include <stdio.h>
      
      struct Person {
        char *name;
        int age;
        int height;
        int weight;
      };
      
      int main(int argc, char **argv)
      {
        struct Person frank;
        frank.name = "Frank";
        frank.age = 41;
        frank.height = 51;
        frank.weight = 125;
      
        printf("Hi my name is %s.\n", frank.name);
        printf("I am %d yeads old.\n", frank.age);
        printf("I am %d inches tall.\n", frank.height);
        printf("And I weigh %d lbs.\n", frank.weight);
      
        printf("\n-----\n");
      
        struct Person joe;
        joe.name = "Joe";
        joe.age = 50;
        joe.height = 93;
        joe.weight = 200;
      
        printf("Hi my name is %s.\n", joe.name);
        printf("I am %d years old.\n", joe.age);
        printf("I am %d inches tall.\n", joe.height);
        printf("And I weigh %d lbs.\n", joe.weight);
      
        return 0;
      }
      

      【讨论】:

      • “这会很困难,所以你会想要研究……”,Zed 说。因此,我没有先考虑清楚,而是在网上查看并看到了这个......但从好的方面来说,它只是 点击 并且是有道理的。我进行了所有更改,甚至没有参考这段代码。
      • Zed 让我研究如何在栈上创建一个结构体,这个信息很完美。
      【解决方案4】:

      使用函数回答 17.4 Extra Credit(在 Zed 的“Learn C the Hard Way”一书中)

      #include <stdio.h>
      
      struct Person {
              char *name;
              int age;
              int height;
              int weight;
      };
      
      
      struct Person Person_create(char *name, int age, int height, int weight)
      {
              struct Person who;
              who.name = name;
              who.age = age;
              who.height = height;
              who.weight = weight;
      
              return who;
      }
      
      
      void Person_print(struct Person who)
      {
              printf("Name: %s\n", who.name);
              printf("\tAge: %d\n", who.age);
              printf("\tHeight: %d\n", who.height);
              printf("\tWeight: %d\n", who.weight);
      }
      
      int main(int argc, char *argv[])
      {
              // make two people structures 
              struct Person joe = Person_create("Joe Alex", 32, 64, 140);
              struct Person frank = Person_create("Frank Blank", 20, 72, 180);
      
              //print them out and where they are in memory
              printf("Joe is at memory location %p:\n", &joe);
              Person_print(joe);
      
              printf("Frank is at memory location %p:\n", &frank);
              Person_print(frank);
      
              // make everyone age 20 and print them again
              joe.age += 20;
              joe.height -= 2;
              joe.weight += 40;
              Person_print(joe);
      
              frank.age += 20;
              frank.weight += 20;
              Person_print(frank);
      
              return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-11
        • 2013-05-03
        • 1970-01-01
        • 2018-05-14
        • 2017-01-30
        • 2011-04-20
        • 2017-07-19
        • 2012-11-23
        相关资源
        最近更新 更多