【发布时间】:2017-04-27 12:46:44
【问题描述】:
所以我试图将一个 char 数组传递给一个函数并将其打印出来,然后返回该数组。
以下是我的主要功能。我正在做二叉搜索树,但我只是坚持传递一个 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]);
}
【问题讨论】:
-
....问题是....?
-
&name不是char*!但是普通的name衰减到一个... -
数组只通过引用传递,所以
insert_bst(&tree, tfn, name);和BSTNodePtr insert_bst_node(BSTNodePtr self, int n, char name[])应该适合你 -
使用
insert_bst(&tree, tfn, name); and BSTNodePtr insert_bst_node(BSTNodePtr self, int n, char name[])它逐步执行,但 printf 函数失败,我得到了这个 在 Assign1.exe 中的 0x0F63125C (ucrtbased.dll) 处引发的异常:0xC0000005:访问冲突读取位置 0x0000007C。 -
您在
insert_bst_node中的括号不匹配,并且名称与对insert_bst的调用不匹配
标签: c arrays function pointers