2012-04-29 180 views
36

我是一個新的C程序員,我想知道如何將struct傳遞給函數。我收到一個錯誤,無法找出正確的語法。下面是它的代碼....將struct傳遞給函數

struct

struct student{ 
     char firstname[30]; 
     char surname[30]; 
    }; 

    struct student person; 

電話:

addStudent(person); 

原型:

void addStudent(struct student); 

和實際的功能:

void addStudent(person) 
{ 
    return; 
} 

編譯器錯誤:

line 21: warning: dubious tag declaration: struct student 
line 223: argument #1 is incompatible with prototype: 
+1

你在哪裏聲明你的結構?在你的實際實現文件中,你的'struct student {/ * ... * /};'代碼在哪裏?它看起來像它在錯誤的範圍(如在'main'功能或任何函數中聲明你想從調用'addStudent' ... – 2012-04-29 06:04:28

+0

是的它在我的功能範圍 – 2012-04-29 06:06:50

回答

34

線功能的實現應該是:

void addStudent(struct student person) { 

} 

person不是一個類型,但一個變量,你不能把它作爲一個函數的類型參數。

另外,請確保您的結構在原型使用它之前定義在函數addStudent的原型之前。

+5

一個好主意是爲「名」的結構類型,以避免此問題,使用typedef。請參閱http://en.wikipedia.org/wiki/Struct_(C_programming_language) – 2012-04-29 05:58:43

+0

@Binyamin股份,即時通訊仍然收到相同的錯誤 – 2012-04-29 05:59:27

+0

@DanielDC - 請參閱編輯 – MByD 2012-04-29 06:00:56

3

你需要指定人類型:

void addStudent(struct student person) { 
... 
} 

此外,您還可以的typedef你的結構,以避免在每次使用它的時間結構類型:

typedef struct student{ 
... 
} student_t; 

void addStudent(student_t person) { 
... 
} 
75

這是怎麼通過參考struct。這意味着你的函數可以訪問函數外的struct並修改它的值。您可以通過將指針傳遞給該函數來完成此操作。

#include <stdio.h> 
/* card structure definition */ 
struct card 
{ 
    int face; // define pointer face 
}; // end structure card 

typedef struct card Card ; 

/* prototype */ 
void passByReference(Card *c) ; 

int main(void) 
{ 
    Card c ; 
    c.face = 1 ; 
    Card *cptr = &c ; // pointer to Card c 

    printf("The value of c before function passing = %d\n", c.face); 
    printf("The value of cptr before function = %d\n",cptr->face); 

    passByReference(cptr); 

    printf("The value of c after function passing = %d\n", c.face); 

    return 0 ; // successfully ran program 
} 

void passByReference(Card *c) 
{ 
    c->face = 4; 
} 

這是你如何通過值傳遞struct讓你的函數接收struct的副本,不能訪問外部結構進行修改。外部我的意思是在功能之外。

#include <stdio.h> 


/* global card structure definition */ 
struct card 
{ 
    int face ; // define pointer face 
};// end structure card 

typedef struct card Card ; 

/* function prototypes */ 
void passByValue(Card c); 

int main(void) 
{ 
    Card c ; 
    c.face = 1; 

    printf("c.face before passByValue() = %d\n", c.face); 

    passByValue(c); 

    printf("c.face after passByValue() = %d\n",c.face); 
    printf("As you can see the value of c did not change\n"); 
    printf("\nand the Card c inside the function has been destroyed" 
     "\n(no longer in memory)"); 
} 


void passByValue(Card c) 
{ 
    c.face = 5; 
} 
+0

簡潔整齊漂亮 – 2015-12-07 05:43:18

+0

絕對完美的 – StrawHara 2016-03-24 19:27:26

6

當將一個結構體傳遞給另一個函數時,通常會更好地按照上面的Donnell建議並通過引用來傳遞它。

這樣做的一個很好的理由是,如果您想要進行更改,那麼當您返回到創建該實例的函數時將會反映出來。

這裏是要做到這一點最簡單的方法的例子:

#include <stdio.h> 

typedef struct student { 
    int age; 
} student; 

void addStudent(student *s) { 
    /* Here we can use the arrow operator (->) to dereference 
     the pointer and access any of it's members: */ 
    s->age = 10; 
} 

int main(void) { 

    student aStudent = {0};  /* create an instance of the student struct */ 
    addStudent(&aStudent);  /* pass a pointer to the instance */ 

    printf("%d", aStudent.age); 

    return 0; 
} 

在這個例子中,對於addStudent()函數的參數是指向一個student結構的實例 - student *s。在main(),我們創建student結構的實例,然後使用引用操作符(&)傳遞給它的引用,我們addStudent()功能。

addStudent()函數中,我們可以使用箭頭運算符(->)來取消引用指針,並訪問它的任何成員(功能上相當於:(*s).age)。

當我們返回main()時,我們在addStudent()函數中所做的任何更改都會反映出來,因爲指針爲我們提供了內存中student結構實例存儲位置的引用。這由printf()來說明,在這個例子中它將輸出「10」。

如果你沒有通過引用,你實際上會使用你傳遞給函數的結構體的副本,這意味着當你返回到main時,任何改變都不會被反映 - 除非你實現了一種傳遞方式結構的新版本返回到主或沿着這些行的東西!

雖然指針看起來倒胃口首先,一旦你得到你的頭圍繞它們是如何工作以及爲什麼它們是那麼得心應手,他們成爲第二天性,你想知道你曾經沒有他們應對!

0

相反的:

void addStudent(person) 
{ 
    return; 
} 

試試這個:

void addStudent(student person) 
{ 
    return; 
} 

既然你已經宣佈了一個名爲「學生」的結構,你不必在功能實現在指定如此:

void addStudent(struct student person) 
{ 
    return; 
} 
+0

我不這麼認爲沒有類型定義,它會出錯?。!。 – Abdillah 2014-07-21 00:47:38