c - 带有 Char 数组的结构正在打印垃圾...如何解决?

标签 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: ");
}

最佳答案

用足以容纳 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 个字符,即“999999”,但不是所有重要 字符串终止符: \0。 C 实际上没有 _string typeP。相反,在 C 中,字符串被定义为字符数组以'\0'

结尾

因此,studentID[7] 将如下所示:

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

重要的一点,当您创建一个字符串变量(或一个字符数组)时,例如:

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

然后用类似的东西填充它:

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

空终止符作为调用 strcpy() 的函数附加,给你:

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

关于c - 带有 Char 数组的结构正在打印垃圾...如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27366737/

相关文章:

创建目录权限

c - 一台电脑有多少处理能力

javascript - javascript 函数无法生成精确的更改

c - 将字符串拆分为结构体的元素

c - 在没有 malloc 的情况下进入 char *str,没有段错误?

php - 如何在 php 中映射两个数组以便得到一个组合数组?

java - 在 Java 中我可以缩小数组以节省内存吗?

go - 具有相同接收器函数实现的两个结构,如何删除重复项

我可以制作一个结构,其中超过一半的空间是填充的吗?

c - 查找具有已知 pid 的进程的 '.text' 部分的范围