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. - #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);
-
-
}
5 6040
You have a typo(?) in Person_getNavn() name, it may be the reason. If not, they also differ by return type - char and char*.
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* .??
@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..?? - #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);
-
-
-
}
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.
@newb16
thank you very much newb16..
now it is working.. and i am sorry again for the mistakes.. Navn suppose to be name..
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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)
{
|
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...
|
by: gk245 |
last post by:
I have something like this:
#include <stdio.h>
main ()
{
struct line
{
char write;
char read;
| |
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
{
|
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;
|
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);
|
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;
|
by: MisterE |
last post by:
typedef struct sg
{
int a;
} G;
int c(G* g)
{
return g->a;
}
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |