简体   繁体   English

将指针传递给函数并在函数内部获取其值

[英]Passing pointer to function and getting its value inside that function

Pseudocode below shows what I am trying to achieve. 下面的伪代码显示了我正在尝试实现的目标。 I am new to C++ and pointers, as you can guess from this question. 我是C ++和指针的新手,您可以从这个问题中猜到。 So, I don't know what to pass to function [1], what to get from parameter [2], and what to use to make comparison [3]. 因此,我不知道该向函数[1]传递什么,从参数[2]中得到什么,以及用于进行比较的内容[3]。

Please do not ask why I am passing a pointer instead of the value itself, the function only accepts pointers for this task. 请不要问为什么我传递一个指针而不是值本身,该函数仅接受此任务的指针。

I need some help. 我需要一些帮助。

main {
   string someString = ..
   function(pointer of the string above [1]*);
}

function (parameter[2]*) {
   if (parameter[3]* == "Hello World") {
      print("okay");
   }
}

It depends on what language you are using. 这取决于您使用的语言。

In C++ the program can look like 在C ++中,程序看起来像

#include <iostream>
#include <string>

void check( const std::string &s )
{
    if ( s == "Hello World" ) 
    {
        std::cout << "okay" << '\n';;
    }
}

int main() 
{
   std::string someString = "Hello World";

    check( someString );
}

In C a corresponding program can look like 在C中,相应的程序看起来像

#include <stdio.h>
#include <string.h>

void check( const char *s )
{
    if ( strcmp( s, "Hello World" ) == 0 ) 
    {
        puts( "okay" );
    }
}

int main( void ) 
{
   char someString[] = "Hello World";

    check( someString );
}

A similar program using a character array is also can be written in C++. 也可以使用C ++编写使用字符数组的类似程序。

Instead of the declaration of the array 代替数组的声明

   char someString[] = "Hello World";

you could declare a pointer to the string literal like 您可以声明一个指向字符串文字的指针,例如

   const char *someString = "Hello World";

You have a couple of options here: 您可以在此处选择两个选项:

  • Pass the string variable by reference 通过引用传递字符串变量
  • Make the variable a pointer itself 使变量本身成为指针

By passing the variable by reference, you will change the main string, unless your function accepts the pointer as constant. 通过引用传递变量,您将更改主字符串,除非您的函数接受指针为常量。 You can do that by doing the following: 您可以通过执行以下操作来做到这一点:

main {
   string someString = ...;

   // Notice here we are passing the variable by reference
   // We are passing the variable's address to the function
   function(&somestring);
}

And with the second option, you can do the following: 使用第二个选项,您可以执行以下操作:

main {
   string* someString = new string(...);

   // Pass it normally, as it will be accepted
   function(someString);
}

And in the function itself you can use that value as it follows: 在函数本身中,您可以按以下方式使用该值:

function(paramString) {
   if(*paramString == "Hello World") {
       do something..
   }
}

Please read some more information about function parameters and arguments, and about pointers itself. 请阅读有关函数参数和参数以及指针本身的更多信息。

The links below will give you a head start. 以下链接将为您提供良好的开端。

https://www.w3schools.com/cpp/cpp_function_param.asp https://www.w3schools.com/cpp/cpp_pointers.asp https://www.w3schools.com/cpp/cpp_function_param.asp https://www.w3schools.com/cpp/cpp_pointers.asp

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

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