Heyz all.Im working on a program to add,sort,search etc contact struct (name last name number..).So i add structs and i sort them with qsort (i print the result,they are sorted).I have problem using bsearch(),it keeps returning NULL (so i see printed "not found"). The error must be something with the pointers key1,key2 etc.Any help is welcome.

char *key1,**key2;
      char key[20];
else if (choice==3) {
               printf("\n\nEnter a search key: ");
               ///gets(key);
               ///printf("%s",key);
               scanf("%s%*c",key);
               key1=key;
               key2=&key1;
               key1= bsearch(key2,catalogue,catFreeEntry,sizeof(struct catPage), pt1);
               if (key1==NULL) { printf("\n not found");}
               else {
                    printf("%s found",key);
               }
}

The first parameter is not char** but char*. Delete key1 and key2 because neither
are needed. And the last parameter is a pointer to your comparison function, which will be something like the following.

Is variable catFreeEntry the number of elements in the catalog array of structures?

int compare(void* k1, void* k2)
{
   struct catPage* p1 = (struct catPage *)k1;
   struct catPage* p2 = (struct catPage *)k2;

   return strcmp( p1->????, p2->????); // compare the two structures
}

bsearch(key, catalogue, catFreeEntry, sizeof(struct catPage), compare);

Also note that the first argument to bsearch needs to be of the same type as the second argument, in your program the first argument needs to be struct catEntry. So you need to copy key into a struct catEntry so that you can pass it as the first argument. The reason for doing that is because bsearch() will pass it to the comparison function.
Read more details about bsearch() here

Edited 5 Years Ago by Ancient Dragon: n/a

Hey ancient dragon.I have to say your answer was the best,most helpful accurate and explaining answer i've ever had. You were right ,it was the first argument.Thanks!

Hey,im not sure if i should be posting on this thread. I got a different problem,but on the same program. Anyway. I want to write data to a file or read data from it.Writing seems to work just fine,but i wouldn't say the same for reading.Program does open the file but prints a "smiley" (%c) or 1 (%d) for every char the file has,including "\n"s.
I thought smileys were supposed to be uninitialised memory spaces?Here's the code,mr Dragon do your magic

else if (choice==4) {
           printf("\nGive file name\n");
           scanf("%s%*c",key);
           
           if ((fp=fopen(key, "r"))==NULL) { 
                                       
                                       printf("\nFile not found\n");
           }
           else
                {
                 printf("\nfile opened!\n");
                 while (c = fgetc(fp)!=EOF) {
                       printf("%c",c);
                 }  
                 fclose(fp);
                 strcpy(key,"");
           }
           }

line 12: how is variable c defined? fgetc() returns int, not char.

You need to post the code that write the file. Also attach a copy of the file or post the first few lines if the file is text.

okay.First,thanks for helping again.
Second: Um,i initialize c like this

int main(int argc, char *argv[])

      {
      int *key1,c;

the code that writes a file (which is working fine) is:

else if (choice==5) {
                printf("\nGive file name\n");
                scanf("%s%*c",key);
                fp=fopen(key, "a+");
                for (i=0;i<catFreeEntry;i++) {
                              fprintf(fp,"\n%-8s -- %8s %8d",catalogue[i].name,catalogue[i].lName,catalogue[i].pNumber);
                }
                fclose(fp);
                strcpy(key,"");
           }

The file that i would like to open for read or write, doesn't have to be is some specific form like name/lName/number .At this point i just want to open a file and print whatever it is inside until EOF. An example file i try is a1.txt
cas cas 123
sac axs 321
bad abd 212

Just to let you know how i think it: I wanted for start to try to open a file for printing whatever is inside it (but i have the smiling problem). when i make it,i will change the fprintf in my writing code,to write them in a specific format. But first i need to get file reading working! Thanks for your help again and sorry for my english

Hey again D,I found the problem although i dont know why it happens.
Below is the code that fixes it in case someone has the problem.I guess it has something to do with buffer reader??? (PS please stick around i got a liiiiittle more code to do,in case i meet difficulties :P )

while ((c=fgetc(fp))!=EOF)
                 {
                    key[i]=c;
                    i++;   
                       
                 }  
                 printf("%s",key);
                 fclose(fp);

ok buddy,last bug.Program is done and runs perfectly but the last option.

i=0;
                        printf("\nfile opened!\n");
                        int factor=1000000000;
                        while ((c=fgetc(fp))!=EOF)
                                {
                                
                                   if((c!='/')&&(c!='\n')) {
                                           if (count==0) {
                                                         cat.name[i]=c;
                                           }
                                           else if (count==1) {
                                                cat.lName[i]=c;
                                                }
                                           else {
                                                cat.pNumber+=(long int)(c) * factor;
                                                factor=factor/10;
                                                }
                                           i++;
                                   }
                                    else {
                                         count++;
                                         i=0;
                                         if (count==3) {
                                                       appendEntry(cat);
                                                       count=0;
                                                       factor=1000000000;
                                                       }
                                    }   
                                    
                                    
                     
                       
                        }

(i dont post the whole code for space)
ok catPage is {char[20] name, char[20]lName long int pNumber}.
(Note:Option 1 was adding a catPage by typing name then lName...It works fine.)
the code above is supposed to read from a file formed something like :
a1.txt:
asads/sdfs/(10-digit number1)
sdfsfg/adsa/(10-digit number2)...and so on,and create catPages
the chars before the first '/' are stored in catPage.name then lName then pNumber,then '\n'= next catPage.
but when i print it instead of getting eg
"asads-adfs-((10-digit number1), i get "asads+(freaky smiley)-sdfs-(10digit random numbers even wierd chars).I can live with the smiley next to name, but something i do wrong with setting pNumber

This question has already been answered. Start a new discussion instead.