简体   繁体   English

传递给函数的struct指针

[英]struct pointer passed to a function

I have trouble understanding pointers being passed to the function. 我很难理解传递给函数的指针。 Here there's a struct pointer, which is passed to a function and the pointer is incremented by some value and it doesn't get reflected in the pointer variable. 这里有一个struct指针,该指针被传递给函数,并且该指针会增加一些值,并且不会反映在指针变量中。 I guess this is call by value but how? 我猜这是按值调用,但是如何? Can you please explain? 你能解释一下吗?

#include<stdio.h>
#include<stdlib.h>

typedef struct node{
    int data;
}node;

void change_addr(node *ptr){
    ptr += 10;
    printf("Inside Address : %p\n",ptr);
}

int main(){
    node x = {100};
    printf("Address : %p\n",x);
    change_addr(&x);
    printf("Address : %p\n",x);
    return 7;
}

You pass the address of the struct into the function. 您将结构的地址传递给函数。 This argument is incremented within the function and not accessible from the outside. 此参数在函数内递增,并且不能从外部访问。 Similar to what would happend if you'd pass and integer and increment it inside the function. 类似于如果在函数中传递和整数并将其递增,将会发生的情况。 If you want to increment data then you need to access it through the pointer passed into the function. 如果要增加data则需要通过传递给函数的指针进行访问。 For example node->data = 0; 例如node->data = 0; which is equivalent to (*node).data = 0; 相当于(*node).data = 0; .

&node is the address of the node struct. &node是节点结构的地址。 This parameter is pushed onto the call stack before writing the return address and jumping to the function. 在写入返回地址并跳转到函数之前,将此参数压入调用堆栈。 See this explanation of the C calling convention . 请参阅C调用约定的说明 change_addr() can now read and write this value without the main() ever seeing the value, because it's popped from the stack after the execution of change_addr() is finished. change_addr()现在可以在main()从未看到该值的情况下读取和写入此值,因为在change_addr()执行完成后,它会从堆栈中弹出。

The declaration 报关单

node x = { 100 };

allocates memory for a node structure, and initializes it. 为节点结构分配内存,并对其进行初始化。 The statement 该声明

change_addr(&x);

takes the memory address of that structure, which is just a number like, say, 1000, and passes it by value into the change_addr() function. 接收该结构的内存地址,该地址只是一个数字,例如1000,然后按值将其传递到change_addr()函数中。 Inside that function, you add 100 to your local copy of the pointer, named ptr , which makes it point to an area of memory far beyond where x is located (if x had been an array rather than a single struct, it would point to the 101st element). 在该函数内部,将100添加到名为ptr的指针的本地副本中,这使其指向远远超出x所在位置的内存区域(如果x是数组而不是单个结构,则它将指向第101个元素)。 You then return, which discards your local copy of the address, and returns to your initial stack frame where x was never modified. 然后返回,丢弃该地址的本地副本,并返回到x从未修改过的初始堆栈帧。

Inside the change_addr() function, you could have changed the content of the structure by accessing it through the pointer. 在change_addr()函数内部,可以通过通过指针访问结构来更改结构的内容 For example, ptr->data = 200 would have changed its data. 例如, ptr->data = 200将会更改其数据。 But if you want change a pointer address itself for some reason, then you'd have to create a pointer to the pointer, and pass that to a function taking the appropriate argument type. 但是,如果出于某种原因要更改指针地址本身,则必须创建指向该指针的指针,并将其传递给采用适当参数类型的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM