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

时间:2013-02-25 03:18:26

标签: c printing malloc repeat

我的代码有效,但打印效果不佳。这是我的意见:

请输入1表示字符,输入2表示整数,3表示浮点数,4表示单词

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

我不知道如何让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;
    }
}
}

2 个答案:

答案 0 :(得分:2)

因为word1,word2和word3都在同一个联合MESSAGE_CONTENT中,所以它们存储在同一个内存中。所以每当你读到其中一个时,所有这些看起来都会被你读到的最后一个值所覆盖。

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

你应该尝试使用“char word [3] [6];”在工会。这样你就可以存储所有三个单词。您还需要确保每个单词都有NULL字符的空间。你通常无法逃脱,但工会有更多的空间。

其中任何一个都会让你更接近你想要的东西,(当然,你的代码中的其他错误还不能承受):

备选方案#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;

答案 1 :(得分:0)

由于代码的以下部分,您输入错误(未按预期)输入:

case 4:
        scanf("%s", &cache[i].MESSAGE_CONTENT.word1);
        scanf("%s", &cache[i].MESSAGE_CONTENT.word2);
        scanf("%s", &cache[i].MESSAGE_CONTENT.word3);
        break;

根据 scanf功能语法,参数为:
1.参数字符串;
2.对象的地址。

注意第二个(对象的地址
as cache [i] .MESSAGE_CONTENT.word == char word1 [5] == 数组上的const指针,这已经是一个地址了,所以在我们的代码中你应该ommit&amp;,

case 4:
            scanf("%s", cache[i].MESSAGE_CONTENT.word1);
            scanf("%s", cache[i].MESSAGE_CONTENT.word2);
            scanf("%s", cache[i].MESSAGE_CONTENT.word3);
            break;