[英]Why can't a struct's member variables be modified in a function?
I am curious to why one cannot modify the variables of a struct when passed as a parameter into a function. 我很好奇为什么当作为参数传递给函数时不能修改结构的变量。 I understand that the parameter is pass by value but when passing a struct variable you are passing its reference as the value.
我知道参数是按值传递的,但是当传递结构变量时,您会将其引用作为值传递。
Surely C doesn't create a copy of the struct on the stack, because the value of the struct reference being passed in is the same as the value of passing a pointer of the struct into a function. 当然,C不会在堆栈上创建该结构的副本,因为传入的结构引用的值与将结构的指针传递给函数的值相同。
#include <stdio.h>
#include <stdlib.h>
typedef struct String {
char* name;
int x;
} String;
/* Func Protos */
void foo(String str);
void bar(String * str);
void banjo(String str);
int main() {
String str = *(String *)malloc(sizeof(String));
str.x = 5; /* x == 5 */
foo(str); /* x == 5 still */
printf("%d\n", str.x);
banjo(str); /* x == 5 still */
printf("%d\n", str.x);
bar(&str); /* and of course, x = 0 now */
printf("%d\n", str.x);
}
void foo(String str) {
printf("%d\n", str); /* Same value as if we passed String * str */
str.x = 0;
}
void bar(String * str) {
printf("%d\n", *str);
str->x = 0;
}
void banjo(String str) {
printf("%d\n", str);
String * strptr = &str; /* This should be identical to passing String * str */
strptr->x = 0;
}
Produces this output: 产生以下输出:
3415000
5
3415000
5
3415000
0
Any help would be much appreciated! 任何帮助将非常感激!
void banjo(String str) {
printf("%d\n", str);
String * strptr = &str; /* This should be identical to passing String * str */
strptr->x = 0;
}
C is pass-by-value. C是传递值。 What you get in
banjo
function with str
argument is a copy of main
str
object. 使用
str
参数在banjo
函数中得到的是main
str
对象的副本。
So your banjo
function is equivalent to: 因此,您的
banjo
函数等效于:
void banjo(String str) {
printf("%d\n", str);
strptr.x = 0; // useless, the object is discarded after function returns
}
By the way printf("%d\\n", str);
通过方式
printf("%d\\n", str);
is not valid. 无效。
d
conversion specifier required an int
but you are passing a structure value. d
转换说明符需要一个int
但是您正在传递一个结构值。 The call invokes undefined behavior. 该调用调用未定义的行为。 If you want to print the address of
str
object use: 如果要打印
str
对象的地址,请使用:
printf("%p\n", (void *p) &str);
This line: 这行:
String str = *(String *)malloc(sizeof(String));
Is why you're getting that behavior. 这就是为什么您会得到这种行为。 Change it to:
更改为:
String *str = (String *)malloc(sizeof(String));
And that will solve the problem. 这样就可以解决问题。
The statement 该声明
printf("%d\n", str);
doesnot print the address of the structure, instead it prints the value of the first element 不打印结构的地址,而是打印第一个元素的值
inside the structure. 内部结构。 And since the first element inside your structure is a pointer to char, thus the printf
而且由于结构内部的第一个元素是指向char的指针,因此printf
is giving garbage value on the console. 在控制台上提供垃圾值。
Try this example : 试试这个例子:
#include<stdio.h>
struct a
{
int b;
};
int main()
{
struct a a1;
a1.b = 10; // try giving different value to a1.b
printf("b = %u\n", a1.b);
printf("a1 = %u\n", a1);
return 0;
}
Where you think you are printing the base address of the structure, you are actually 您认为要打印结构的基地址的地方,实际上
printing the value of the first element inside the structure. 在结构内部打印第一个元素的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.