简体   繁体   English

在C中将字符串作为参数传递

[英]Passing string as an argument in C

I am having a function: 我有一个功能:

int getparam(char *gotstring)

and i am passing a string argument to it, like char *sendstring = "benjamin" 我正在传递一个字符串参数,如char *sendstring = "benjamin"

Instead of the above declaration I can use, 而不是我可以使用的上述声明,

int getparam(char gotstring[])

Question: Which one is better? 问题:哪一个更好? And if I have to use int getparam(char gotstring[]) what are all the other changes I have to make to the existing function? 如果我必须使用int getparam(char gotstring[])那么我必须对现有函数做出的所有其他更改是什么?

int getparam(char gotstring[]) and int getparam(char* gotstring) are identical. int getparam(char gotstring[])int getparam(char* gotstring)是相同的。 Personally, I would recommend the latter syntax, because it better describes what is actually going on. 就个人而言,我会推荐后一种语法,因为它更好地描述了实际发生的事情。 The getparam function only has a pointer to the string; getparam函数只有一个指向字符串的指针 ; it has no knowledge about the actual array size. 它不了解实际的数组大小。 However, that is just my opinion; 但是,这只是我的意见; either will work. 要么会奏效。

In this case they mean the same thing, and you do not need to change the remainder of your function. 在这种情况下,它们意味着相同的事情,您不需要更改函数的其余部分。 But be aware that in general, arrays and pointers are different things. 但请注意,通常,数组和指针是不同的东西。

Neither is better, really. 两者都不是更好,真的。 It's what you're more comfortable with. 这是你更舒服的东西。 Idiomatically, it is more common to use char *arg instead of char arg[] (think strcmp, strcpy, etc). 在惯用法中,更常见的是使用char * arg而不是char arg [](想想strcmp,strcpy等)。

The best way to accept a string argument is 接受字符串参数的最佳方法是

int getparam(const char *gotstring);

You can then call this using a literal string: 然后,您可以使用文字字符串调用它:

int main(void)
{
  int x = getparam("is this a parameter string?");
  return 0;
}

Or a character array: 或者一个字符数组:

int main(void)
{
  char arg[] = "this might be a parameter string";
  int x = getparam(arg);
  return 0;
}

The use of const on the argument pointer indicates to the caller that the argument is read-only inside the function, which is very nice information. 在参数指针上使用const指示调用者该参数在函数内是只读的,这是非常好的信息。

Since arrays are sent by reference in C both 因为数组是通过C中的引用发送的

int getparam(char *gotstring)

and

int getparam(char gotstring[])

means the same. 意思是一样的。 But first one is used more. 但首先使用的是更多。 So using it makes you a good C citizen. 所以使用它会让你成为一名优秀的C公民。

This is called passing by reference and how the name states it means to pass just a reference of the variable (in this case a char* ). 这称为通过引用传递以及名称如何表示它仅表示传递变量的引用 (在本例中为char* )。

Either solutions will work fine, what will actually happen is that, when void functionName(char* string) is called, the address of the first char in memory will be saved on stack and passed as a parameter to the function. 两种解决方案都可以正常工作,实际发生的是,当void functionName(char* string) ,内存中第一个char的地址将保存在堆栈中并作为参数传递给函数。

Consequently any edit to the variable inside the function would edit also the original variable. 因此,对函数内部变量的任何编辑都将编辑原始变量。 In this case, since you need to pass an array you just have this option. 在这种情况下,由于您需要传递一个数组,您只需拥有此选项。

For standard types like int , float you can distinguish them: 对于像intfloat这样的标准类型,你可以区分它们:

void passByValue(int i);
void passByReference(int &i);

The difference is that in the former case value is copied into stack when calling the function, while in the latter just a reference (pointer) to the value is passed. 区别在于前者在调用函数时将值复制到堆栈中,而在后者中只传递值的引用(指针)。

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

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