r123454321
r123454321

Reputation: 3403

Instantiations for structs in C?

So I'm trying to learn C right now, and I have some basic struct questions I'd like to clear up:

Basically, everything centers around this snippet of code:

#include <stdio.h>
#include <stdlib.h>

#define MAX_NAME_LEN 127

const char* getName(const Student* s);
void setName(Student* s, const char* name);
unsigned long getStudentID(const Student* s);
void setStudentID(Student* s, unsigned long sid);

int main(void) {
    Student sarah;
    const char* my_name = "Sarah Spond";
    setName(&sarah, my_name);
    printf("Name is set to %s\n", sarah.name);
}

typedef struct {
    char name[MAX_NAME_LEN + 1];
    unsigned long sid;
} Student;

/* return the name of student s */
const char* getName (const Student* s) { // the parameter 's' is a pointer to a Student struct
    return s->name; // returns the 'name' member of a Student struct
}

/* set the name of student s
If name is too long, cut off characters after the maximum number of characters allowed.
*/
void setName(Student* s, const char* name) { // 's' is a pointer to a Student struct |     'name' is a pointer to the first element of a char array (repres. a string)
    int iStringLength = strlen(name);
    for (i = 0; i < iStringLength && i < MAX_NAME_LEN; i++) {
        s->name[i] = name[i];
}   
}

/* return the SID of student s */
unsigned long getStudentID(const Student* s) { // 's' is a pointer to a Student struct
    return s->sid;
}

/* set the SID of student s */
void setStudentID(Student* s, unsigned long sid) { // 's' is a pointer to a Student struct | 'sid' is a 'long' representing the desired SID
    s->sid = sid;
}

However, when I try and compile the program, I get a bunch of errors saying that there's an "unknown type name Student". What am I doing wrong?

Thanks!

Upvotes: 0

Views: 266

Answers (6)

FacundoGFlores
FacundoGFlores

Reputation: 8118

A basic structure of a C program is:

//======DOCUMENT SECTION=========
//File:test.c
//Author:
//Description:
//...
//================================

//====INCLUDE SECTION=============
#include "lib1"
#include <lib2>
//================================

//========DEFINITIONS SECTION=====

#define TRUE 1
#define FALSE 0

//================================

//========STRUCTURES SECTION======
struct P{
};
//================================

//========TYPEDEFS SECTION========
typedef *P P;
//================================

//========FUNCTION HEADERS========
void foo1(...);
int foo2(...,...,...);
//================================


//=========GLOBAL VARIABLES=======
int GLOBAL_INT;
float GLOBAL_FLOAT;
//================================ 


//=====MAIN FUNCTION DEFINITION===
void main(void)
{
    ...
    ...
    ...
}
//=================================


//======FUNCTIONS DEFINITION======
void foo1(...)
{
}

int foo2(...,...,...)
{
}
//================================

A main function is where a C program starts. A main function also typically has access to the command arguments given to the program when it was executed.

Usually you have got:

int main(void);
int main();

int main(int argc, char **argv);
int main(int argc, char *argv[]);

Upvotes: 0

Djibril NDIAYE
Djibril NDIAYE

Reputation: 216

> Move the typedef .. right after #define MAX_NAME_LEN 127, i.e. before
> it's being used.

OR, if you want to keep your definition after, and if you are ready to use a pointer to Student, you can:


#include <stdio.h> 
#include <stdlib.h> 

#define MAX_NAME_LEN 127 

// forward declare Student ici
struct Student;

//...

// in main, use a pointer to student
int main(void) { 
    Student *sarah;                             // Changed to pointer
    const char* my_name = "Sarah Spond"; 
    setName(sarah, my_name);                    // Pass the pointer instead of reference
    printf("Name is set to %s\n", sarah->name); // Use the pointer

    //....
    delete sarah;                               // delete object when done
} 


// Change struct decl to the following          // can't explain the diff yet
struct Student {  
    char name[MAX_NAME_LEN + 1];  
    unsigned long sid;  
};

Upvotes: 0

Dave Doknjas
Dave Doknjas

Reputation: 6542

As cnicutar said, move the typedef - the reason for this is that the type must be known before it's used. Alternatively, you can forward declare the type.

Upvotes: 0

MichaelT
MichaelT

Reputation: 7944

Struct declarations need to be defined before you use them , so you need to move your Student

Upvotes: 0

mathematician1975
mathematician1975

Reputation: 21351

You need to move the declaration of the Student struct above the first time it is referenced by other code - otherwise those functions will not know what it is.

Upvotes: 0

cnicutar
cnicutar

Reputation: 182774

Move the type definition for Student - the typedef .. right after #define MAX_NAME_LEN 127, i.e. before it's being referenced.

Upvotes: 1

Related Questions