So I am trying to a pass a char array into a function and print it out and then return the array back.
Below is my main function. I am doing Binary Search Trees however Im just stuck on passing a char array.
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);
}
And here is the insert BST function so far. I haven't implemented it yet as I can't pass the name field to my function, im using the printf as a test to see if it worked.
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
is not achar*
! But plainname
decays to one... – ascheplerinsert_bst(&tree, tfn, name);
andBSTNodePtr insert_bst_node(BSTNodePtr self, int n, char name[])
should work for you – Shubham Khatriinsert_bst(&tree, tfn, name); and BSTNodePtr insert_bst_node(BSTNodePtr self, int n, char name[])
It steps through but the printf function fails and I get this Exception thrown at 0x0F63125C (ucrtbased.dll) in Assign1.exe: 0xC0000005: Access violation reading location 0x0000007C. – Tarik Stonemaninsert_bst_node
and the name does not match the call toinsert_bst
– stark