[英]How to create a struct on the stack in C?
I understand how to create a struct
on the heap using malloc
.我了解如何使用malloc
在堆上创建struct
。 Was looking for some documentation regarding creating a struct
in C on the stack but all docs.正在寻找一些关于在堆栈上创建 C struct
文档,但所有文档。 seem to talk about struct creation on heap only.似乎只在堆上谈论结构创建。
The same way you declare any variable on the stack:与在堆栈上声明任何变量的方式相同:
struct my_struct {...};
int main(int argc, char **argv)
{
struct my_struct my_variable; // Declare struct on stack
.
.
.
}
To declare a struct on the stack simply declare it as a normal / non-pointer value要在堆栈上声明结构,只需将其声明为普通/非指针值
typedef struct {
int field1;
int field2;
} C;
void foo() {
C local;
local.field1 = 42;
}
an answer to 17.4 Extra Credit (in Zed's book "Learn C the Hard Way") using functions使用函数回答 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;
}
I got it to work this way:我让它以这种方式工作:
#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;
}
It worked this way 它是这样工作的
#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.