several beginner questions...

This is a discussion on several beginner questions... within the C Programming forums, part of the General Programming Boards category; First question: I am wondering if there is a way to loop through structures. I can see how this might ...

  1. #1
    Maybe? Maybe not...
    Join Date
    Feb 2005
    Location
    Atlanta
    Posts
    16

    Question several beginner questions...

    First question:

    I am wondering if there is a way to loop through structures. I can see how this might be bad form and an even worse way to make a reliable program but its more out of curiosity than anything I need to use.

    In VB there was a "for each" statement that would loop through everything in a class is there something similar in C?

    Second Question:

    I know there are many many posts on headers and a faq I read a majority of them that actually dealt with the creation and format but Im still not quite sure I get it.

    Code:
      
    someheader.h
     
    struct somestruct{
    	int someint;
    	float somefloat;
    	char somechar;
    	char somestring[10];
    };
    and then in a C file

    Code:
     
    someheader.c
     
    #include <someheader.h>
     
    int main()
    {
       struct somestruct NewStruct = {0,0,"",""};
     
       goodfunction(NewStruct);
     
       return 0;
    }
     
    struct somestruct goodfunction(struct somestruct NewStruct)
    {
     
    	useful code here;
    	here too;
    	some good stuff here as well;
     
       return NewStruct;
     
    }

    and then compile them together?
    Is that right at all?
    How does the data get passed between the two files? How does the program I use it in know whats in the file?

    As a bit of the why to this post and probably several more. I am a full time student. I am in an Into to C course. This is for me not for class I could write those in my sleep right now. The instructor doesn't teach fast enough or go in depth very much.

    Last question is what is a good resource to learn the network end of C, there seems to be a few different standards of C and I know there are a few different network types even though I think most these days are ethernet and I couldnt get near a fiber network if I wanted too so I think Im looking for mostly basic things that arent dependant on 1 os or 1 network type.


    Feel free to rip apart my little code example and make jokes about it...just tell me why, can't fix what you don't see.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I am wondering if there is a way to loop through structures.
    If you mean loop through the members of a structure instance then no, not really. If you mean loop through the elements of a data structure, then sure! Just use a loop.
    Code:
    char *p;
    
    for (p = begin; p != end; p = next) {
      // Yadda yadda
    }
    >and then compile them together?
    No, all you have to compile are the .c files. The .h files will be included implicitly when you compile, during the preprocessing step.

    >How does the program I use it in know whats in the file?
    A copy of the file textually replaces the include directive. So it's as if you typed in the contents of the .h file in the .c file. After preprocessing, your source file will look something like this:
    Code:
    struct somestruct{
        int someint;
        float somefloat;
        char somechar;
        char somestring[10];
    };
    
    int main()
    {
       struct somestruct NewStruct = {0,0,"",""};
     
       goodfunction(NewStruct);
     
       return 0;
    }
     
    struct somestruct goodfunction(struct somestruct NewStruct)
    {
     
        useful code here;
        here too;
        some good stuff here as well;
     
       return NewStruct;
     
    }
    >what is a good resource to learn the network end of C
    Do a google search for "beej". That's always a good starting point.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    There isn't any for each in C. The only way to go through all the elements in a structure is to write a line for each member in the structure.

    At least, so far as I know.

    As for you question on the header files, you have the right idea. Just think of the #include as a command along hte lines of "Copy this entire code file into this code file right here". Take a look at this:

    Your Header:
    Code:
    struct somestruct{
        int someint;
        float somefloat;
        char somechar;
        char somestring[10];
    };
    Your source file:
    Code:
    #include <someheader.h>
     
    int main()
    {
       struct somestruct NewStruct = {0,0,"",""};
     
       goodfunction(NewStruct);
     
       return 0;
    }
     
    struct somestruct goodfunction(struct somestruct NewStruct)
    {
     
        useful code here;
        here too;
        some good stuff here as well;
     
       return NewStruct;
     
    }
    The way the compiler sees it:
    Code:
     struct somestruct{
        int someint;
        float somefloat;
        char somechar;
        char somestring[10];
    };
    
    int main()
    {
       struct somestruct NewStruct = {0,0,"",""};
     
       goodfunction(NewStruct);
     
       return 0;
    }
     
    struct somestruct goodfunction(struct somestruct NewStruct)
    {
     
        useful code here;
        here too;
        some good stuff here as well;
     
       return NewStruct;
     
    }
    So theres no data exchange between the files, they just get merged together.

    Theres a bit more to it than that (no sense confusing the issue wit ha discussion on the preprocessor stage of the compile) but for a fairly simple answer that ought to do.

    As for the network end, look for tutorials on BSD Sockets. Most platforms use some version of them, even the function calls are almost identical. At the very least they all use the same concepts so if you understand the BSD implementation you'll have a good start on learning a sockets on a different platform.

    [edit]You type faster than me Prelude [/edit]
    Last edited by Exile; 02-09-2005 at 08:56 AM.
    So, do you understand everything you know about this yet?

    "Begin at the beginning," the King said, very gravely, "and go on till you come to the end; then stop."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some beginner questions.
    By Meikj in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2009, 11:37 AM
  2. Total beginner questions
    By Sparky in forum C Programming
    Replies: 5
    Last Post: 06-22-2008, 04:50 AM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. beginner questions.
    By Jaken Veina in forum C Programming
    Replies: 5
    Last Post: 03-16-2005, 08:38 PM
  5. 2 beginner questions
    By GCat in forum C++ Programming
    Replies: 15
    Last Post: 11-24-2004, 02:55 AM

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