C return the structure pointer
source link: https://www.codesd.com/item/c-return-the-structure-pointer.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
C return the structure pointer
Let's say I have a struct like this:
struct Person {
int age;
char *name;
Person *next_ptr;
}
Now I have a function that generates 2 people and returns pointer to the first struct:
Person *GetPerson(){
char[5] p1name = "John";
char[4] p2name = "Bob";
struct Person *p1;
struct Person *p2;
p2 = malloc(sizeof(struct Person));
strcpy(p2.name, p2name);
p2->age = 25;
p1 = malloc(sizeof(struct Person));
strcpy(p1.name, p1name);
p1->age = 20;
p1->next_ptr = p2;
return p1;
}
And the usage of the function to extract both Person.
struct Person *person = malloc(sizeof(struct Person));
person = GetPerson();
int person1age = person.age; // Get age
char person1name[4] = person.name; // Get name
int *person2_ptr = person.next_ptr; // Extract the person 2 pointer
struct Person *person2 = malloc(sizeof(struct Person));
person2 = (*person2_ptr);
char person2name[4] = person2.name; // gets person 2 name
int person2age = person2; // get person 2 age
I hope I made clear what I am attempting to do. Can someone show me the correct way to implement this?
Unfortunately there are a lot of mistakes in your code.
1. In C, unlike C++, every time you want to make an instance of struct Person
, you have to say struct Person
, not Person
2. In C, the way to declare a char
array of size 5 is char p1name[5];
not char[5] p1name;
- Inside the body of
GetPerson()
p1
andp2
are declared as pointers, that meansstrcpy(p1.name,p1name);
should actually bestrcpy(p1->name,p1name);
or evenstrcpy((*p1).name,p1name);
if you prefer.
More serious problems:
You have to notice that when you allocate memory dynamically for an instance of
struct Person
, you do not automatically allocate memory for thechar * name;
; because of this callingstrcpy(p1->name,p1name);
will fail. You could keep thechar*
and allocate memory for it dynamically, but for the sake of simplicity I would recommend you just pick achar
array of reasonably large size.Remember that
GetPerson()
allocates memory for an instance ofstruct Person
and returns a pointer. Now inside your main function you have:struct Person *person = malloc(sizeof(struct Person)); //allocating memory in main person = GetPerson(); // you just leaked the memory you allocated in main
You are allocating memory twice. This will not cause your program to crash, but will cause a "memory leak" which you need to avoid. You always want to
free()
dynamically allocated memory.
Put that together and you get this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Person {
int age;
char name[256];
struct Person *next_ptr;
};
struct Person * GetPerson(){
char p1name[5] = "John";
char p2name[4] = "Bob";
struct Person *p1;
struct Person *p2;
p2 = malloc(sizeof(struct Person));
strcpy(p2->name, p2name);
p2->age = 25;
p2->next_ptr=NULL;
p1 = malloc(sizeof(struct Person));
strcpy(p1->name, p1name);
p1->age = 20;
p1->next_ptr = p2;
return p1;
}
int main()
{
struct Person * tmp=GetPerson();
printf(tmp->name); //prints John
printf("\n");
printf(tmp->next_ptr->name); //prints Bob
printf("\n");
free(tmp);
return 0;
}
Notice that this is still not great code logically, but it does not have bugs.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK