簡體   English   中英

如何在C中的堆棧上創建結構?

[英]How to create a struct on the stack in C?

我了解如何使用malloc在堆上創建struct 正在尋找一些關於在堆棧上創建 C struct文檔,但所有文檔。 似乎只在堆上談論結構創建。

與在堆棧上聲明任何變量的方式相同:

struct my_struct {...};

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

要在堆棧上聲明結構,只需將其聲明為普通/非指針值

typedef struct { 
  int field1;
  int field2;
} C;

void foo() { 
  C local;
  local.field1 = 42;
}

使用函數回答 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;
}

我讓它以這種方式工作:

#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;
}

 #include<stdio.h>

    struct Person {
        char *name;
        int age;
        int height;
        int weight;
    };

    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[]){

        struct Person joe;  
        joe.name="Joe Alex";
        joe.age=32;
        joe.height=64;
        joe.weight=140;

/* Another way of initializing struct */

        struct Person frank ={.name="Frank Blank",.age=20,.height=72, .weight=180};

        printf("Joe is at memory location %p:\n", joe);
        Person_print(joe);
        printf("Frank is at memory location %p\n", frank);
        Person_print(frank);

        joe.age += 20;
        joe.height -= 2;
        joe.weight += 40;
        Person_print(joe);

        frank.age += 20;
        frank.weight += 20;
        Person_print(frank);

        return 0;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM