2011-04-28 73 views
1

嗨,大家好,我在這裏遇到了一個問題,結構是,我創建了一個結構,然後創建了一個捕獲從該結構引用的員工詳細信息的函數。現在問題出現時,我嘗試調用主函數。請給我一些關於如何調用該函數的指針。代碼如下:問題結構在c

typedef struct employeeType 
{ 
    char name; 
    int employeeNumber; 
    float salary; 
    float taxPercentage; 
}EMPLOYEE; 

void enterDetails(EMPLOYEE details) 
{ 
    FILE *file; 
    file = fopen("employees.txt","w"); 
    if(file == NULL) 
    { 
     printf("File error!!!"); 
     exit(0); 
    } 
    else 
    { 
     fprintf(file,"%s",details); 
    } 
    fclose(file); 

} 

void main() 
{ 
    enterDetails(); 
} 

我不知道是什麼參數傳遞給函數在主

+1

您可能是指'int main(){}'。 – 2011-04-28 09:20:32

+0

@Cody:'int main(){}'不會做太多;-) – 2011-04-28 09:23:21

+0

Hrm,至少它編譯。我認爲這顯然不是重點。 – 2011-04-28 09:24:32

回答

0
void main() 
{ 
    EMPLOYEE details; 
    // get the value of element of struct from scanf or from other way 
    printf("Enter Name : "); 
    scanf("%s", details.name); // same for others, change the format specifier according to their data type 
    enterDetails(details); 
} 

和struct應該像

typedef struct employeeType 
{ 
    char name[]; // should be an array or pointer, to store name 
    int employeeNumber; 
    float salary; 
    float taxPercentage; 
}EMPLOYEE; 
+0

@Gaurav我想提示詳細信息並將其寫入文件 – TaIra 2011-04-28 09:33:32

+0

@Talra:現在查看我的答案。 – Gaurav 2011-04-28 09:36:39

+0

@Gaurav:你的代碼會導致崩潰。你已經將display.name聲明爲一個指針,但是在使用scanf之前並沒有將它初始化爲任何內存。可能更安全的去與陣列。 – forsvarir 2011-04-28 09:46:00

0

可以傳遞結構的指針

void main() 
{ 
    EMPLOYEE employee; 
    ..... 
    enterDetails(&employee); 
} 

void enterDetails(EMPLOYEE *details) 
{ 

} 
1

我已經註釋你的代碼與其他一些問題需要考慮

typedef struct employeeType 
{ 
    /* THIS IS ONLY ONE CHARACTER... SEEMS WRONG */ 
    /* should be 'char name[someMaxSize]', or 'char *name' */ 
    char name; 
    int employeeNumber; 
    float salary; 
    float taxPercentage; 
}EMPLOYEE; 

/* As pointed out by 'Cody Gray', this function is called 'enterDetails' 
* does it really need to have a parameter at all, or should it be responsible 
* for taking the details from the user? Is it an appropriately 
* named method for the task it's actually performing 
* (would saveDetails be better for example)? 
*/ 
void enterDetails(EMPLOYEE details) 
{ 
    FILE *file; 
    file = fopen("employees.txt","w"); 
    if(file == NULL) 
    { 
     printf("File error!!!"); 
     exit(0); 
    } 
    else 
    { 
     /* THIS IS PASSING A STRUCTURE AS A STRING */ 
       /* You probably want to write out the individual fields instead */ 
       /* fprintf(file, "%s,%d", details.name, details.employeeNumber); etc */ 
     fprintf(file,"%s",details); 
    } 
    fclose(file); 

} 

void main() 
{ 
    EMPLOYEE details; 
    /* populate details somehow then pass it in to the function*/ 
    enterDetails(details); 
} 

您可能還需要考慮細節傳遞到函數的指針,儘管這會改變你的函數簽名,那豈不是你不會將太多信息推入堆棧。

如果用指針版本,那麼去:

void enterDetails(EMPLOYEE details) 

將成爲

void enterDetails(EMPLOYEE *details) 

和主會變成:

void main() 
{ 
    EMPLOYEE details; 
    /* populate details somehow then pass it in to the function as pointer */ 
    enterDetails(&details); 
} 

您還需要改變方式你在你的函數中使用細節,但正如我已經說過的,我相信你的fprintf調用已經被破壞了。

0

你需要傳遞一個參考,而不是一個值。如果你通過員工價值作爲在過去後,它會被複制,拷貝將被修改,而不是原來

void enterDetails(EMPLOYEE* emp) { 
    // do stuffs 
} 

void main() { 
    EMPLOYEE emp; 
    enterDetails(&emp); 
} 
+0

他們寫出一個文件,而不是填充結構,所以內容不會被函數修改。 – forsvarir 2011-04-28 09:27:11

+0

@forsvarir:所以如果寫出一個文件,爲什麼'enterDetails'函數完全接受一個參數呢?它似乎應該創建一個結構的實例,填充它,保存到一個文件,然後返回。 – 2011-04-28 09:33:10

+0

@Cody Gray:一個合理的問題。它可能名字很差,意思是將詳細信息輸入到文件中,或者可能是你正確的,並且enterDetails函數應該詢問信息以便能夠將它寫入文件中。認爲OPs能夠更好地回答他們的想法...... – forsvarir 2011-04-28 09:38:27

0

的第一個問題是你的結構不正確。由於只有一個字節,因此您不能在名稱字段中存儲員工的姓名。你必須使它成爲一個數組(在這種情況下更簡單)或指向已分配內存的指針。

如果你想使它成爲一個數組,那麼你應該定義數組的最大尺寸。在我們的例子中,我們只是將它設爲100個字節,這將足以存儲任何名稱。

#define MAX_NAME 100 

typedef struct employeeType 
{ 
    char name[MAX_NAME]; 
    int employeeNumber; 
    float salary; 
    float taxPercentage; 
}EMPLOYEE; 

其次,你的功能命名很混亂。 enterDetails應該只填充你傳遞的結構。第三,你的輸入細節應該接受一個指向EMPLOYEE結構的指針。如果你想將任何值傳遞給一個將會改變它的內容的函數,那麼你只能使用指針(或者如果你使用C++,但基本上是一個指針)。所以enterDetails應該是,

void enterDetails(EMPLOYEE *details) 
{ 
    printf("\nEnter the employee's name "); 
    scanf("%s", details->name); // this isn't secure since it doesn't perform bound checking. 

    printf("\nEnter employee number "); 
    scanf("%d", &details->employeeNumber); 

    printf("\nEnter employee salary "); 
    scanf("%f", &details->salary); 

    printf("\nEnter tax percentage "); 
    scanf("%f", &details->taxPercentage); 

} 

最後,如果你想結構的內容存儲到要給人閱讀的,那麼你應該格式化結構的內容和轉儲到一個文件的文件。

int writeToFile(EMPLOYEE *details) /* accepting the structure will work as well but it's faster and efficient to pass the structure's pointer */  
{ 
    FILE *file; 

    file = fopen("employees.txt","w"); 
    if(file == NULL) { 
     printf("File error!!!"); 
     return 0; 
    } 

    fprintf(file, "\nEmployee Name: %s", details->name); 
    fprintf(file, "\nEmployee Number: %d", details->employeeNumber); 
    fprintf(file, "\nSalary: %f", details->salary); 
    fprintf(file, "\nTax Percentage: %f", details->taxPercentage); 

    fclose(file) 
    return 1; 
} 

及主要

int main(void) 
{ 
    EMPLOYEE details; 

    enterDetails(&details); // passing the pointer here is a must 
    if (!writeToFile(&details)) { // passing the pointer since it's faster 
     printf("\nError writing to file"); 
     return 1; 
    } else { 
     printf("\nSuccess!"); 
     return 0; 
    } 
} 

而在你的情況,你不需要任何參數傳遞給主。但是如果你想知道如何傳遞參數,那麼這裏就是一個簡單的例子。

int main(int argc, char **argv) 
{ 
    int i; 

    for (i = 0; i < argc; i++) 
     printf("\n%s", argv[i]); 

    return 0; 
} 
+0

您對scanf的電話號碼呼叫不正確。這:scanf(「%d」,details-> employeeNumber);應該是傳遞數字的地址,而不是實際的數字,否則scanf不知道該把值放在哪裏。 – forsvarir 2011-04-28 13:00:04

+0

@shebaw感謝它似乎工作,但是當我運行該程序時,它只提示輸入名稱和員工編號,當我按Enter鍵繼續它暫停和終止。因此,不向文件寫任何東西,問題是什麼? – TaIra 2011-04-28 16:41:51

+0

@forsvarir你能給我舉一個例子嗎?請告訴我 – TaIra 2011-04-28 16:45:33