2

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.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
user1186173
  • 565
  • 3
  • 7
  • 26
  • Please can you add the tag 'homework' if it's homework? It's not pejorative. Thanks – Tim Sep 09 '12 at 19:04
  • I don't have time for a full answer, but you say you know Obj-C? A struct is like an Obj-C class definition except it only contains data - never any methods. It's more like an NSDictionary than an Object. – Abhi Beckert Sep 09 '12 at 19:14
  • 2
    One thing that typedef does is make it somewhat more difficult to deal with an object of that type (since the structure type itself has no name - only a pointer to the structure does). So it will be difficult to have a local variable of that structure type, and you'd only be able to get the size of the struct by using a pointer dereference expression: `pStudentRecord p = malloc(sizeof(*p));` – Michael Burr Sep 09 '12 at 19:18
  • 2
    On a side note, don't `typedef` pointer types unless they are truly opaque. – Ed S. Sep 09 '12 at 19:21
  • Why would I need to get the size though? Is it needed to add to the g_ppRecords* itself? Also, when I do that, I get an error saying "a value of type void* cannot be used to initialize an entity of type pStudentRecord" – user1186173 Sep 09 '12 at 19:21
  • 1
    Once upon a time, people learned programming languages by reading books -- language manuals or tutorials. It's still a good practice. I suggest Kernigan and Ritchie, "The C Programming Language (Second Edition)". – Jim Balter Sep 09 '12 at 19:47
  • lol Thank you very much, I have a text book for C, but I'm having a hard time understanding these concepts. I'll look at that one as well, thanks – user1186173 Sep 09 '12 at 19:48
  • @JimBalter's suggestion is an excellent one. The book he refers to is short, very clear, and was written by the creators of the language. There really are no better books about pure C than that one. – Tim Sep 09 '12 at 22:52

4 Answers4

2

defines a pointer to the struct described within the curly bracers, here is a simpler example

typedef struct {
    int x;
    int y;
}Point,* pPoint;

int main(void) {
   Point point = {4,5};
   pPoint point_ptr = &point;
   printf("%d - %d\n",point.x,point_ptr->x);

   pPoint second_point_ptr = malloc(sizeof(Point));
   second_point_ptr->x = 5;
   free(second_point_ptr);
}
jackdoe
  • 1,846
  • 1
  • 15
  • 13
2

The first declares an unnamed struct, and a type pStudentRecord that is a pointer to it. The second declares g_ppRecords to be a pointer to a pStudentRecord. In other words, a pointer to a pointer to a struct.

It's probably easier to think of the second as an "array of pointers". As such, g_ppRecords[0] may point to a pStudentRecord and g_ppRecords[1] to another one. (Which, in turn, point to a record struct.)

In order to add to it, you will need to know how it stores the pointers, that is, how one might tell how many pointers are stored in it. There either is a size somewhere, which for size N, means at least N * sizeof(pStudentRecord*) of memory is allocated, and g_ppRecords[0] through g_ppRecords[N-1] hold the N items. Or, it's NULL terminated, which for size N, means at least (N+1) * sizeof(pStudentRecord*) of memory is allocated and g_ppRecords[0] through g_ppRecords[N-1] hold the N items, and g_ppRecords[N] holds NULL, marking the end of the string.

After this, it should be straightforward to create or add to a g_ppRecords.

aib
  • 45,516
  • 10
  • 73
  • 79
1

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

typedef int BOOL;

(There's no built-in BOOL type in older ANSI C.)

This means you can now do things like:

BOOL state = 1;

and declare functions that take BOOL parameters, then have the compiler make sure you're passing BOOLs even though they're really just ints:

void flipSwitch(BOOL isOn); /* function declaration */
...
int value = 0;
BOOL boolValue = 1;
flipSwitch(value); /* Compiler will error here */
flipSwitch(boolValue); /* But this is OK */

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 StudentRecord every 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:

typedef struct {
  char * firstName;
  char * lastName;
  int id;
  float mark;
} StudentRecord;

You can now do things like:

StudentRecord aStudent = { "Angus\n", "Young\n", 1, 4.0 };

or

void writeToParents(StudentRecord student) {
    ...
}

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 writeToParents which 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:

void changeGrade(StudentRecord *student, float newGrade) {
  student->mark = newGrade;
}

Easy to see that you might miss the *, so instead, typedef a pointer type for StudentRecord and the compiler will help:

typedef struct { /* as above */ } *PStudentRecord;

Now:

void changeGrade(PStudentRecord student, float newGrade) {
  student->mark = newGrade;
}

It's more common to declare both at the same time:

typedef struct {
  /* Members */
} StudentRecord, *PStudentRecord;

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

Tim
  • 5,024
  • 2
  • 30
  • 58
  • Wow! That was very thorough, thank you! I'm still having a hard time understanding how to add to an "array of pointers" of *g_ppRecords. I basically have an int value, which needs to reflect the amount of pStudentRecords in *g_ppRecords. I then need to add a pStudentRecord based on a current value, like i in a for loop. I think I understand how to allocate enough memory, I believe it's something like this g_ppRecords[num * sizeof(pStudentRecord*)];, however I don't know that next step. – user1186173 Sep 09 '12 at 19:39
  • You're welcome @user1186173. As to the array, that's another question. You want to start reading up about dynamic arrays in C. You could go right here, too, for a similar question: http://stackoverflow.com/questions/2024448/c-dynamic-array-of-pointers-to-array-of-structure – Tim Sep 09 '12 at 22:50
0

This defines the name of a pointer to the structure but not a name for the structure itself. Try changing to:

typedef struct
{
    char* firstName;
    char* lastName;
    int id;
    float mark;
} StudentRecord;

StudentRecord foo;
StudentRecord *pfoo = &foo;
Musa
  • 96,336
  • 17
  • 118
  • 137
ddyer
  • 1,792
  • 19
  • 26