简体   繁体   English

为什么不能在函数中修改结构的成员变量?

[英]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.

相关问题 为什么我们不能为结构的成员设置默认值? - Why we can't set a default value to a struct's member? 为什么结构不能成为其自身的成员? - Why can't a struct be a member of itself? 为什么即使我定义了 _GNU_SOURCE 也无法访问 uname 结构的域名成员 - Why can't I access uname struct's domainname member even if I defined _GNU_SOURCE 为什么结构不能拥有与自身类型相同的成员? - Why can't a struct have a member that is of the same type as itself? 访问传递给函数的结构成员变量 - Accessing struct member variables passed into function 为什么我不能将 function 返回的结构分配给结构? - Why I can't assign struct returned by a function to struct? 选择结构成员并更改其内容的函数 - Function that selects a member of a struct and alters it's contents 无法访问struct的struct数组中的成员变量 - can't access a member variable in struct array in struct 为什么不能在函数外访问结构指针 - Why can't access struct pointer outside of function 对于struct变量s1,s2,为什么我可以初始化“s1 = {25,3.5}”,将s2指定为“s1 = s2”,但是不能使用“s1 = {59,3.14}? - For struct variables s1,s2,why can I initialize “s1={25,3.5}”,assign s2 as “s1=s2”,but then can't use "s1={59,3.14}?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM