Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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
 
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

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;
}
 
Last edited:
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.
 

Attachments

  • example.jpg
    example.jpg
    51.5 KB · Views: 122
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.
 
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
 
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.

And that photo is of a GUI. It is a picture showing the Xcode GUI.

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

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.
 
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
 
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.)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.