所以我試圖將一個char數組傳遞給一個函數並打印出來然後返回數組。將字符串數組傳遞給使用指針的函數
以下是我的主要功能。我正在做二進制搜索樹但是我只是堅持傳遞一個字符數組。
int main()
{
BST tree = new_bst();
int noRecords;
int tfn;
char name[10];
char *test;
FILE *fileptr;
fileptr = fopen("welfare1.txt", "r");
fscanf(fileptr, "%d", &noRecords);
fscanf(fileptr, "%s", name);
fscanf(fileptr, "%d", &tfn);
insert_bst(&tree, tfn, &name);
}
到目前爲止,這裏是插入BST函數。我還沒有實現它,因爲我無法將名稱字段傳遞給我的函數,我使用printf作爲測試來查看它是否工作。
BSTNodePtr insert_bst_node(BSTNodePtr self, int n, char *name)
{
if (self == NULL)
{
TaxRecordPtr newRecord = new_record();
self = malloc(sizeof *self);
self->data = newRecord;
self->left = self->right = NULL;
printf("%c", name[0]);
}
....問題是....? – LPs
'&name'不是'char *'!但簡單的'名稱'衰減到一個... – aschepler
數組只通過引用傳遞,只需'insert_bst(&tree,tfn,name);'''和'BSTNodePtr insert_bst_node(BSTNodePtr self,int n,char name [])'應該爲你工作 –