【问题标题】:Array with only 1 element storing more than it should仅包含 1 个元素的数组存储超出其应有的存储量
【发布时间】:2017-06-02 14:47:32
【问题描述】:

我的代码如下:

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

typedef struct{

    char ID[1];
    char Content[4];
}

int main(){

    DATA z[1];
    DATA t;
    int c1, c2;

    scanf("%c",z[1].ID);
    scanf("%s",z[1].Content);

    printf("\n");

    c1=strlen(z[1].ID);
    c2=strlen(z[1].Content);

    t=z[1];

    printf("t:\n");
    printf("%s\n",t.ID);
    printf("%s\n",t.Content);

    printf("\n");

    printf("z:\n");
    printf("%s\n",z[1].ID);
    printf("%s\n",z[1].Content);

    printf("\n");

    printf("Length of z's ID: %d.\n",c1);
    printf("Length of z's Content: %d.\n",c2);
}

这就是我执行它时得到的:

8
3092

t:
83092
3092

z:
83092
3092

Length of z's ID: 5.
Length of z's Content: 4.

我不知道为什么 ID 应该只有 1 个元素。 请帮忙,因为我需要为我正在处理的项目解决这个问题。 谢谢。

【问题讨论】:

  • 更新代码使其能够编译。
  • char Content[4] 将只保存四个chars。但是当您输入:3092 时,将存储五个 chars,因为在使用 %s 转换说明符时,scanf() 会自动添加 \0 终止符。在将%s 与scanf() 一起使用时,您应该始终指定最大宽度以避免缓冲区溢出:在您的情况下为%3s。
  • 贴出的代码无法编译!除此之外,'typedef' 必须以分号 ; 结尾,并且 typedef 缺少新的类型名称。将结构定义与 typedef 语句分开的另一个原因。
  • 此语句:t=z[1]; 只是分配地址,而不是 z[1] 数组项的内容。注意:这也是未定义的行为,因为它正在访问数组末尾之外的内存z[] 请记住,在 C 中,数组偏移量从 0 开始并扩展到(数组中的元素数 -1)建议:memcpy( &amp;t, &amp;z[0], sizeof( t ) );
  • 关于此语句:(已针对有效偏移进行更正)c1=strlen(z[0].ID); 无法获取单个字符的长度(通过strlen())。注意:实际长度始终为 1,但函数:strlen() 在遇到 NUL 字节('\0')之前不会停止。

标签: c arrays struct


【解决方案1】:

我认为有几个问题。

首先,您使用长度为 1 的 z 上的索引 1。您应该使用索引 0。

其次,您使用 %s 打印 ID,它需要一个以空字符结尾的字符数组。您的角色 ID 不是以 null 结尾的,因此它的读数比您预期的要多(它在内容中继续存在)。请注意,内容实际上是空终止的,因为您使用了%s 而不是%c。

尝试将printf("%s\n",t.ID); 更改为printf("%c\n",t.ID);。

编辑: 我监督您在长度为 4 的数组中存储 5 个字符(4 个字符和空终止符)。正如@David Bowling 建议的那样,您应该使用%3s 或至少更大的数组大小。 感谢@David Bowling 指出这一点!

请注意,我还没有对此进行测试,因为您的示例代码仍然无法正常工作。

【讨论】:

  • 在这些更改之后,它给了我这个:teste.c: In function ‘main’: teste.c:31:11: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=] printf("%c\n",t.ID); ^ teste.c:37:11: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=] printf("%c\n",z[1].ID); ^
  • 请注意,由于用户输入和未能在%s 在scanf() 中指定最大宽度,OP 具有 UB。 See my comment above
【解决方案2】:

以下建议的代码:

  1. 干净编译
  2. 将cmets中列出的问题更正到问题中
  3. 执行所需的功能

现在是代码:

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

typedef struct{

    char ID[1];
    char Content[4];
} DATA;

int main( void )
{
    DATA z[1];
    DATA t;
    size_t c1;
    size_t c2;

    if( 1 != scanf("%c",  &(z[0].ID[0]) ) )
    {
        perror( "scanf for character failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    if( 1 != scanf("%3s",  z[0].Content) )
    {
        perror( "scanf for content string failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    printf("\n");

    c1=sizeof(z[0].ID);
    c2=strlen(z[0].Content);

    memcpy( &t, z, sizeof( t ) );

    printf("t: ID:%c Content:%s\n\n", t.ID[0], t.Content);
    printf("z: ID:%c Content:%s\n\n", z[0].ID[0], z[0].Content);

    printf("Length of z's ID: %lu.\n",c1);
    printf("Length of z's Content: %lu.\n",c2);
}

使用此输入运行建议的代码:

a
bcd

或者这个输入:

a bcd

或者这个输入:

abcd

产生这个输出:

t: ID:a Content:bcd

z: ID:a Content:bcd

Length of z's ID: 1.
Length of z's Content: 3.

【讨论】:

    【解决方案3】:

    z[1].ID 等于您的代码中的8。你的问题是你如何打印你的价值观。

    这里:

    printf("%s\n",z[1].ID);
    printf("%s\n",z[1].Content);
    

    您请求printf 以%s 格式将char 类型变量z[1].ID 打印为字符数组(char*)。这种格式可以让您打印每个字符,直到达到空字符 \0。

    因此,它显示字符串,但它也与内存中的内容连接。实际上,struct 中包含的变量在内存中是串联的。在您的情况下,z[1].ID 与 z[1].Content 连接。

    因此,为了避免该问题,您必须将 printf 的格式更改为 z[1].ID 和 %c。

    和

    注意你如何使用你的数组DATA z[1];。

    请记住,C 中数组的可达范围是 [0; n-1]。

    在您的代码中,您需要更改“取消引用”您的数组的每个语句。

    这是一个例子:

    scanf("%c",z[1].ID);
    scanf("%s",z[1].Content);
    

    到

    scanf("%c",z[0].ID);
    scanf("%s",z[0].Content);
    

    如果您的数组仍然这样声明:DATA z[1];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 2014-12-07
      • 2015-06-09
      相关资源
      最近更新 更多