I’m really new to C programming and I’m still trying to understand the concept of using pointers and using typedef structs.
I have this code snippet below that I need to use in a program:
typedef struct
{
char* firstName;
char* lastName;
int id;
float mark;
}* pStudentRecord;
I’m not exactly sure what this does – to me it seems similar as using interfaces in Objective-C, but I don’t think that’s the case.
And then I have this line
pStudentRecord* g_ppRecords;
I basically need to add several pStudentRecord to g_ppRecords based on a number. I understand how to create and allocate memory for an object of type pStudentRecord, but I’m not sure how to actually add multiple objects to g_ppRecords.
A struct is a compound data type, meaning that it’s a variable which contains other variables. You’re familiar with Objective C, so you might think of it as being a tiny bit like a ‘data only’ class; that is, a class with no methods. It’s a way to store related information together that you can pass around as a single unit.
Typedef is a way for you to name your own data types as synonyms for the built-in types in C. It makes code more readable and allows the compiler to catch more errors (you’re effectively teaching the compiler more about your program’s intent.) The classic example is
(There’s no built-in BOOL type in older ANSI C.)
This means you can now do things like:
and declare functions that take
BOOLparameters, then have the compiler make sure you’re passingBOOLs even though they’re really justints:So your typedef above is creating a synonym for a student record struct, so you can pass around student records without having to call them
struct StudentRecordevery time. It makes for cleaner and more readable code. Except that there’s more to it here, in your example. What I’ve just described is:You can now do things like:
or
But you’ve got a
*after the typedef. That’s because you want to typedef a data type which holds a pointer to a StudentRecord, not typedef the StudentRecord itself. Eh? Read on…You need this pointer to StudentRecord because if you want to pass StudentRecords around and be able to modify their member variables, you need to pass around pointers to them, not the variables themselves. typedefs are great for this because, again, the compiler can catch subtle errors. Above we made
writeToParentswhich just reads the contents of the StudentRecord. Say we want to change their grade; we can’t set up a function with a simple StudentRecord parameter because we can’t change the members directly. So, we need a pointer:Easy to see that you might miss the *, so instead, typedef a pointer type for StudentRecord and the compiler will help:
Now:
It’s more common to declare both at the same time:
This gives you both the plain struct typedef and a pointer typedef too.
What’s a pointer, then? A variable which holds the address in memory of another variable. Sounds simple; it is, on the face of it, but it gets very subtle and involved very quickly. Try this tutorial