c - 为什么递增指针可以工作,但将其视为数组会失败?第2部分

标签 c arrays pointers struct

我想我明白了问题所在:C 很臭。它以一种方式处理本地声明的指针,以另一种方式处理作为参数传递的指针。这是对 question 的回应我几天前发过帖子。我试图将其发布到那里,但该程序不适合。

// Test arrays and pointers

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

#define EOL "\n"

typedef struct
{
    int useless1;
    int useless2;
}   type1;

void function1(char* s)
{
    printf("Pointer to string.  ");
    while (*s)
        putchar(*s++);

    printf(EOL);
}

void function2(char* s)
{
    int i=0;
    printf("Index into string.  ");
    while (s[i])
        putchar(s[i++]);

    printf(EOL);
}

void functionX1(type1* p, int count)
{
    printf("Pointer to struct." EOL);
    for (int i=0; i<count; i++)
    {
        printf("index: %d  useless1: %d" EOL, i, p->useless1);
        printf("index: %d  useless2: %d" EOL, i, p->useless2);
        p++;
    }
    printf(EOL);
}

void functionX2(type1* p, int count)
{
    printf("Index into array of structures." EOL);
    for (int i=0; i<count; i++)
    {
        printf("index: %d  useless1: %d" EOL, i, p[i].useless1);
        printf("index: %d  useless2: %d" EOL, i, p[i].useless2);
    }
    printf(EOL);
}

void getptr1(char* s, char** ss)
{
    *ss = s;
}

void getptr2(type1* ptr, type1** pptr)
{
    *pptr = ptr;
}

int main(int argc, char** argv)
{
// Work with strings of characters

    char* s;

    function1( argv[0]);
    function2( argv[0]);
    function1(*argv   );
    function2(*argv   );

    getptr1(argv[0], &s);
    function1(        s);
    function2(        s);

    printf("main Pointer to string.  ");
    while (*s)
        putchar(*s++);
    printf(EOL);

    int i=0;
    printf("main Index into string.  ");
    while (s[i])
        putchar(s[i++]);
    printf(EOL);

// Work with array of structures
    #define count   3

    type1   a[count];

    int x1 = 123;
    int x2 = 789000;
    int kx = 111;

    for (int i=0; i<count; i++)
    {
        a[i].useless1 = x1;
        a[i].useless2 = x2;
        x1 +=  kx;
        x2 += (kx * 1000);
    }

//    functions1(&a   , count);     Fail
//    functions2(&a   , count);     Fail
    functionX1(&a[0], count);
    functionX2(&a[0], count);
    functionX1( a   , count);
    functionX2( a   , count);

    type1*  b;

    getptr2(a, &b);
    functionX1( b   , count);
    functionX2( b   , count);

    printf("main Pointer to struct." EOL);
    for (int i=0; i<count; i++)
    {
        printf("index: %d  useless1: %d" EOL, i, b->useless1);
        printf("index: %d  useless2: %d" EOL, i, b->useless2);
        b++;
    }
    printf(EOL);

    printf("main Index into array of structures." EOL);
    for (int i=0; i<count; i++)
    {
        printf("index: %d  useless1: %d" EOL, i, b[i].useless1);
        printf("index: %d  useless2: %d" EOL, i, b[i].useless2);
    }
    printf(EOL);
}

最佳答案

我不确定你的问题是什么,但这段代码可能没有达到你想要的效果:

printf("main Pointer to struct." EOL);
for (int i=0; i<count; i++)
{
    printf("index: %d  useless1: %d" EOL, i, b->useless1);
    printf("index: %d  useless2: %d" EOL, i, b->useless2);
    b++;
}
printf(EOL);

printf("main Index into array of structures." EOL);
for (int i=0; i<count; i++)
{
    printf("index: %d  useless1: %d" EOL, i, b[i].useless1);
    printf("index: %d  useless2: %d" EOL, i, b[i].useless2);
}
printf(EOL);

第一个循环更改了 b 的值,因此第二个循环不会从您认为的位置开始。

考虑这样的事情:

type1* bOrig = b; // store the original value
printf("main Pointer to struct." EOL);
for (int i=0; i<count; i++)
{
    printf("index: %d  useless1: %d" EOL, i, b->useless1);
    printf("index: %d  useless2: %d" EOL, i, b->useless2);
    b++;
}
printf(EOL);
b = bOrig; // restore the original value

printf("main Index into array of structures." EOL);
for (int i=0; i<count; i++)
{
    printf("index: %d  useless1: %d" EOL, i, b[i].useless1);
    printf("index: %d  useless2: %d" EOL, i, b[i].useless2);
}
printf(EOL);

关于c - 为什么递增指针可以工作,但将其视为数组会失败?第2部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44794249/

相关文章:

c - 使用 volatile 变量的示例 C 函数

c - 函数应该有一个原型(prototype)

Java数组栈实现字符串转换和增长

c++ - 如何使这个运算符(operator)调用明确?

C++:使用 libcurl 和流

c - malloc的使用和free : invalid next size error

C:我尝试使用链表读取程序并统计关键字,但出现段错误

c - 将整数数组加载到 SIMD 寄存器中

arrays - 如何在 Core Data 中存储一组自定义(和重复)对象?

C 字符串删除 - 此代码如何工作?