簡體   English   中英

帶有Char數組的結構正在打印垃圾……如何解決?

[英]Struct with Char array is printing garbage… How to fix?

我有下面的代碼。 當我打印出分配給該結構中的字符數組的數據時,除非我在數組中添加額外的字符並放入“ / 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];
};

所示的大小在長度上是任意的,但是對於您的用例而言,足夠了。

在內存中,僅以足夠的空間包含6個字符(即“ 999999”)創建成員: studentID[6] ,而不創建所有重要的 字符串終止字符: \\0 C實際上沒有_string typeP。 相反,在C中,字符串定義為以'\\ 0'結尾的字符數組

因此,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  

空終止符被附加為調用strcpy()的函數,從而為您提供:

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

在你的結構中

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];
};

應該工作正常。

也許您想要這樣:

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

如果要在此結構中打印出字符值,並且不想將結構更改為包含以空字符結尾的字符串(如您在注釋中所建議的那樣),則必須告訴printf限制要輸入多少個字符打印。

通常%s打印直到遇到空終止符,但是您的結構不包含任何空終止符,因此必須指定最大字段寬度:

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

您可以通過編程找到此值,而不是依靠幻數:

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

您可以選擇將所有這些行包裝在宏中:

#define PRINT_ITEM(prompt, field) \
    printf("%-15s %.*s\n", prompt, (int)sizeof(field), field)

PRINT_ITEM("Student ID:", studentInfo.studentID);
PRINT_ITEM("First Name:", studentInfo.firstName);
PRINT_ITEM("Last Name:" , studentInfo.lastName);
// etc.

注意 這個結構的初始化方法是正確的:在C語言中,可以從包含與數組大小一樣多的非null字符的字符串文字中初始化char數組; 並且用這些字符填充char數組。 (這在C ++中是不允許的)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM