c - 在 C 中错误地打印输出;故障和重复

标签 c printing malloc repeat

我的代码可以工作,但打印不正确。这是我的输入:

请输入 1(字符)、2(整数)、3( float )、4(单词)

1 abcdefghijklmnop 3 123.4 45.54 6.0 7890.09876 2 123 34 23 12345 4 aaaaa bbbbb ccccc SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS

我不知道如何让它在 cccccc 之后停止,就像它应该的那样。我必须随机输入一些内容来填充空间,然后才能通过。我很确定这与不使用 malloc 有关。

然后这是输出:

类型 1:abcdefghijklmnop 类型 3:123.400002/45.540001/6.000000/7890.098633 类型 2:123、34、23、12345 类型 4: ccccc ccccc ccccc 类型1:ssssssssssssssss

在类型 4 上,它应该是 aaaaa bbbbb ccccc,但它却这样做了。而且在类型 1 上,它不会显示,但它打印起来就像这个奇怪的方形故障看起来的东西。

这是我的代码。共 3 个文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lab5.h"
#include "lab5dispatch.c"
#define MAX_ENTRIES 16

int main()
{
MESSAGE cache[MAX_ENTRIES];
int i = 0;
printf("Please enter in 1 for characters, 2 for ints, 3 for floats and 4 for words\n");


while (scanf("%d", &cache[i].messageType) != EOF && i < MAX_ENTRIES) 
{
   switch(cache[i].messageType)
    {
    case 1:
        scanf("%16s", &cache[i].MESSAGE_CONTENT.charPointer);
        break;
    case 2:
        scanf("%d", &cache[i].MESSAGE_CONTENT.theInts[0]);
        scanf("%d", &cache[i].MESSAGE_CONTENT.theInts[1]);
        scanf("%d", &cache[i].MESSAGE_CONTENT.theInts[2]);
        scanf("%d", &cache[i].MESSAGE_CONTENT.theInts[3]);
        break;
    case 3:
        scanf("%f",  &cache[i].MESSAGE_CONTENT.theFloats[0]);
        scanf("%f",  &cache[i].MESSAGE_CONTENT.theFloats[1]);
        scanf("%f",  &cache[i].MESSAGE_CONTENT.theFloats[2]);
        scanf("%f",  &cache[i].MESSAGE_CONTENT.theFloats[3]);
        break;
    case 4:
        scanf("%s", &cache[i].MESSAGE_CONTENT.word1);
        scanf("%s", &cache[i].MESSAGE_CONTENT.word2);
        scanf("%s", &cache[i].MESSAGE_CONTENT.word3);
        break;
    }
    i++;
}
message_dispatcher(cache, i);
}

这是第二个文件。

#ifndef LAB5_H_ /* to prevent re-definitions */
#define LAB5_H_ /* that cause errors */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//the unsigned short is to store what type of data the main struct will store

typedef struct MESSAGE
{
unsigned short int messageType;
union
{
    char * charPointer; //this is for the string
    int theInts[4];
    float theFloats[4];
    char word1[5]; //can probably use a 2d array here but that's too complicated right now haha
    char word2[5];
    char word3[5];
} MESSAGE_CONTENT;
} MESSAGE;

这是最后一个文件。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lab5.h"
void message_dispatcher( MESSAGE msg[], int j ) {
int i;
for (i = 0; i < j; i++)
{
    switch(msg[i].messageType)
    {
    case 1:
        printf("Type 1: %s\n", &msg[i].MESSAGE_CONTENT.charPointer);
        break;

    case 2:
        printf("Type 2: %d, %d, %d, %d\n", msg[i].MESSAGE_CONTENT.theInts[0],
                msg[i].MESSAGE_CONTENT.theInts[1], msg[i].MESSAGE_CONTENT.theInts[2],
                msg[i].MESSAGE_CONTENT.theInts[3]);
        break;

    case 3:
        printf("Type 3: %f/ %f/ %f/ %f \n", msg[i].MESSAGE_CONTENT.theFloats[0], msg[i].MESSAGE_CONTENT.theFloats[1], msg[i].MESSAGE_CONTENT.theFloats[2], msg[i].MESSAGE_CONTENT.theFloats[3]);
        break;

    case 4:
        printf("Type 4: %s %s %s\n", msg[i].MESSAGE_CONTENT.word1, msg[i].MESSAGE_CONTENT.word2, msg[i].MESSAGE_CONTENT.word3);
        break;
    }
}
}

最佳答案

因为 word1、word2 和 word3 都位于同一个 union MESSAGE_CONTENT 中,所以它们存储在同一内存中。因此,每次您读取其中一个值时,所有这些值看起来都会被您读取的最后一个值覆盖。

看看http://en.cppreference.com/w/cpp/language/union

您应该尝试使用“char word[3][6];”在联盟中。这样你就可以存储所有三个单词。您还需要确保每个单词都有空字符的空间。通常情况下你不可能逃脱惩罚,但 union 中有更多的空间。

其中任何一个都会让您更接近您想要的,(当然,代码中存在其他错误也不受限制):

替代方案#1

typedef struct MESSAGE
{
    unsigned short int messageType;
    union
    {
        char * charPointer;
        int theInts[4];
        float theFloats[4];
        char theWords[3][6];
    } MESSAGE_CONTENT;
} MESSAGE;

替代方案#2

typedef struct MESSAGE
{
    unsigned short int messageType;
    union
    {
        char * charPointer;
        int theInts[4];
        float theFloats[4];
        struct {
            char word1[6];
            char word2[6];
            char word3[6];
        } WORDS;
    } MESSAGE_CONTENT;
} MESSAGE;

关于c - 在 C 中错误地打印输出;故障和重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15059545/

相关文章:

c - 替换字符串上的字母C语言

objective-c - 数学表达式评估 - 非常快 - 使用 objective-c

jsf - 提供PDF下载后自动打开打印机对话框

css - 浏览器对 CSS 页码的支持

c - 增加结构段错误的成员

c - 我们如何检测具有相同背景的图像的边缘?

c - 符号 __no_init __root C

c++ - 尝试打印时堆损坏

c - malloc 和 calloc 在使用上的区别

c - 在 C 的单个函数中处理多个 malloc 错误的推荐方法