【问题标题】:Trying to print a struct element but getting blank. - C试图打印一个结构元素但得到空白。 - C
【发布时间】:2015-02-21 19:22:20
【问题描述】:

我有一个struct person,它具有以下元素,在data.h 中定义

typedef struct person{
    char firstName[20];
    char familyName[20];
    char telephoneNum[20];
    int type; // 0 = student / 1 = employee;
}newPerson;

我创建了一个person[MAX_PERSONS] 数组,该数组在我的menu() 函数中初始化。然后我有一个addFirstName(newPerson pers) 函数。但是,当我尝试使用 printFormat(newPerson pers)function 测试打印格式时,我得到空白,而不是输入的名称。

我在下面包含了 menu()、addFirstname(newPerson pers) 和 printFormat(newPerson pers) 函数。我想知道是否有人可以告诉我这样做的原因。任何帮助或指示将不胜感激。提前谢谢你。

int menu(){
    int num = 0;
    newPerson person[MAX_PERSONS]; 
    int option; // for user input for menu
    printf("\n\tPlease choose one of the following options to continue (0-9): ");   
    scanf("%d", &option );
    printf("\n\tYou selected %d\n", option);

    if (option == 0){ //program will close
        printf("\tProgram will now close.\n");
        exit(1);
    }   

    if (option == 1){ //program will ask for name input 
        addRecord(person[num]);
        printFormat(person[num]);
        char choice[0];
        printf("\n\t\tWould you like to enter another record? (y/n): ");
        scanf("%s", choice);    
        if (choice[0] == 'y'){
            num++;
            addRecord(person[num]);

        }
        if (choice[0] == 'n'){
            num++;
            mainMenu();
        }               
            /*

            IF YES, THEN NUM++
            THEN RUN ADDRECORD(PERSONNUM) AGAIN.    

            IF NO, THEN RETURN TO MAIN MENU.
            PRINTMENU
            THEN RUN MENU AGAIN
        */  
    }

    printf("\n\tNot a valid option, please try again // THE END OF MENU FUNCTION\n");
    return 0;
}

void addFirstName(newPerson pers){
    char firstName[20];
    printf("\n\tEnter first Name: ");
    scanf("%20s", firstName);
    strcpy(pers.firstName, firstName);
    printf("\n\tThe name entered is %s", pers.firstName);
}


void printFormat(newPerson pers){
    printf("\t\tThe name is %s", pers.firstName);
}

【问题讨论】:

  • 不要忽略scanf()的返回值。另外,scanf("%20s", firstName); 是错误的,应该是scanf("%19s", firstName);,不需要临时缓冲区,可以直接scanf("%19s", fpers.firstName); 避免strcpy()

标签: c string printing struct format


【解决方案1】:

这是因为您将结构传递给addFirstName按值,这意味着该函数接收到结构的副本。更改副本当然不会更改原件。

虽然 C 不支持通过引用传递参数,但它可以使用指针来模拟。因此,更改addFirstName 函数以接收指向结构的指针 作为其参数。

【讨论】:

    【解决方案2】:

    您的大问题是您通过值而不是指针传递结构。这导致您更改函数 addFirstName 内的原始对象的副本,而不是原始对象。您应该将其声明为:

    void addFirstName( newPerson* pers);
    void printFormat( newPerson* pers);
    

    例子:

    #include <stdio.h>
    
    void call_by_value(int x) {
        printf("Inside call_by_value x = %d before adding 10.\n", x);
        x += 10;
        printf("Inside call_by_value x = %d after adding 10.\n", x);
    }
    
    int main() {
        int a=10;
    
        printf("a = %d before function call_by_value.\n", a);
        call_by_value(a);
        printf("a = %d after function call_by_value.\n", a);
        return 0;
    }
    

    这将产生:

    a = 10 before function call_by_value.
    Inside call_by_value x = 10 before adding 10.
    Inside call_by_value x = 20 after adding 10.
    a = 10 after function call_by_value.
    

    【讨论】:

    • 感谢您的帮助,对于 printFormat 函数,我是否必须将函数从 pers.firstName 更改为其他函数?我尝试了&amp;pers.firstName,但随后收到此错误request for member ‘firstName’ in something not a structure or union
    • 对不起,pers.firstName 不是函数,什么意思?
    猜你喜欢
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    相关资源
    最近更新 更多