Thread: Problem about Creating structure

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    Problem about Creating structure

    hi all, i've little problem about structure creating, loot at this first..
    Code:
    .......
    struct student
    {
      int score;
    };
    
    struct student *creat()
    {
      struct student *head;
      head = (struct student *)malloc (sizeof (struct student));
      scanf("%d", &head->score);   /* my problem is here*/
      return (head);
    }
    
    int main()
    {
      struct student *pointer;
      pointer = creat();
      ............
    }
    if i don't have a misconception, head is a pointer, it just receives an address from malloc(), and it points to an empty space created by malloc(). but here
    Code:
    head = (struct student *)malloc (sizeof (struct student));
    scanf("%d", &head->score);
    i don't understand : head->score.
    did we declare a varible -score while mallocing space?
    if not, how can we input a number to it(score)?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Apart from casting the result of malloc (which is poor form in C, see the FAQ), it looks fine.

    > i don't understand : head->score.
    It's basically the same as
    struct student var;
    scanf("%d", &head.score);

    Except you're dealing with a pointer to a struct rather than a struct (hence the -> in place of .)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    58
    Code:
    scanf("%d", &(head->score));
    I'm not sure about the precedence so i use extra parentesis.

    maybe it interpreted it like:

    Code:
    (&head)->score
    Which makes no sense.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Which makes no sense.
    One of the reasons that the precision of operators was choosen in a way that the code

    &head->score

    does make sense
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 05-21-2007, 12:10 AM
  2. Creating menu, problem
    By Tehy in forum C Programming
    Replies: 2
    Last Post: 04-13-2007, 05:19 AM
  3. Structure Problem
    By Generator in forum C Programming
    Replies: 5
    Last Post: 07-28-2003, 11:54 PM
  4. Structure problem
    By QuincyEQ in forum C Programming
    Replies: 3
    Last Post: 05-29-2003, 09:49 AM
  5. Pointer to structure problem
    By unregistered in forum C Programming
    Replies: 3
    Last Post: 12-24-2001, 07:54 AM