Wie erstelle ich eine Struktur auf dem Stack in C?

Lesezeit: 2 Minuten

Ich verstehe, wie man eine erstellt struct auf dem Haufen mit malloc. War auf der Suche nach einer Dokumentation zum Erstellen von a struct in C auf dem Stapel, aber alle Dokumente. scheinen nur über die Erstellung von Strukturen auf dem Heap zu sprechen.

Auf die gleiche Weise deklarieren Sie eine beliebige Variable auf dem Stack:

struct my_struct {...};

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

Um eine Struktur auf dem Stapel zu deklarieren, deklarieren Sie sie einfach als normalen / Nicht-Zeigerwert

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

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

  • Es muss eine nichtstatische funktionslokale Variable sein (wie sehr viele Variablen), um auf den Stack zu gehen.

    – Donal Fellows

    6. Juni 2012 um 15:02 Uhr

eine Antwort auf 17.4 Extra Credit (in Zeds Buch „Learn C the Hard Way“) mit Funktionen

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

Ich habe es so zum Laufen gebracht:

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

1411920cookie-checkWie erstelle ich eine Struktur auf dem Stack in C?

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy