Struct Help

Discussion in 'Mac Programming' started by larswik, Dec 28, 2010.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    I just started the Struct section in Learning C. The example steps me through the code and how the #include "structSize.h" file works, I get that. I like to run tests after I learn something to really grasp it but the book says nothing on how to create them. In Xcode I went to new file and selected Header File and named it something, but I am unable to access it using the methods described in the book. He says to use the #include file pop up but I don;t see the new file that I created.

    What am I doing wrong?

    -Lars
     
  2. Cromulent, Dec 28, 2010
    Last edited: Dec 28, 2010

    Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Since I don't own that book I don't have a clue what you are talking about.

    You can declare a struct anywhere using a header is just (good) convention. When you place it in a header you are placing it in global scope so that any file that includes that particular header file can declare and use that particular struct.

    So a stupid (and pointless) example would be:

    test_header.h:

    Code:
    struct mystruct
    {
       int numPeople;
       int avgAge;
       float avgHeight;
    } mystruct;
    
    test_file.c:
    Code:
    #include <stdlib.h>
    #include "test_header.h"
    
    int main(void)
    {
       struct mystruct *people = malloc(sizeof(struct mystruct));
       if(people == NULL)
       {
           return -1;
       }
    
       people->numPeople = 5;
       people->avgAge = 37;
       people->avgHeight = 164.46f;
    
       printf("The number of people is: %d.\n", people->numPeople);
    
       free(people);
    
       return 0;
    }
    
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    Cromulent - Thanks. I am having a hard time creating is a separate .h file in the project folder that I can use in the project. Here is a photo from the book that show what he is doing, which is switching between Main.c and his structSize.h file. I thought you could do this by just selecting 'new file' in Xcode. Then select 'Header File' from the 'C and C++' pop up area to create this file.

    When I did this I noticed it showing up in the folder to the left called 'Source', but still can't access it from this pulldown as shown from the photo. Plus I get an error saying "Invalid Processing Directive #included". My guess is that it is not seeing the new header file that I created.

    That is what I am trying to understand how to create.
     

    Attached Files:

  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    #include
    not
    #included

    Not sure about the IDE GUI stuff, don't believe in it for new programmers.

    -Lee
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    The GUI popup requires well-formed syntax. #included is not well-formed. Therefore, the GUI popup won't show any file named by #included.

    If you have an uncompilable source file, some GUI elements won't show the things that are uncompilable. It doesn't understand what you intend. It only understands what you say. If you say spmfang unintelligible, it will mokr mosticks.
     
  6. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #6
    It's not a GUI, it is just a photo from the book showing you how to switch between your main.c and the .h. It works with the sample program that came with the book but when I try to create my own .h file to learn I can't get it to work, Or I can't switch between main.c and the .h file to add my struct to.

    The book is geared to show you programing through their examples but fails to show the person learning how to create their own .h file.

    The #include typo was my mistake rewriting the error that I got from my complied code.

    Cromulent explained why it is good to use the .h files. I need to know how to create a .h file that I can access with my main.c when I create a new C project from xcode.

    -Lars
     
  7. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #7
    And that photo is of a GUI. It is a picture showing the Xcode GUI.

    File Menu -> New File. Select the C and C++ option on the left and then select the "Header File" option on the right the rest is self explanatory.
     
  8. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    Yep, That is what I did but this time around it worked. I must have done something wrong with my test code.

    Thank you.

    -Lars
     
  9. z0rt macrumors newbie

    Joined:
    Jan 3, 2011
    #9
    Headers aside, one should note that people is also a dangling pointer. Instead, the code should read something like,
    Code:
    ...
    free(people);
    people = NULL;
    ...
    
    (NB., elipses mean whatever precedes and follows those two lines of code.)
     

Share This Page