-1

我在理解指针在特定情况下如何工作时遇到了一些麻烦。我的困惑的细节概述如下。

我创建了一个名为 buffer 的 8 字节字符串的字符数组,我试图使用 fread() 将文件中的 8 个字节加载到这个 8 字节字符串数组的元素之一中。

我位于 /c/file.txt 的文件的内容很简单: TESTTEST

我希望程序将 TESTTEST 加载到缓冲区 [1] 而不是值 ijklmnop 中,仅此而已。

我知道缓冲区实际上是一个指针数组,在这种情况下,它只是三个连续的内存地址,其中包含三个不以空值结尾的连续字节数组的第一个元素的地址。

我将其可视化为一行中的 24 个内存地址,其中包含 ABCDEFGHijklmnopQRSTUVWX 的二进制值,末尾没有 \0。

buffer[0]的地址指向一个指向A的地址,buffer[1]的地址指向一个指向i的地址,buffer[2]的地址指向一个指向Q的地址。

我看到 &buffer[1] 的 fread() 将 8 个字节读入存储在缓冲区 [1] 中的地址的位置。

我在这个 fread() 上尝试过各种尝试,使用 &buffer+1、buffer+1、(buffer+1) 等等,但大多数都给了我分段错误;&buffer[1] 似乎几乎可以满足我的要求。

尝试执行 printf("buffer[1] as %%s is %s\n", buffer[1]); 在 fclose() 之前接近尾声时出现分段错误。

我接近理解,但有些事情不太正确,我没有掌握一些我需要的基本概念。

我不确定终止 (buffer+1)[8] 是否溢出到缓冲区 [2] 的第一个地址,或者有什么影响。

总的来说,我有点困惑,任何帮助将不胜感激。

谢谢!

这是我的整个程序:

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

int main()
{
    FILE *fp;
    char c[] = "OFFONOFF";
    char *buffer[] = {"ABCDEFGH", "ijklmnop", "QRSTUVWX"};
    char *t;

    printf("the value in file /c/file.txt is TESTTEST, only those 8 bytes\n");
    printf("the ls listing for /c/file.txt shows 9, I am not sure where the other character comes from\n");

    printf("the value as %%s of buffer[0] is %s\n", buffer[0]);
    printf("the value as %%s of buffer[1] is %s\n", buffer[1]);
    printf("the value as %%s of buffer[2] is %s\n", buffer[2]);
    printf("the sizeof(buffer[2]) is %d\n", sizeof(buffer[2]));

    fp = fopen("/c/file.txt", "r");
    fseek(fp, SEEK_SET, 0);

    printf("the value as %%c of buffer[1][3] is %c\n", buffer[1][3]);
    printf("the memory address as %%p for  buffer+1 is %p\n", buffer+1);

    fread(&buffer[1], strlen(c), 1, fp);

    /* I am attempting to terminate the fread into &buffer[1] with a \0, removing this doesn't change much */
    (buffer+1)[8] = "\0";

    printf("the memory addreses below are all in %%p format\n");
    printf("the memory address of buffer is %p\n", buffer);
    printf("memory address of buffer[1] is %p\n", buffer[1]);
    printf("memory address of &buffer is %p\n", &buffer);
    printf("memory address of &buffer+1 is %p\n", &buffer+1);
    printf("memory address of &buffer[1] is %p\n", &buffer[1]);
    printf("memory address of buffer is %p\n", buffer);
    printf("memory address of buffer[15] is %p\n", buffer[15]);
    printf("memory address of buffer+15 is %p\n", buffer+15);
    printf("memory address of *(buffer+1) is %p\n", *(buffer+1));
    printf("the sizeof(buffer[0]) is %d\n", sizeof(buffer[0]));
    printf("the sizeof(*(buffer+1)) is %d\n", sizeof(*(buffer+1)));
    printf("the sizeof(buffer) is %d\n", sizeof(buffer));
    printf("the value of *(buffer+0) as %%s is %s\n", *(buffer+0));
    printf("the value of *buffer, the first element of the array of strings, as %%s is %s\n", *buffer);
    printf("the value of buffer+1, the first element of the array of strings, as %%s is %s\n", buffer+1);
    printf("buffer[0] as %%s is %s\n", buffer[0]);
    printf("buffer+1 as %%s is %s\n", buffer+1);
    printf("buffer[2] as %%s is %s\n", buffer[2]);
    printf("buffer is %s\n", buffer+1);
    printf("sizeof(*buffer) is %d\n", sizeof(*buffer));

    fclose(fp);

    return(0);
}

这是确切的输出:

the value in file /c/file.txt is TESTTEST, only those 8 bytes
the ls listing for /c/file.txt shows 9, I am not sure where the other character comes from
the value as %s of buffer[0] is ABCDEFGH
the value as %s of buffer[1] is ijklmnop
the value as %s of buffer[2] is QRSTUVWX
the sizeof(buffer[2]) is 8
the value as %c of buffer[1][3] is l
the memory address as %p for  buffer+1 is 0x7ffff7b23a28
the memory addreses below are all in %p format
the memory address of buffer is 0x7ffff7b23a20
memory address of buffer[1] is 0x5453455454534554
memory address of &buffer is 0x7ffff7b23a20
memory address of &buffer+1 is 0x7ffff7b23a38
memory address of &buffer[1] is 0x7ffff7b23a28
memory address of buffer is 0x7ffff7b23a20
memory address of buffer[15] is 0x400674
memory address of buffer+15 is 0x7ffff7b23a98
memory address of *(buffer+1) is 0x5453455454534554
the sizeof(buffer[0]) is 8
the sizeof(*(buffer+1)) is 8
the sizeof(buffer) is 24
the value of *(buffer+0) as %s is ABCDEFGH
the value of *buffer, the first element of the array of strings, as %s is ABCDEFGH
the value of buffer+1, the first element of the array of strings, as %s is TESTTEST

@
buffer[0] as %s is ABCDEFGH
buffer+1 as %s is TESTTEST

@
buffer[2] as %s is QRSTUVWX
buffer is TESTTEST

@
4

1 回答 1

0

该行fread(&buffer[1], strlen(c), 1, fp);似乎覆盖了指针本身。

看来您想要的是一个二维数组,而不是指向字符串文字的指针数组(顺便说一下,在大多数现代平台上它们是只读的:char *abc="abc"; abc[0]='d'通常是段错误):

char buffer[][9] = {"ABCDEFGH", "ijklmnop", "QRSTUVWX"};
于 2013-05-16T11:08:53.717 回答