在函数内部更改堆栈上的结构
Changing a struct on the stack inside function
我正在学习 艰难地学习 C ,我目前卡在 extra credit exercise number 16。
我目前正在尝试调整他们的代码并在堆栈而不是堆上创建结构,但我的代码出现分段错误,我不确定为什么或如何继续。非常感谢任何建议。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Person {
char *name;
int age;
int height;
int weight;
};
void Person_create(struct Person p,char *name,int age,int height,int weight)
{
p.name = name;
p.age = age;
p.height = height;
p.weight = 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[])
{
// make two people structures
struct Person joe;
struct Person frank;
Person_create(
joe,"Joe Alex",32,64,140);
Person_create(
frank,"Frank Blank",20,72,180);
// print them out
Person_print(joe);
Person_print(frank);
// make everyone age 20 years 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;
}
您的代码在运行时产生错误的原因是 C 按值传递 struct
s,这意味着 Person_create
中的以下赋值无效:
p.name = name;
p.age = age;
p.height = height;
p.weight = weight;
这不会更改 main()
内的 joe
和 frank
,导致打印时出现未定义的行为,因为 name
数据成员 struct Person
保持未初始化状态。
为了解决这个问题,通过指针传递 struct Person
,并应用 ->
运算符代替 .
运算符:
void Person_create(struct Person *p,char *name,int age,int height,int weight)
{
p->name = name;
p->age = age;
p->height = height;
p->weight = weight;
}
...
Person_create(&joe, "Joe Alex", 32, 64, 140);
// ^ Pass an address
It is asking for me to not use pointers or malloc, and says as advice to look into creating a struct on the stack
您的代码已经在堆栈上创建了 struct
。如果您想完全摆脱指针,请将 Person_create
更改为 *return` 一个新的人,像这样:
struct Person Person_create(char *name,int age,int height,int weight) {
struct Person p;
p.name = name;
p.age = age;
p.height = height;
p.weight = weight;
return p;
}
...
joe = Person_create("Joe Alex", 32, 64, 140);
按以下方式定义函数Person_create
void Person_create(struct Person *p, char *name, int age, int height, int weight)
{
p->name = name;
p->age = age;
p->height = height;
p->weight = weight;
}
并这样称呼它
Person_create( &joe, "Joe Alex", 32, 64, 140 );
更复杂的方法如下
#include <string.h>
#include <stdlib.h>
//...
void Person_create(struct Person *p, const char *name, int age, int height, int weight)
{
size_t n = strlen( name );
p->name = malloc( ( n + 1 ) * sizeof( char ) );
strcpy( p->name, name );
p->age = age;
p->height = height;
p->weight = weight;
}
但是在这种情况下,您应该记得在结构对象离开其作用域时释放分配的内存。
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *name;
int age;
int height;
int weight;
}Person;
void Person_print(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[])
{
Person joe= {"Joe Alex", 32, 64, 140};
Person frank={"Joe Alex", 32, 64, 140};
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;
}
typedef struct called 'Person', 那么你就不必使用 'Person create'.
我正在学习 艰难地学习 C ,我目前卡在 extra credit exercise number 16。
我目前正在尝试调整他们的代码并在堆栈而不是堆上创建结构,但我的代码出现分段错误,我不确定为什么或如何继续。非常感谢任何建议。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Person {
char *name;
int age;
int height;
int weight;
};
void Person_create(struct Person p,char *name,int age,int height,int weight)
{
p.name = name;
p.age = age;
p.height = height;
p.weight = 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[])
{
// make two people structures
struct Person joe;
struct Person frank;
Person_create(
joe,"Joe Alex",32,64,140);
Person_create(
frank,"Frank Blank",20,72,180);
// print them out
Person_print(joe);
Person_print(frank);
// make everyone age 20 years 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;
}
您的代码在运行时产生错误的原因是 C 按值传递 struct
s,这意味着 Person_create
中的以下赋值无效:
p.name = name;
p.age = age;
p.height = height;
p.weight = weight;
这不会更改 main()
内的 joe
和 frank
,导致打印时出现未定义的行为,因为 name
数据成员 struct Person
保持未初始化状态。
为了解决这个问题,通过指针传递 struct Person
,并应用 ->
运算符代替 .
运算符:
void Person_create(struct Person *p,char *name,int age,int height,int weight)
{
p->name = name;
p->age = age;
p->height = height;
p->weight = weight;
}
...
Person_create(&joe, "Joe Alex", 32, 64, 140);
// ^ Pass an address
It is asking for me to not use pointers or malloc, and says as advice to look into creating a struct on the stack
您的代码已经在堆栈上创建了 struct
。如果您想完全摆脱指针,请将 Person_create
更改为 *return` 一个新的人,像这样:
struct Person Person_create(char *name,int age,int height,int weight) {
struct Person p;
p.name = name;
p.age = age;
p.height = height;
p.weight = weight;
return p;
}
...
joe = Person_create("Joe Alex", 32, 64, 140);
按以下方式定义函数Person_create
void Person_create(struct Person *p, char *name, int age, int height, int weight)
{
p->name = name;
p->age = age;
p->height = height;
p->weight = weight;
}
并这样称呼它
Person_create( &joe, "Joe Alex", 32, 64, 140 );
更复杂的方法如下
#include <string.h>
#include <stdlib.h>
//...
void Person_create(struct Person *p, const char *name, int age, int height, int weight)
{
size_t n = strlen( name );
p->name = malloc( ( n + 1 ) * sizeof( char ) );
strcpy( p->name, name );
p->age = age;
p->height = height;
p->weight = weight;
}
但是在这种情况下,您应该记得在结构对象离开其作用域时释放分配的内存。
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *name;
int age;
int height;
int weight;
}Person;
void Person_print(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[])
{
Person joe= {"Joe Alex", 32, 64, 140};
Person frank={"Joe Alex", 32, 64, 140};
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;
}
typedef struct called 'Person', 那么你就不必使用 'Person create'.