Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redundant naming in C/C++ typedefs/structs

Tags:

c++

c

#include <stdio.h>
#include <string.h>

const int NAMELEN=30;

const int MAXCLASSSIZE=10;

typedef struct StudentRec {
    char lastname[NAMELEN];
    char firstname[NAMELEN];
    long int ID;
    int finalmark;
}Student;

I'm new to coding..and I have a question about why there is Student; after the bracket.. is it a format that we have to follow.

like image 340
bloomy Avatar asked Apr 13 '10 14:04

bloomy


1 Answers

you are confusing two things. In C you can define a struct like this:

struct foo {
    int a, b;
};

Then to use one you have to do this:

struct foo myFoo;

This is really wordy, so they had this brilliant idea of using typedef to make a new type. I can do something like this:

typedef int myInt;

and then use it:

myInt x;

So what you're doing is declaring that there is a new type, Student, which is equivalent to a struct StudentRec. By convention, many people use the same name for the typedef as for the struct - it is legal:

typedef struct foo { int a, b; } foo;
like image 72
plinth Avatar answered Sep 29 '22 17:09

plinth