带有Char数组的结构是打印垃圾...如何修复?

时间:2014-12-08 20:55:51

标签: c arrays struct

我有以下代码。当我打印出我分配给结构中字符数组的数据时,它打印出一堆垃圾,除非我在数组中添加一个额外的字符并放入" / 0"。这个问题后来我需要将出生日期和月份作为整数(4个字节)来获取。有谁能告诉我如何打印出适当的数据?我需要将char数组作为数据大小的精确内存大小。

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

struct Info
{
        char studentID[6];
        char firstName[5];
        char lastName[4];
        char birthDay[2];
        char birthMonth[2];
        char birthYear[4];
};

void printFunction( struct Info studentInfo );

int main()
{
        struct Info studentInfo = {"999999", "first", "last", "01", "07", "1993"};
        void *baseptr;
        asm("movl %%ebp,%0;":"=r"(baseptr));
        printf("The value of the basepointer main:\n");
        printf("%p\n", baseptr);
        printf("%-15s %s \n", "Student ID:", studentInfo.studentID);
        printf("%-15s %s \n", "First Name:", studentInfo.firstName);
        printf("%-15s %s \n", "Last Name:", studentInfo.lastName);
        printf("%-15s %s \n", "Birth Day:", studentInfo.birthDay);
        printf("%-15s %s \n", "Birth Month:", studentInfo.birthMonth);
        printf("%-15s %s \n", "Birth Year:", studentInfo.birthYear);

        printFunction( studentInfo );
        return 0;
}

void printFunction( struct Info studentInfo )
{
        printf("The value of basepointer printFunction is:\n");
        int *baseptr;
        asm("movl %%ebp,%0;":"=r"(baseptr));
        printf("%p\n", baseptr);
        printf("The value at basepointer address is:\n");
        printf("%p\n", *baseptr);
        printf("%-25s %p \n", "Address of Student ID:", &studentInfo.studentID);
        printf("%-25s %p \n", "Address of First Name:", &studentInfo.firstName);
        printf("%-25s %p \n", "Address of Last Name:", &studentInfo.lastName);
        printf("%-25s %p \n", "Address of Birth Day:", &studentInfo.birthDay);
        printf("%-25s %p \n", "Address of Birth Month:", &studentInfo.birthMonth);
        printf("%-25s %p \n", "Address of Birth Year:", &studentInfo.birthYear);
        printf("%s %x \n", "The address of my birth day and month is at address: ", &studentInfo.birthDay );
        printf("%s \n", "The integer value of my birth day and month is: ");
}

4 个答案:

答案 0 :(得分:2)

更改结构定义,其大小足以容纳C字符串终止字符:\0

struct Info
{
        char studentID[7];
        char firstName[50];
        char lastName[50];
        char birthDay[3];
        char birthMonth[3];
        char birthYear[5];
};

显示的尺寸长度是任意的,但为了说明您的使用案例,已足够。

在内存中,成员:studentID[6]仅在包含6个字符的足够空间中创建,即&#34; 999999&#34;,而不是 所有重要 < / strong> 字符串终止字符: \0。 C实际上没有_string typeP。相反,在C中,字符串被定义为由&#39; \ 0&#39;

终止的字符数组

所以,studentID [7]看起来像这样:

|'9'|'9'|'9'|'9'|'9'|'9'|0|  

重要的一点是,在创建字符串变量(或char数组)时,例如:

char string[10]  = {0};//space for 10 char

然后填写类似:

strcpy(string, "nine char"); //9 spaces used  

null终结符作为调用strcpy()的函数附加,为您提供:

|'n'|'i'|'n'|'e'|' '|'c'|'h'|'a'|'r'|0| //9 characters AND a NULL terminator

答案 1 :(得分:1)

在你的结构中

struct Info
{
        char studentID[6];
        char firstName[5];
        char lastName[4];
        char birthDay[2];
        char birthMonth[2];
        char birthYear[4];
};

变量没有足够的内存来存储空终止符。因此,打印为字符串是错误的。

要避免,请始终再允许char存储空终止符。使用相同的初始化程序集,

struct Info
{
        char studentID[7];
        char firstName[6];
        char lastName[5];
        char birthDay[3];
        char birthMonth[3];
        char birthYear[5];
};

应该可以正常工作。

答案 2 :(得分:0)

也许你想要这个:

printf("Birth Day: %c%c \n", studentInfo.birthDay[0], studentInfo.birthDay[1]);

答案 3 :(得分:0)

如果要在此结构中打印出字符值,并且不想将结构更改为包含以空字符结尾的字符串(正如您在注释中所建议的那样),那么您必须告诉{{1限制要打印的字符数。

通常printf会打印,直到遇到空终止符,但您的结构不包含任何空终止符,因此您必须指定最大字段宽度:

%s

您可以通过编程方式找到此值,而不是依赖于幻数:

 printf("%-15s %.6s \n", "Student ID:", studentInfo.studentID);
 //             ^^

您可以选择将所有这些行包装在宏中:

 printf("%-15s %.*s \n", "Student ID:", 
        (int)(sizeof studentInfo.studentID), studentInfo.studentID);

NB。此结构的初始值设定项是正确的:在C中,允许从字符串文字初始化char数组,该字符串文字包含与数组大小完全一样多的非空字符;并使用这些字符填充char数组。 (这在C ++中是不允许的。)