Structures.

This is a discussion on Structures. within the C Programming forums, part of the General Programming Boards category; I've got the following code: Code: #include <stdio.h> struct first { int a; int b; }; int main() { struct ...

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Structures.

    I've got the following code:

    Code:
    #include <stdio.h>
    struct first {
           int a;
           int b;
    };
    int main()
    {
      struct first;
      scanf(" %d", &first.a);
      scanf(" %d", &first.b);
      getchar();
      return 0;
    }
    My compiler gives me a warning that the variables a and b haven't been declared. I don't really understand what mistake I have made here. Is it with the structure in general, or did I commit a mistake in the syntax?

    Thank You.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    struct first; is the type -- like say int;
    struct first first; is a type and an object -- like int i;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    struct firt is a type
    writing
    struct first;

    as if writing

    int;

    you should declare variable
    Code:
    struct first myVar;
    myVar.a = 7;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    I now have:
    Code:
    #include <stdio.h>
    struct first {
           int a;
           int b;
    };
    int main()
    {
      struct first var;
      scanf(" %d", &var.a);
      scanf(" %d", &var.b);
      getchar();
      return 0;
    }
    If I understand correctly, the following is the case:
    1. I define a structure data type.
    2. I then define the "var" variable within the main function to store data in the "first" structure.
    3. Then I can give data to the "var" variable which is then put into the "first" structure.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by omnificient View Post
    I now have:
    Code:
    #include <stdio.h>
    struct first {
           int a;
           int b;
    };
    int main()
    {
      struct first var;
      scanf(" %d", &var.a);
      scanf(" %d", &var.b);
      getchar();
      return 0;
    }
    If I understand correctly, the following is the case:
    1. I define a structure data type.
    2. I then define the "var" variable within the main function to store data in the "first" structure.
    3. Then I can give data to the "var" variable which is then put into the "first" structure.
    1. Sorta-kinda: you define a new data type called "struct first" (you can have a lot of different kinds of structures running around).
    2. Again, sorta-kinda: you define a variable called "var" of this type "struct first" that you just did.

    I'm getting the impression, just based on these three lines, that you think there's only one structure out there, which isn't true. You can have as many separate "struct first" variables as you want, and they won't interact with each other at all.

  6. #6
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Well that was very helpful. I think I understand it now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

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