Warning: Assignment from incompatible pointer type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • plazzasele
    New Member
    • Apr 2010
    • 4

    Warning: Assignment from incompatible pointer type

    I want to register a new Person by using the set and get method.
    I am running my code on a program ( DEVc++), and when i compile the code i get this error message:

    [Warning] assignment from incompatible pointer type.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    struct Person {
           char *name;
        
           void (*setName) (struct Person *this, char *name); 
           char (*getName) (struct Person *this);
           };
    
    void Person_setName (struct Person *this, char* name) {
        this->name = name;
    }
    
    char *Person_getNavn (struct Person *this) {
        return this->name;
    }
    
    void menu();
    void regPerson();
    
    
    int main(char *argv[], int argc) {
        menu();
        return 0;
    }
    
    void menu() {
        int c = -1;
        while (c != 0) {
            printf("\n==========\n");
            printf("Main Menu:\n");
            printf("==========\n");
            printf("0 - Exit\n");
            printf("1 - Register new person\n");
            
            printf("Choice: ");
            scanf("%i", &c);
            printf("\n");
            
    switch (c) {
                case 0:
                    printf("\nGoodbye\n");
                    return;
                case 1:
                    regPerson();
                    break;
                            default:
                    printf("Unrecognized choice, please try again.\n");
            }
        }
    }
    
    void regPerson() {
        struct Person *p=(struct Person *)malloc(sizeof(struct Person *));
        
        char *name = (char *)malloc(sizeof(char) * 100);
        char *id = (char *)malloc(sizeof(char) * 100);
      
        p->setName=Person_setName;
        p->getName=Person_getName;  // it is here i get an error.
        
    
        printf("===============\n");
        printf("Register person\n");
        printf("===============\n");
        printf("Name: ");
        scanf("%s", name);
        printf("ID: ");
        scanf("%s", id);
        
        p->navn=(name);
        p->id=(id);
        printf("\n Navn: %s\n Id: %s ", p->navn,p->id);
      
    }
    Last edited by Banfa; Apr 16 '10, 04:11 PM. Reason: Added [code]...[/code] tags
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    You have a typo(?) in Person_getNavn( ) name, it may be the reason. If not, they also differ by return type - char and char*.

    Comment

    • plazzasele
      New Member
      • Apr 2010
      • 4

      #3
      hi..

      sorry that is a mistake.. in line 72 there should be p->name(name); not p->navn(name);

      and what do you mean by typo(?) ..??
      and how to return a char* .??

      Comment

      • plazzasele
        New Member
        • Apr 2010
        • 4

        #4
        Originally posted by newb16
        You have a typo(?) in Person_getNavn( ) name, it may be the reason. If not, they also differ by return type - char and char*.
        i was working on this code, this what i come to. now i dont get anymore error while compiling, but the program stopes when it comes to this line:
        printf("navn: %s\n",p->getNavn(p));
        have i wright the rest wright..??


        Code:
        #include <stdlib.h>
        #include <stdio.h>
        
        struct Person{
               char *name;
               char *id;
               void (*setName)(struct Person *this, char *name);
               char (*getName)(struct Person *this);
               void (*setId)(struct Person *this, char *id);
               char (*getId)(struct Person *this);
               };
        void Person_setName(struct Person *this, char *name) {
            this->name = name;
        }
        
        char Person_getName(struct Person *this) {
            return *this->name;
        }
        void Person_setId(struct Person *this, char *id) {
            this->id = id;
        }
        
        char Person_getId(struct Person *this) {
            return *this->id;
        }
        void menu();
        void regPerson();
        
        int main(char *argv[], int argc) {
            menu();
            return 0;
        }
        
        void menu() {
            int c = -1;
            while (c != 0) {
                printf("\n==========\n");
                printf("Main Menu:\n");
                printf("==========\n");
                printf("0 - Exit\n");
                printf("1 - Register new person\n");
                printf("Choice: ");
                scanf("%i", &c);
                
                switch (c) {
                    case 0:
                        printf("\nGoodbye\n");
                        return;
                    case 1:
                        regPerson();
                        break;
        default:
                        printf("Unrecognized choice, please try again.\n");
                }
            }
        }
        
        void regPerson() {
            struct Person *p=(struct Person *)malloc(sizeof(struct Person *));
            
            char *name = (char *)malloc(sizeof(char) * 100);
            char *id = (char *)malloc(sizeof(char) * 100);
            
            p->name=(char *)malloc(sizeof(char) * 100);
            p->id=(char *)malloc(sizeof(char) * 100);
            
            p->setName=(*Person_setName);
            p->getName=*Person_getName;
            p->setId=*Person_setId;
            p->getId=*Person_getId;
            printf("===============\n");
            printf("Register person\n");
            printf("===============\n");
            printf("Name: ");
            scanf("%s", name);
            printf("ID: ");
            scanf("%s", id);
            printf("\nName: %s\n Id: %s\n",name,id);
            
            p->setName(p,&*name);
          
            p->setId(p,&*id);
            printf("test\n");
            
            printf("name: %s\n",p->getNavn(p));
            
            printf("id: %s\n",p->getId);
           
            
        }
        Last edited by Banfa; Apr 16 '10, 11:25 PM. Reason: Added [code] ... [/code] tags

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          I don't know how you managed to compile it, because you still declare it as getName in the structure declaration and call as getNavn.
          Then, char (*getName)(stru ct Person *this);
          returns char , not char*. And printf %s format specifier expects char*, but your function returns char. It can't check the type mismatch, but crashes instead.
          change it to char* (*getName)(stru ct Person *this); and change Person_getName accordingly.
          You getId() is also wrong.

          Comment

          • plazzasele
            New Member
            • Apr 2010
            • 4

            #6
            Originally posted by newb16
            I don't know how you managed to compile it, because you still declare it as getName in the structure declaration and call as getNavn.
            Then, char (*getName)(stru ct Person *this);
            returns char , not char*. And printf %s format specifier expects char*, but your function returns char. It can't check the type mismatch, but crashes instead.
            change it to char* (*getName)(stru ct Person *this); and change Person_getName accordingly.
            You getId() is also wrong.
            thank you very much newb16..

            now it is working.. and i am sorry again for the mistakes.. Navn suppose to be name..

            Comment

            Working...