> char d;
You're on the right track here
Look at previous posts, and what a single phone number was stored in.
Then make your element store one of those.
> char d;
You're on the right track here
Look at previous posts, and what a single phone number was stored in.
Then make your element store one of those.
so i should go back to storing my telephone numbers as arrays in a list structure? would that not defy the point of a list?
No - like this
Code:typedef struct linked_list { char phone[20]; struct linked_list *next; } Element;
ok ive done that , where else am i going wrong?
here is my code: (i have included the errors my compiler is giving me by commenting them into the relevent places) :
thank youCode:#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct tree { char name[20]; char *Element; struct tree *left; struct tree *right; }Tree; typedef struct linked_list { char d[20]; struct linked_list *next; } Element; Element* insert( char h, Element *t ) { Element* y = calloc( 1, sizeof( Element ) ); y->d = h; //ERROR: INCOPATIBLE TYPES IN ASSIGNMENT AT TOP LEVEL if( t ) t->next = y; return y; } Tree *makenode( char *in, char *in2, Tree *l, Tree *r) { Tree *t=malloc(sizeof(Tree)); t->left =l; t->right =r; strncpy(t->name,in,19); /*Statement to copy pointer to linked list of phone numbers into tree (someone help)*/ return t; } Tree *insert( Tree *root, char *what, char *what2) //ERROR:CONFLICTING TYPES FOR INSERT { if(root==NULL) {root=makenode(what,Element,NULL,NULL);} //PARSE ERROR BEFORE ELEMENT else if(strcmp(what, root->name)<0) {root->left =insert(root->left,what,Element);} //PARSE ERROR BEFORE ELEMENT else{root->right =insert(root->right,what,Element); //PARSE ERROR BEFORE ELEMENT } return root; } char *search(Tree *root, char *what) { if(root==NULL){return "NOT FOUND";} else if(strcmp(what,root->name)==0) {return Element;} //PARSE ERROR BEFORE ELEMENT else if(strcmp(what,root->name)<0) {return search(root->left,what);} else{return search(root->right,what);} } int main(void) { int i; char s[20]; char d; Tree *tree =NULL; Element* head = NULL; Element* tail = NULL; Element* next; do{ scanf("%s",s); if(strcmp(s,".")==0) {break;} scanf("%s",&d); for(i=0;i<20;i++) {s[i]=toupper(s[i]);} if(strcmp(s,".")!=0); { tree=insert(tree, s,*Element); //PARSE ERROR BEFORE ELEMENT tail = insert( d, tail ); //WARNING: PASSING ARG1 of 'insert' makes pointer without if( !head ) //cast {head = tail; } } }while(strcmp(s,".")!=0); do{ printf("Type a name name please "); scanf("%s",s); for(i=0;i<20;i++) {s[i]=toupper(s[i]);} if(strcmp(s,".")!=0) { for( ; head ; head = next ) { printf( "%c", head->d ); next = head->next; free( head );} } }while(strcmp(s,".")!=0); return 0; }
Well, if you read your code and then read the error, you should be able to figure these out for yourself:
See? The first one is an array (d), and the second one is a single character (h). You need to assign that character to a specific spot in the array. Not just to the array name (which is treated as a pointer to the array in this case).Code:typedef struct linked_list { char d[20]; struct linked_list *next; } Element; Element* insert( char h, Element *t ) { Element* y = calloc( 1, sizeof( Element ) ); y->d = h; //ERROR: INCOPATIBLE TYPES IN ASSIGNMENT AT TOP LEVEL
You are trying to make two functions called the same thing. You can't do that in C. Fix those, then try your code again.Code:Element* insert( char h, Element *t ) { ...stuff... } ... Tree *insert( Tree *root, char *what, char *what2) //ERROR:CONFLICTING TYPES FOR INSERT { ...stuff... }
Quzah.
Hope is the first step on the road to disappointment.
i'm still not sure how i should change my insertlist function , this is what ive done, but its wrong, "subscripted value is neither array nor pointer"
any help is appreciated. thanksCode:Element* insertlist( char h, Element *t ) { int i; Element* y = calloc( 1, sizeof( Element ) ); for(i=0;i<20;i++) {int b; b=0; y->d = h[b]; b++; if( t ) t->next = y;} return y; }
You don't understand:
h is one single character.
That means it is a single letter or number, etc. You don't subscript it. You simply assign that one single character to one single spot in the array.
If it is supposed to be more than one single character, then you'll need to pass an array of characters to the function instead:
Quick example of what I'm talking about. What you are doing is passing one character, not an array.Code:void foo( char bar[] ) { char baz[10]; ...copy the array bar into baz... } int main( void ) { char array[] = "Hello!"; foo( array ); return 0; }
Quzah.
Hope is the first step on the road to disappointment.