473,473 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Warning: Assignment from incompatible pointer type

4 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct Person {
  5.        char *name;
  6.  
  7.        void (*setName) (struct Person *this, char *name); 
  8.        char (*getName) (struct Person *this);
  9.        };
  10.  
  11. void Person_setName (struct Person *this, char* name) {
  12.     this->name = name;
  13. }
  14.  
  15. char *Person_getNavn (struct Person *this) {
  16.     return this->name;
  17. }
  18.  
  19. void menu();
  20. void regPerson();
  21.  
  22.  
  23. int main(char *argv[], int argc) {
  24.     menu();
  25.     return 0;
  26. }
  27.  
  28. void menu() {
  29.     int c = -1;
  30.     while (c != 0) {
  31.         printf("\n==========\n");
  32.         printf("Main Menu:\n");
  33.         printf("==========\n");
  34.         printf("0 - Exit\n");
  35.         printf("1 - Register new person\n");
  36.  
  37.         printf("Choice: ");
  38.         scanf("%i", &c);
  39.         printf("\n");
  40.  
  41. switch (c) {
  42.             case 0:
  43.                 printf("\nGoodbye\n");
  44.                 return;
  45.             case 1:
  46.                 regPerson();
  47.                 break;
  48.                         default:
  49.                 printf("Unrecognized choice, please try again.\n");
  50.         }
  51.     }
  52. }
  53.  
  54. void regPerson() {
  55.     struct Person *p=(struct Person *)malloc(sizeof(struct Person *));
  56.  
  57.     char *name = (char *)malloc(sizeof(char) * 100);
  58.     char *id = (char *)malloc(sizeof(char) * 100);
  59.  
  60.     p->setName=Person_setName;
  61.     p->getName=Person_getName;  // it is here i get an error.
  62.  
  63.  
  64.     printf("===============\n");
  65.     printf("Register person\n");
  66.     printf("===============\n");
  67.     printf("Name: ");
  68.     scanf("%s", name);
  69.     printf("ID: ");
  70.     scanf("%s", id);
  71.  
  72.     p->navn=(name);
  73.     p->id=(id);
  74.     printf("\n Navn: %s\n Id: %s ", p->navn,p->id);
  75.  
  76. }
Apr 16 '10 #1
5 6040
newb16
687 Contributor
You have a typo(?) in Person_getNavn() name, it may be the reason. If not, they also differ by return type - char and char*.
Apr 16 '10 #2
plazzasele
4 New Member
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* .??
Apr 16 '10 #3
plazzasele
4 New Member
@newb16
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..??


Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct Person{
  5.        char *name;
  6.        char *id;
  7.        void (*setName)(struct Person *this, char *name);
  8.        char (*getName)(struct Person *this);
  9.        void (*setId)(struct Person *this, char *id);
  10.        char (*getId)(struct Person *this);
  11.        };
  12. void Person_setName(struct Person *this, char *name) {
  13.     this->name = name;
  14. }
  15.  
  16. char Person_getName(struct Person *this) {
  17.     return *this->name;
  18. }
  19. void Person_setId(struct Person *this, char *id) {
  20.     this->id = id;
  21. }
  22.  
  23. char Person_getId(struct Person *this) {
  24.     return *this->id;
  25. }
  26. void menu();
  27. void regPerson();
  28.  
  29. int main(char *argv[], int argc) {
  30.     menu();
  31.     return 0;
  32. }
  33.  
  34. void menu() {
  35.     int c = -1;
  36.     while (c != 0) {
  37.         printf("\n==========\n");
  38.         printf("Main Menu:\n");
  39.         printf("==========\n");
  40.         printf("0 - Exit\n");
  41.         printf("1 - Register new person\n");
  42.         printf("Choice: ");
  43.         scanf("%i", &c);
  44.  
  45.         switch (c) {
  46.             case 0:
  47.                 printf("\nGoodbye\n");
  48.                 return;
  49.             case 1:
  50.                 regPerson();
  51.                 break;
  52. default:
  53.                 printf("Unrecognized choice, please try again.\n");
  54.         }
  55.     }
  56. }
  57.  
  58. void regPerson() {
  59.     struct Person *p=(struct Person *)malloc(sizeof(struct Person *));
  60.  
  61.     char *name = (char *)malloc(sizeof(char) * 100);
  62.     char *id = (char *)malloc(sizeof(char) * 100);
  63.  
  64.     p->name=(char *)malloc(sizeof(char) * 100);
  65.     p->id=(char *)malloc(sizeof(char) * 100);
  66.  
  67.     p->setName=(*Person_setName);
  68.     p->getName=*Person_getName;
  69.     p->setId=*Person_setId;
  70.     p->getId=*Person_getId;
  71.     printf("===============\n");
  72.     printf("Register person\n");
  73.     printf("===============\n");
  74.     printf("Name: ");
  75.     scanf("%s", name);
  76.     printf("ID: ");
  77.     scanf("%s", id);
  78.     printf("\nName: %s\n Id: %s\n",name,id);
  79.  
  80.     p->setName(p,&*name);
  81.  
  82.     p->setId(p,&*id);
  83.     printf("test\n");
  84.  
  85.     printf("name: %s\n",p->getNavn(p));
  86.  
  87.     printf("id: %s\n",p->getId);
  88.  
  89.  
  90. }
Apr 16 '10 #4
newb16
687 Contributor
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)(struct 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)(struct Person *this); and change Person_getName accordingly.
You getId() is also wrong.
Apr 17 '10 #5
plazzasele
4 New Member
@newb16
thank you very much newb16..

now it is working.. and i am sorry again for the mistakes.. Navn suppose to be name..
Apr 17 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: isxyos | last post by:
Hello, It's just a warning, but can anybody explain to me what this warning is: warning C4047: '=' : 'unsigned int ' differs in levels of indirection from 'void *' #include <stdio.h> void...
6
by: Jason | last post by:
I have a function (Inet_ntop) that returns const char * and if I try to assign that return value to a char * variable, I get the gcc error message: warning: assignment discards qualifiers from...
29
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
6
by: PraZ | last post by:
Hi all. Here is a simple code, which when compiled with gcc results in the warning "incompatible pointer type" for arg 1, as expected. But this is just what I want to do, because it makes it...
10
by: gk245 | last post by:
I have something like this: #include <stdio.h> main () { struct line { char write; char read;
1
by: wanglei0214 | last post by:
I compiles a program in SLOS, but there is a warning i donot know how to remove? here is the framework of the code: typedef struct device_tree { ...... union {
6
by: subramanian | last post by:
Consider the following program: #include <stdio.h> void myfn(const int **a) { static int i, j, k; a = &i; a = &j;
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
4
by: lovecreatesbea... | last post by:
Gcc only gives out a warning: `assignment discards qualifiers from pointer target type' against code such as following: $ type a.c int main(void) { const char *pc; char *p = pc;
10
by: MisterE | last post by:
typedef struct sg { int a; } G; int c(G* g) { return g->a; }
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.