0
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
struct Person {
char name[50];
     int year_of_birth;
    char sex[7];
    char father[50];
    char mother[50];
    char significant_other[50];
    char children[50];
};

struct Person* person_constructor(char *name, int year_of_birth, char *sex);

int main(){

struct Person* p1 = person_constructor("Abbas", 1970, "male");

}

struct Person* person_constructor(char *name, int year_of_birth, char *sex) {
   struct Person *p;
    printf("%s",*name); 
    printf("%s",*sex);
    printf("%d",&year_of_birth);
// how to initalise these here and return name, age and sex everytime , can you tell me in print function
}

i want to do : Person* person_constructor(char *name, int year_of_birth, char *sex); A person with the given arguments and return it. Also allocate memory.

Ansh Varun
  • 100
  • 1
  • 12
  • @TanveerBadar NO, its just memory related, i need to know about pointers and structure how this will be done – Ansh Varun Nov 18 '19 at 08:07

1 Answers1

1

In example code bellow you can find one of possible solutions to your question. In C language is not possible to return more then one variable, but you can return pointer to constructed structure object and access structure members using notation stuct_ptr->struct_member.

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
struct Person {
char name[50];
     int year_of_birth;
    char sex[7];
    char father[50];
    char mother[50];
    char significant_other[50];
    char children[50];
};

struct Person* person_constructor(char *name, int year_of_birth, char *sex);

int main(){

struct Person* p1 = person_constructor("Abbas", 1970, "male");

/* it is not possible to return more variables in C */
/* you can use pointer to access members  from constructed structure: */

 printf("print from main:\n %s %d %s \n", p1->name, p1->year_of_birth, p1->sex);

 if( p1 != NULL) free(p1);  /* do not forget do deallocate something taht is allocated */
 return 0;
}

struct Person* person_constructor(char *name, int year_of_birth, char *sex) {

   struct Person *p = calloc(1, sizeof(struct Person));

   if( p == NULL ) return p;  /* memory alocation failed! */

   strcpy(p->name, name);
   p->year_of_birth = year_of_birth;
   strcpy(p->sex, sex);

    printf("print from constructor:\n");
    printf("%s ",p->name);
    printf("%s ",p->sex);
    printf("%d \n",p->year_of_birth);
    return p;
}
risbo
  • 188
  • 7
  • Suggesting using the variable name in the calloc: `struct Person *p = calloc(1, sizeof(*p)) ;` It reduces the risk that of mismatch if one of the type is modified. – dash-o Nov 17 '19 at 18:33
  • this Code still doesn't print anything in these printf – Ansh Varun Nov 18 '19 at 06:33
  • #tested on Ubuntu 18.04 #compile the code with: $ cc person.c -o person #execute with command: $ ./person #result should display: print from constructor: Abbas male 1970 print from main: Abbas 1970 male – risbo Nov 18 '19 at 20:58
  • @risbo thanks for the solution – Ansh Varun Nov 21 '19 at 05:19