general question about how struct works.

This is a discussion on general question about how struct works. within the C Programming forums, part of the General Programming Boards category; I have exams tomorrow morning, so i need a answer soon(if it is possible)...i know that we will have a ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    81

    general question about how struct works.

    I have exams tomorrow morning, so i need a answer soon(if it is possible)...i know that we will have a program about structs and files. First, if anyone knows the most important points in structs please post me and second i have a question about a program...here is the code:

    obviously the next program does not compile correct. I try to understand some differences about structs. More specific about passing printing parametres in structs. My teacher told us that we should think structs like boxes. This is what i do. So, i imagine something like the following and i want to correct me if i am wrong.

    There is one big box (struct Foo) which contains:
    1) a box named foo
    2) a box named group1
    3) a box named group2

    and finally each one of these 3 boxes contains x and y variable.
    CORRECT? WRONG?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Foo{
    	int x;
    	int y;
    }group1,group2;
    
    int main(void){
    	struct Foo foo;
    	
    	printf("x: %d\n", struct Foo.x);
    	printf("y: %d\n", struct Foo.y);
    	
    	printf("x: %d\n", foo.x);
    	printf("y: %d\n", foo.y);
    
    	printf("x: %d\n", group1.x);
    	printf("y: %d\n", group1.y);
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by nullifyed View Post
    I have exams tomorrow morning, so i need a answer soon(if it is possible)...i know that we will have a program about structs and files. First, if anyone knows the most important points in structs please post me and second i have a question about a program...here is the code:

    obviously the next program does not compile correct. I try to understand some differences about structs. More specific about passing printing parametres in structs. My teacher told us that we should think structs like boxes. This is what i do. So, i imagine something like the following and i want to correct me if i am wrong.

    There is one big box (struct Foo) which contains:
    1) a box named foo
    2) a box named group1
    3) a box named group2

    and finally each one of these 3 boxes contains x and y variable.
    CORRECT? WRONG?
    Wrong, but close. Foo *defines* what the group1 and group2 structs will have inside them, but it doesn't CONTAIN, anything except Foo definitions.

    And yes, both group1 and group2, contain members x and y: group1.x, group1.y, group2.x and group2.y.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    81
    Quote Originally Posted by Adak View Post
    Wrong, but close. Foo *defines* what the group1 and group2 structs will have inside them, but it doesn't CONTAIN, anything except Foo definitions.

    And yes, both group1 and group2, contain members x and y: group1.x, group1.y, group2.x and group2.y.
    thank you...what happens with 1)?

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    The struct "Foo" doesn't contain the object of struct Foo called "foo"...
    (So 1) is wrong)
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    81
    ok...thanks....

    what about accessing the x and y variables?
    which of the six ways are correct and which wrong?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Quote Originally Posted by nullifyed
    what about accessing the x and y variables?
    which of the six ways are correct and which wrong?
    For this question, you can test it yourself. Comment all of them out except for one, each in turn, and compile. That said, before you test, make an intelligent guess with a good reason, then check to see if you were correct. If your guess is wrong, come back and tell us what you thought it should be, and what it turned out to be. If your guess is correct, congratulations, but of course you could always ask us to confirm the reason.
    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

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The last four, because struct Foo is a datatype, EXACTLY like int and char.

    Code:
    int group1, group2;
    Here, group1 is an int, and group2 is an int. In your example, group1 was a struct Foo, and group2 was a struct Foo.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    81
    i would like to confirm that....i thought tha last four were correct.

    after main() the command struct Foo foo; means that insteed struct Foo we can write foo?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    struct Foo foo, creates or declares the new struct foo, which is of type struct Foo.

    After it's declared, foo is just "foo", and no more "struct foo". That would be an error, because you already declared foo, once.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nullifyed View Post
    after main() the command struct Foo foo; means that insteed struct Foo we can write foo?
    No. That is like saying that in this case:
    Code:
    int number;
    I can now write "number" instead of int.

    So think: it does not make sense to say, what is the value of int? Instead, you would consider the value of an int.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    81
    if an exercise says: make a struct with some variables, what should i do?

    1)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    struct Foo{
    	int x;
    	int y;
    }group;
    int main(void){
    
    	printf("x: %d\n", group.x);
            printf("y: %d\n", group.y);
    return 0;
    }
    or

    2)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    struct Foo{
    	int x;
    	int y;
    };
    int main(void){
    	struct Foo foo;
    	foo.x=10;
    	foo.y=11;
    	
    	printf("x: %d\n", foo.x);
    	printf("y: %d\n", foo.y);
    	return 0;
    }
    ?

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Either one. With the first one, "group" is global.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    81
    Quote Originally Posted by MK27 View Post
    Either one. With the first one, "group" is global.
    can you be more specific please?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Both examples create a new Foo struct. In the first example, it's named group, in the second it's named foo.
    In the first example, group is global. That is, it is available to all functions in your project. In the second, foo is local. That is, it's only accessible to the function main (and it's destroyed as soon as main finishes).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #15
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I usually use typedef to do that.

    Code:
    typedef struct{
            int a;
            int b;
    }foo;
    Now you can use it like this.

    Code:
    foo myfoo = {1, 2};

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  2. struct question
    By ctovb in forum C Programming
    Replies: 3
    Last Post: 08-13-2008, 11:23 AM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. struct problem and general C question
    By techrolla in forum C Programming
    Replies: 8
    Last Post: 01-09-2004, 12:37 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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