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.
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); }
Comment