增加指向C的指针

时间:2022-02-20 21:16:34

I've been going through Kernighan and Ritchie book in C and got lost in pointers to pointers that point to char arrays.

我一直在阅读C中的Kernighan和Ritchie书,并指向指向char数组的指针。

Let's take this sample code:

我们来看看这个示例代码:

char str1[] = "This ";
char str2[] = "is ";
char str3[] = "a cat.";

char *ptr1 = str1;
char *ptr2 = str2;
char *ptr3 = str3;

char *ptrall[] = { ptr1, ptr2, ptr3 };

Question: How do I print all of the arrays using, say, while?

问题:如何使用,比如说,同时打印所有数组?

The book tells me (quote):

这本书告诉我(引用):

while (...){
   printf("%s\", *ptr++);}

So, this *ptr++ is supposed to increment pointers within **ptr. But When I try to build it in VS it says there's the "lvalue" mistake.

所以,这个* ptr ++应该在** ptr内增加指针。但是当我尝试在VS中构建它时,它表示存在“左值”错误。

What do I misunderstand here? How should I increment pointers stored in **? Am I limited to printf("%s", ptr[i++]) only?

我在这里误解了什么?我应该如何增加存储在**中的指针?我只限于printf(“%s”,ptr [i ++])吗?

2 个解决方案

#1


2  

Question: How do I print all of the arrays using, say, while?

问题:如何使用,比如说,同时打印所有数组?

It depends on how the array ptrall is defined.

这取决于数组ptrall的定义方式。

If the array ptrall is defined like

如果数组ptrall定义为

char *ptrall[] = { ptr1, ptr2, ptr3, NULL };

then it can be outputted the following way using a while loop

然后可以使用while循环以下列方式输出

#include <stdio.h>

int main( void )
{
    char str1[] = "This ";
    char str2[] = "is ";
    char str3[] = "a cat.";

    char *ptr1 = str1;
    char *ptr2 = str2;
    char *ptr3 = str3;

    char *ptrall[] = { ptr1, ptr2, ptr3, NULL };

    char **ptr = ptrall;

    while (*ptr) printf("%s", *ptr++);
    putchar('\n');

    return 0;
}

The program output is

程序输出是

This is a cat.

If the array ptrall is defined as in your question that is like

如果数组ptrall在你的问题中被定义为

char *ptrall[] = { ptr1, ptr2, ptr3 };

then it can be outputted the following way using a while loop

然后可以使用while循环以下列方式输出

#include <stdio.h>

int main( void )
{
    char str1[] = "This ";
    char str2[] = "is ";
    char str3[] = "a cat.";

    char *ptr1 = str1;
    char *ptr2 = str2;
    char *ptr3 = str3;

    char *ptrall[] = { ptr1, ptr2, ptr3 };

    char **ptr = ptrall;

    while (ptr != ptrall + sizeof( ptrall ) / sizeof( *ptrall ) ) printf("%s", *ptr++);
    putchar('\n');

    return 0;
}

The program output is the same as shown above.

程序输出与上面显示的相同。

As for the expression used in this call

至于此调用中使用的表达式

printf("%s", *ptrall++); 

then array designators are non-modifiable lvalues. You may not apply preincrement and postincrement operators to an array designator.

那么数组指示符是不可修改的左值。您可能不会将preincrement和postincrement运算符应用于数组指示符。

#2


1  

To use this you need to terminate your array of pointers;

要使用它,您需要终止指针数组;

char *ptrall[] = {ptr1, ptr2, ptr3, NULL};

for(char**ptr=ptrall;*ptr;) printf("%s\n",*ptr++);

#1


2  

Question: How do I print all of the arrays using, say, while?

问题:如何使用,比如说,同时打印所有数组?

It depends on how the array ptrall is defined.

这取决于数组ptrall的定义方式。

If the array ptrall is defined like

如果数组ptrall定义为

char *ptrall[] = { ptr1, ptr2, ptr3, NULL };

then it can be outputted the following way using a while loop

然后可以使用while循环以下列方式输出

#include <stdio.h>

int main( void )
{
    char str1[] = "This ";
    char str2[] = "is ";
    char str3[] = "a cat.";

    char *ptr1 = str1;
    char *ptr2 = str2;
    char *ptr3 = str3;

    char *ptrall[] = { ptr1, ptr2, ptr3, NULL };

    char **ptr = ptrall;

    while (*ptr) printf("%s", *ptr++);
    putchar('\n');

    return 0;
}

The program output is

程序输出是

This is a cat.

If the array ptrall is defined as in your question that is like

如果数组ptrall在你的问题中被定义为

char *ptrall[] = { ptr1, ptr2, ptr3 };

then it can be outputted the following way using a while loop

然后可以使用while循环以下列方式输出

#include <stdio.h>

int main( void )
{
    char str1[] = "This ";
    char str2[] = "is ";
    char str3[] = "a cat.";

    char *ptr1 = str1;
    char *ptr2 = str2;
    char *ptr3 = str3;

    char *ptrall[] = { ptr1, ptr2, ptr3 };

    char **ptr = ptrall;

    while (ptr != ptrall + sizeof( ptrall ) / sizeof( *ptrall ) ) printf("%s", *ptr++);
    putchar('\n');

    return 0;
}

The program output is the same as shown above.

程序输出与上面显示的相同。

As for the expression used in this call

至于此调用中使用的表达式

printf("%s", *ptrall++); 

then array designators are non-modifiable lvalues. You may not apply preincrement and postincrement operators to an array designator.

那么数组指示符是不可修改的左值。您可能不会将preincrement和postincrement运算符应用于数组指示符。

#2


1  

To use this you need to terminate your array of pointers;

要使用它,您需要终止指针数组;

char *ptrall[] = {ptr1, ptr2, ptr3, NULL};

for(char**ptr=ptrall;*ptr;) printf("%s\n",*ptr++);