2015-10-13 240 views
-4

所以,我有这样的:参数传递给函数

struct Entry { 
    int Key; 
    char * Info;  
}; 

const int SIZE=100; 
typedef Entry T; 
typedef T MyList[SIZE]; 

void read_MyList(MyList & L, int & n) 

MYLIST应该是一个指向结构的载体,对不对?所以我应该只传递指针的名字,对吧?但是'&'是什么意思?我是否传递变量的引用?我传递了指针的引用吗?

+6

也许有好的想法是读一本书,关于位引用和指针读两次 –

回答

3
struct Entry { 
    int Key; 
    char * Info;  
}; 

意味着你已经结构称为入口

const int SIZE=100; 

意味着你声明一个常量

typedef Entry T; 

意味着你给另一名入境,从现在开始T是另一名struct Entry

typedef T MyList[SIZE]; 

意味着你给另一名至100 T■数组,并将其命名为:MyList

void read_MyList(MyList & L, int & n) 

表示您声明一个函数,并给它一个参考MyList和一个参考int。引用意味着没有副本,您使用发送给该函数的相同原始对象或变量。

因此: 要叫它,你必须提供一个MyList(不是一个指针,一个真实的物体)和一个int。参数是输入/输出参数。

喜欢:

int main(){ 
    MyList l1; 
    int n=0; 
    read_MyList(l1,n); 
    return 0; 
} 
+0

谢谢你的帮助!最好的!这就说得通了。 –

0

必须

struct Entry { 
    int Key; 
    char * Info;  
}; 

const int SIZE=100; 
typedef Entry T; 
T MyList[SIZE]; 

void read_MyList(T* L, int & n) 

和函数调用

int iEntries; 
read_MyList(MyList, iEntries); 
+0

谢谢您的帮助!这就说得通了。 –