在 C 中增加字符串数组的值

Increasing Value of Array of Strings in C

我有一个作业,我将一个字符串传递给定义为 char *get_word( char **string_ptr);

的函数

当传递的字符串指向 '[=11=]' 字符时,我的 main 将停止调用该函数。因此我必须在函数内实现这一行:*string_ptr += strlen( *string_ptr );

这让我很困惑。我的印象是 *string_ptr 将是函数中传递的字符串。那么增加它的价值意味着什么呢?

另一个问题。如果 **string_ptr 是一个字符串数组,那么 *string_ptr 或只是 string_ptr 是什么?

好吧,首先,**string_ptr 在语义上并不能保证 string_ptr 将是一个字符串数组或一个字符指针数组。这在处理内存管理后就很清楚了。

据我所知,您正在向您的函数传递一个指向字符串的指针。 所以你做类似的事情

char* string = "my string in read-only memory";

foo(&string);

要访问指针指向的字符串的值,先取消引用指针,然后取消引用字符串。这解析为字符串的第一个字符。

**string_ptr 是字符串的第一个字符。

*string_ptr 是字符串

string_ptr 是指向字符串的指针。


有更清晰或至少更易于理解的方法来实现您想要实现的目标:

char* p = *string_ptr;

for(char* p = *string_ptr; *p != '[=11=]' p++);

您创建了指向字符串的新指针

然后增加它直到它指向保存 [=18=]

的地址

向变量添加更多 * 只是意味着它指向另一个东西。

char* ch; // ch points to char or string
char** ch; // ch points to char*
char*** ch; // ch points to char**

等等


*string_ptr would be the string that is being passed in the function. So what would it mean to increase the value of that?

你说得对。 增加指针的值通常意味着对变量执行指针运算。本质上它与增加常规变量相同,但这次是对地址值执行的,其中增加 1 意味着增加 1 * sizeof(type)

我演示了什么是递增指针及其作用。


*string_ptr += strlen( *string_ptr );

因为*string_ptr是字符串偏移量 这将使您的字符串指向自身的末尾。 所以如果你在那之前获得它的额外参考就好了。

I have an assignment where I pass a string into a function with the [prototype] char *get_word( char **string_ptr);

My main will stop calling the function when the string that was passed points to the '/0' character.

区分 stringchar 的空终止 array 很重要指向字符串(的第一个字符)的指针,这是将字符串传递给函数并从函数返回的方式,通常也是访问字符串的方式。指向字符串第一个字符的 char * 与字符串本身是分开的。因此,您的陈述只有在解释为 "My main will stop calling the function when the value of the pointer variable whose address was passed points to the '/0' character of the string."

时才有意义

Therefore I have to implement this line inside the function: *string_ptr += strlen( *string_ptr );

这确实会更新 string_ptr 指向的地址处的指针值,以便它指向它最初指向的字符串的终止符。但是,我倾向于假设那是 而不是 您真正想要的,因为这样就不需要多次调用该函数。如果函数无条件地执行该语句,则传递的指针将始终指向仅一次调用后的终止符。

This is very confusing to me. I am under the impression that *string_ptr would be the string that is being passed in the function. So what would it mean to increase the value of that?

*string_ptrstring_ptr 指向的任何内容。根据您提供的声明,这将是一个 char * 类型的指针。请记住:区分此类指针和它指向的事物(在本例中为字符串的第一个字符)很重要。增加指针的值只是让它指向字符串中后面的某个字符(只要你不增加太多)。如果将它增加字符串中字符的总数,那么它将指向终止符。

Another question. If **string_ptr is an array of strings then what would be *string_ptr or just string_ptr on it's own?

作为表达式,*string_ptr**string_ptr脱离上下文是没有意义的。但是,根据您提供的声明,表达式 *string_ptr 的计算结果为 string_ptr 指向的 char *。表达式 **string_ptr 的计算结果为 string_ptr 所指向的 char * 所指向的 charstring_ptr 本身只是函数参数的值,它被声明为指向 char.

的指针

但我认为您实际上是在询问函数原型,在这种情况下您问错了问题。通常的问题是关于参数 string_ptr 的类型 (char **) 是否表示字符串数组。答案在几个层面上是 "no",至少在一个层面上是 "maybe, sort of"。

从表面上看,类型 char ** 表示指向 char 的指针。这个 可以是 指向 char * 数组中第一个 char * 的指针,如果您将这样的数组作为相应的参数呈现,它就会是这样。无论如何,指向的 char * 是数组的成员并不确定,如果是,则不会传递有关该数组长度的信息。指向单个标量 char * 的指针与指向 char *.

的单成员数组的第一个也是唯一一个元素之间没有有意义的区别

当然,同样的区别也适用于下一级。有效的 char * 通常 指向字符串的第一个 char ,但您无法仅从类型中得知这一点。假设这样的指针是有效的,它可能会指向一个未终止的 char 数组,或者指向一个单一的标量 char。特别是对于 char * 类型,它也可能指向(in)任何其他类型对象的值的原始字节。