Like Tree1Likes
  • 1 Post By laserlight

Can someone explain this..??

This is a discussion on Can someone explain this..?? within the C Programming forums, part of the General Programming Boards category; Could not able to understand in line 13 that how a pointer function is assigned to a struct . And ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    35

    Can someone explain this..??

    Could not able to understand in line 13 that how a pointer function is assigned to a struct . And on line 15 *who is allocated with some memory space . normally a pointer is assigned to an address but how this way serves the purpose... By the way it is from the book Learn code the hard way...Please shed some light...

    Code:
    1  #include <stdio.h>
    2  #include <assert.h>
    3  #include <stdlib.h>
    4  #include <string.h> 
    5  
    6  struct Person {
    7      char *name;
    8      int age;
    9      int height;
    10      int weight; 
    11  };
    12  
    13  struct Person *Person_create(char *name, int age, int height, int weight) 
    14  {
    15      struct Person *who = malloc(sizeof(struct Person)); 
    16      assert(who != NULL);
    17  
    18      who->name = strdup(name);
    19      who->age = age; 
    20      who->height = height;
    21      who->weight = weight;
    22  
    23      return who;
    24  } 
    25  
    26  void Person_destroy(struct Person *who)
    27  {
    28      assert(who != NULL);
    29   
    30      free(who->name);
    31      free(who);
    32  }
    33  
    34  void Person_print(struct Person *who) 
    35  {
    36      printf("Name: %s\n", who->name);
    37      printf("\tAge: %d\n", who->age); 
    38      printf("\tHeight: %d\n", who->height);
    39      printf("\tWeight: %d\n", who->weight); 
    40  }
    41  
    42  int main(int argc, char *argv[])
    43  {
    44      // make two people structures 
    45      struct Person *joe = Person_create(
    46              "Joe Alex", 32, 64, 140);
    47   
    48      struct Person *frank = Person_create(
    49              "Frank Blank", 20, 72, 180); 
    50  
    51      // print them out and where they are in memory 
    52      printf("Joe is at memory location %p:\n", joe);
    53      Person_print(joe);
    54   
    55      printf("Frank is at memory location %p:\n", frank);
    56      Person_print(frank); 
    57  
    58      // make everyone age 20 years and print them again 
    59      joe->age += 20;
    60      joe->height -= 2;
    61      joe->weight += 40; 
    62      Person_print(joe);
    63  
    64      frank->age += 20;
    65      frank->weight += 20; 
    66      Person_print(frank);
    67  
    68      // destroy them both so we clean up 
    69      Person_destroy(joe);
    70      Person_destroy(frank);
    71  
    72      return 0;
    73  }

    I tried to replace that function with the my understanding and the code works fine.....
    Code:
    int create_profile(char *name,int age,int height,int weight)
    {
        struct Nani *item = malloc(sizeof(struct phani));
        assert(item != NULL);
    
        item->heissen = name;
        item->alt     = age;
        item->grosse  = height;
        item->gewicht = weight;
    
    
    
       return item;
    }

    Thanks n Regards
    Max
    Last edited by seemaxie; 06-26-2012 at 09:01 AM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The function Person_create creates a new struct person with the arguments passed to the function and returns a pointer to the new struct with all members filled in with the arguments. The malloc in line 15 allocates memory for the struct person that will be returned. What is it you don't understand exactly?

    Edit: regarding your function, you are sharing *name with the struct and where ever that originated. The first code uses strdup() which creates a new unique string that is not depending on the argument string. If that string is local, or is deallocated then item->heissen will also disappear.
    Last edited by Subsonics; 06-26-2012 at 09:12 AM.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Line 13:
    Code:
    struct Person *Person_create(char *name, int age, int height, int weight)

    says that the Person_create function returns a pointer to struct Person.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    Hi Subsonics,
    I dont understand how the function Person_create creates a new struct person with the arguments passed to the function and why malloc is used to allocate memory .... And i forgot to use strdup with my function and im aware about its usage ...

    Normally, i have noticed something like passing an address
    [code]
    struct person profile;
    struct Person *who = &profile

    [\code]

    Thank you
    Max
    Last edited by seemaxie; 06-26-2012 at 09:25 AM.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by seemaxie View Post
    Normally, i have noticed something like passing an address

    Code:
    struct person profile;
    struct Person *who = &profile
    Yes, that will pass the address to "struct person profile". However "who" is just a second reference to "struct person profile" you do not get two instances of "struct person profile" by doing this. Also if this is declared inside a function then "struct person profile" is local.

    If you want to create an arbitrary amount of "struct persons" you need to do it manually with that method. Let's say you want 1.000 "struct persons" from a file, then you can read the details from the file and call Person_create in a loop.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    Im breaking my head to get this concept but if i want 1000 struct person i would do this
    struct person profile[1000];
    Last edited by seemaxie; 06-26-2012 at 09:51 AM.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by seemaxie View Post
    Im breaking my head to get this concept but if i want 1000 struct person i would do this
    struct person profile[1000];
    What I really meant was "arbitrary amount", 1.000 was an example, consider a situation where you do not know how many struct persons you will need. Also, creating an array of struct person like that is local to the function that creates it, unless you create a global variable.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    Quote Originally Posted by seemaxie View Post
    Hi Subsonics,
    I dont understand how the function Person_create creates a new struct person with the arguments passed to the function and why malloc is used to allocate memory
    That's kind of the question and the answer, right there.

    The 'how' is malloc, and the 'why' is to create a new 'person'.

    There is no "Normally" with a pointer. They are frequently used to hold heap allocated objects, and they are frequently used to refer to non-owned objects such as an objects parent.
    They are very seldom used to point to a local variable. Contrary to what you may have seen, that practice is usually limited to examples given to beginners.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    Thanks all , special thanks to subsonics .. i lacked the understanding of "returning a pointer to an another pointer" for example
    Code:
    int m = 30,*j,*g;
    m= &j;
    g = m; (i have no idea that doing this points to variable j so the m)
    [/code]

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    I have made few changes to program and could able to print the values in a struct . Someone please help me out..
    Code:
    #include<stdio.h>
    #include<assert.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct profile{
        char *heissen;
        int alt;
        int grosse;
        int gewicht;
    };
    
    struct profile *create_profile(char *name,int age,int height,int weight)
    {
        struct profile *who ; // Made changes replacing malloc
        who->heissen = name;
        who->alt     = age;
        who->grosse  = height;
        who->gewicht = weight;
    
        return who;
    }
    
    
    void print_profile(struct profile *who)
    {
        printf("%s name \n= ",who->heissen);
        printf("%d alt\n",who->alt);
        printf("%d grosse\n",who->grosse);
        printf("%d gewicht\n\n",who->gewicht);
    
    }
    
    int main()
    {
        struct profile *details = create_profile("Maxie", 24,6,68);
        print_profile(details);
    
    return 0;
    }
    I just cant understand why is that code is not able to execute . I tried checking the behavior of program by removing the malloc but dont understand why is that it is not working..

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,633
    Quote Originally Posted by seemaxie
    I just cant understand why is that code is not able to execute .
    You simply never created a struct profile object. Look at this:
    Code:
    struct profile *who;
    who->heissen = name;
    So, you declared a pointer, named who, to a struct profile. This pointer was not initialised, so it does not actually point to a struct profile object. Next, you assign name to the heissen member of the struct profile object that who points to. But who does not point to a struct profile object!
    seemaxie likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    Thank you laserlight ..!! Enlightened ..!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. no one is able to explain!
    By c_lady in forum C Programming
    Replies: 3
    Last Post: 05-12-2010, 09:47 AM
  2. explain please
    By prasanna in forum C Programming
    Replies: 2
    Last Post: 07-08-2007, 05:14 PM
  3. Please explain help me!!
    By KySiRo in forum C++ Programming
    Replies: 6
    Last Post: 06-10-2007, 12:03 PM
  4. Can someone explain to me
    By rEtard in forum Windows Programming
    Replies: 1
    Last Post: 06-22-2005, 11:09 AM
  5. Could someone explain why this happens
    By GSLR in forum C Programming
    Replies: 3
    Last Post: 05-13-2003, 09:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21