Assignment from incompatible pointer type

This is a discussion on Assignment from incompatible pointer type within the C Programming forums, part of the General Programming Boards category; The problem has something to do with my temp in the sort function. It says assignment from incompatible pointer type ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Assignment from incompatible pointer type

    The problem has something to do with my temp in the sort function. It says assignment from incompatible pointer type when i set "temp" = to something. It's a bubble sort. suggestions?

    thank you,

    brehon1104

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    
    //function protos
    void addentry();
    void delentry ();	
    void printentry ();
    void sort ();
    void random ();
    void clear ();
    void search ();
    
    // struct for an entry
    typedef struct
    {
           char *fname;
           char *lname;
           char *num;
    } acontact;
    
    acontact *ac; //pointer toward struct named "ac"
    char first[100];
    char last[100];
    int counter = 0; // keeps tract of contacts
    int i = 0; //printing index
    
    // set eeverything to 0
    
    
    main() 
    {
    
    
    int choice = 0;
    
          while (choice !=8)
    {
          printf("\n\nYellow Pages Editor v 1.06");
          printf("\n1) Add entry");
          printf("\n2) Delete entry");
          printf("\n3) Display Yellow Pages");
          printf("\n4) Sort entries");
          printf("\n5) Find a number");
          printf("\n6) Randomly select a friend");
          printf("\n7) Erase phonebook");
          printf("\n8) Exit");
          printf("\n\nWhat do you want to do? ");
          scanf("%d", &choice);
          
          
          
          switch (choice) {
          //add entry
                 case 1:
                      addentry();
                      system("\npause\n\n");
                      break;
                       
         //delete entry
                 case 2:
                      delentry();
                      system("\npause\n\n");
                      break;
          //display
                 case 3:
                      printentry();
                      system("\npause\n\n");
                      break;
          
          //sort
                case 4:
                     sort();
                     system("\npause\n\n");
                     break;
                     
          //find a name given a number
                 case 5:
                      search();
                      system("\npause\n\n");
                      break;
                      
          //randomly select a friend
                 case 6:
                      random ();
                      system("\npause\n\n");
                      break;
                      
          // erase phonebook entirely
                 case 7:
                      clear ();
                      system("\npause\n\n");
                      break;
                     
                      default:
                             printf("Try Again.");
                             break;
                 
                 
    
    } //end switch
    } // end while
    } // end main
    
    
    void addentry ()
    {
    	if(counter == 0)
    	{
            ac = (acontact *) malloc (sizeof(acontact) + (counter*20));
        }
        else
        {
            ac = (acontact *) realloc (ac, sizeof(acontact) + (counter*20) + 20);
        }
        if(ac == NULL)
        {
            printf("out of memory\n");
        }
        else
        {
        ac[counter].fname = (char *) malloc (sizeof(char)*15);
        ac[counter].lname = (char *) malloc (sizeof(char)*15);
        ac[counter].num = (char *) malloc (sizeof(char)*20);
        printf("\nFirst name: ");
        scanf("%s", ac[counter].fname);
        printf("Last name: ");
        scanf("%s", ac[counter].lname);
        printf("Phone number (no dash): ");
        scanf("%s", ac[counter].num);
        
        counter++;
        printf("Entry added to Yellow Pages!\n\n");
        }
    }	//end add
    
    void delentry ()
    {
        int i;
        int removed = 0;
        
        printf("\nFirst name: ");
    	scanf("%s", first);
        printf("Last name: ");
    	scanf("%s", last);
    	
    	// string comparison
    	for (i = 0; i<counter; i++)
    	{
            if ((ac[i].fname == NULL) && (ac[i].lname == NULL)) continue;
    		if ((stricmp(ac[i].fname, first) == 0) && (stricmp(ac[i].lname, last) == 0))
    		{
    			ac[i].fname = NULL;
    			ac[i].lname = NULL;
    			ac[i].num = NULL;
    			removed = 1;	
    			break;
    		}
    		if(removed == 1)
    		{
                       printf("\nUser Deleted!\n\n");
            }
            else
            {
                       printf("\nUser not found!\n\n");
            }
        }
    }// end del funct
    
    void printentry ()
    {
         if (counter==0)
         printf("Nothing to display.\n\n");
         else
         {
    	int i;
    		printf("\n\nYellow Page Entries:\n\n");
    		for(i=0; i<counter; i++)
    		{
    			if (ac[i].lname != NULL)
    			{
    				printf("%s %s %s\n", ac[i].fname, ac[i].lname, ac[i].num);
    			}// end if
    		} //end for
    }// end else
    } //end print
    
    
    void sort ()
    {
         int i;
         int j;
         acontact *temp; // the problem is here
         temp = (char *) malloc (sizeof(char)*15); // and here
         
    
         int choice;
         printf("Sort by:\n");
         printf("1) First name\n");
         printf("2) Last name\n");
         printf("Which? ");
         scanf("%d", choice);
         
         switch (choice) {
                case 1:
                     
            if (counter == 0)
            printf("No entries to sort.");
            
            else {
            for (i = 0; i<counter; i++)
    {
            for (j = 0; j<counter; j++)
            {
                if ((stricmp(ac[j].fname, ac[i].fname)) > 0)
                {
                    temp = ac[j].fname;
                    ac[j].fname = ac[i].fname;
                    ac[i].fname = temp;
                    
                }// end if
            }// end inner for
    }// end outer for
    printf("Sorted");
    }// else else
    
             system("\npause\n\n");
             break;
    
    // if this doesnt work instead of j+1 make it i 
    case 2:
                     if (counter == 0)
            printf("No entries to sort");
            
            else {
            for (i = 0; i< counter-1; i++)
    {
            for (j = 0; j < counter-1; j++)
            {
                if ((stricmp(ac[i].lname, ac[j].lname)) > 0)
                {                     
                    temp = ac[j].lname;
                    ac[j].lname = ac[i].lname;
                    ac[i].lname = temp;
                    
                }// end if
            }// end inner for
    }// end outer for
    }// else else
    
                system("\npause\n\n");
                break;
               
    }// end switch
    }// end sort funct
    
    
    void random ()
    {
         int y;
         if (counter == 0)
         printf("No entries!");
         
         else
         {
         y = rand()%counter;
         printf("%s %s %s", ac[y].fname, ac[y].lname, ac[y].num);
         }
    }
         
    
    void search ()
    {
         if (counter == 0)
         printf("\nNo entries.\n\n");
         
         else {
         int g = 0;
         char ifname[100];
         char ilname[100];
         printf("Enter the first name: ");
         scanf("%s", ifname);
         printf("Enter the last name: ");
         scanf("%s", ilname);
         
         for(g=0;g<counter;g++)
         {
              if ((stricmp(ac[i].fname, ifname) == 0 && (stricmp(ac[i].lname, ilname)) == 0))
              printf("\n%s\n\n", ac[i].num);
         }// end for
    } // end else
    } // end search func
         
     
    void clear ()
    {
         if (counter ==0) 
         printf("\nNothing to erase\n");
         
         else{
              
              counter == 0;
              free(ac);
    
    
    }// end else
    }// end clear

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    temp and fname are char arrays of 15 characters. So to move such data you will need:
    Code:
    memcpy(temp, ac[j].lname, 15);
    memcpy(ac[j].lname, ac[i].lname, 15);
    memcpy(ac[i].lname, temp, 15);
    Also, your delentry() function has poroblems. You shouldn't just assign NULL to the pointers. You should use free() to deallocate the memory that you allocated earlier. Then you can assign NULL if you wish.
    Last edited by nonoob; 03-03-2011 at 06:31 AM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    i posted your solution in the sort function. It still says the same thing, and still crashes when you enter the switch statement within the sort function.

  4. #4
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by brehon1104 View Post
    i posted your solution in the sort function. It still says the same thing, and still crashes when you enter the switch statement within the sort function.
    In addition to what others have pointed out, you have other problems in your code.
    Line 204
    Code:
    scanf("%d", choice);
    Line 304
    Code:
    counter == 0;
    Seems like you forgot to free temp in your sort function.
    Also, you should make your main function return an integer is that is standard behavior these days.
    Maybe I'm missing something, but on Line 197 why are you casting your malloc to (char *)? I don't see the point, and it throws a warning on my compiler.
    Last edited by Phenax; 03-03-2011 at 09:04 AM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Code:
    ac[counter].fname = (char *) malloc (sizeof(char)*15);
    ac[counter].lname = (char *) malloc (sizeof(char)*15);
    ac[counter].num = (char *) malloc (sizeof(char)*20);
    1. Avoid casting the return value from the malloc call, the returned void* can be safely assigned to other pointer types (in your case a char*) without the need of a cast.
    2. sizeof(char) is guaranteed to be 1 and since anything multiplied by 1 is just simply that original value it is unneeded here.
    Code:
    ac[counter].fname = malloc (15);
    ac[counter].lname = malloc (15);
    ac[counter].num = malloc (20);

    It says assignment from incompatible pointer type when i set "temp" = to something.
    Code:
    acontact *temp; // the problem is here
    temp = (char *) malloc (sizeof(char)*15); // and here
    3. Your cast (which I already mentioned you should not be using) is converting the malloc'd memory into a char* which does not match the acontact* you are attempting to assign it to.
    4. You are mallocing an incorrect size, you are asking for 15 bytes when you should be asking for whatever sizeof(acontact) is.
    5. Why even use dynamic memory allocation here at all? Just have a temp acontact object instead of a pointer to one.

    In general:
    6. You have no error checking code for your dynamic memory allocations. You simply assume it all works.
    7. Your data entry (the scanf calls do not limit how many characters they accept into your buffers. A user can type in 50 characters for a first name for example and the scanf will happily try to store it into a buffer that is only 15 characters in size.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... look at this rationally... you have an array of structs but in your sort you are moving only partial contents of the struct... This is going to cause data corrpution...

    John Smith ... Fred Doe ... Cathy Martin ...
    would become
    Cathy Smith ... Fred Doe ... John Martin...

    You have to move the entire struct, not just the part you are sorting on.

    Code:
    // struct for an entry
    typedef struct tContact
    {
           char fname[20];
           char lname[20];
           char num;
    } Contact, *pContact;
    
    // array of pointers to contacts
    pContact Contacts = Null;
    
    // how many contacts.
    int LastContact = 0;  
    int MaxContacts = 0;
    
    // add new contacts to array
    int AddNewContact( char* nfirst, char *nlast)
      { ++LastContact;
         // expand array by 10s 
         if (LastContact > MaxContacts)
           { MaxContacts += 10;
             Contacts = realloc(Contacts, sizeof(tContact) * MaxContacts); }
        // add new contact
        strncpy( Contacts[LastContact]->fname,  nfirst, 20 );  
        strncpy(  Contacts[Lastcontact]->lname, nlast, 20); 
        return LastContact; } 
    
    // delete, edit, etc goes here
    
    
    
    // now when you come to your sort
    // all you have to do is swap pointers.
    void sort ()
    {
         int i;
         int j;
         pcontact temp;    
         
          for (i = 0; i < LastContact; i++)
            for (j = 0; j < LastContact; j++)
               if ((stricmp(Contacts[j]->fname, Contacts[i]->fname)) > 0)
                {   temp = Contacts[j];
                    Contacts[j] = Contacts[i];
                    Contacts[i] = temp; }
           printf("Sorted"); }
    I'm sure there's a couple of minor errors in there somplace --this is untested code -- but I trust you get the concepts involved....
    Last edited by CommonTater; 03-03-2011 at 09:43 AM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Thank you everyone, I got it figured out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing argument from incompatible pointer type
    By bhdavis1978 in forum C Programming
    Replies: 5
    Last Post: 03-17-2010, 12:42 PM
  2. incompatible pointer type?
    By kezman in forum C Programming
    Replies: 3
    Last Post: 04-22-2009, 04:42 PM
  3. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21