Problem about Creating structure

This is a discussion on Problem about Creating structure within the C Programming forums, part of the General Programming Boards category; hi all, i've little problem about structure creating, loot at this first.. Code: ....... struct student { int score; }; ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 06:54 AM

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