所以我试图将一个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 [])'应该为你工作 –