简体   繁体   English

在C / C ++中将指针作为参数传递?

[英]Passing a Pointer as an Argument in C/C++?

How would one go about using the pointer parameter if given a function like this: 如果给定这样的函数,如何使用指针参数:

int testFunction(int *){  
}

I understand that a pointer is being passed as the parameter, but how do I use that pointer given that there is no variable name after the pointer? 我知道指针将作为参数传递,但是鉴于指针后面没有变量名,我该如何使用该指针?

You should name the argument. 您应该为参数命名。 See this link for a tutorial 请参阅此链接以获取教程

Name of function parameters can be omitted from function prototypes (declarations) 函数原型(声明)中可以省略函数参数的名称

int testFunction(int *);  

You can't omit name of parameters from function definition. 您不能从函数定义中省略参数名称。

You can get parameters from function's stack. 您可以从函数的堆栈中获取参数。

Before your function called, your program pushes parameters to stack. 在调用函数之前,程序会将参数压入堆栈。 So you can define some internal variable in your function, and pop this parameter from stack into this variable. 因此,您可以在函数中定义一些内部变量,然后将此参数从堆栈中弹出到该变量中。

For more information you have to research how do C functions work. 有关更多信息,您必须研究C函数如何工作。

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

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