0

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!

2
  • I notice your setName function never sticks a null onto the end of the string. If the string's length is MAX_NAME_LEN on the first call, I do believe it would end up with no null.
    – Qaz
    Commented Sep 9, 2012 at 18:33
  • Ah, I was almost right. You need to set s->name[i + 1] to 0 regardless.
    – Qaz
    Commented Sep 9, 2012 at 18:52

6 Answers 6

1

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

3
  • Thanks guys. So does this only apply to struct declarations? Or should I just put my main at the bottom of the .c file no matter what...
    – r123454321
    Commented Sep 9, 2012 at 18:37
  • It applies to anything you declare. First declare it, then use it :)
    – Eitan T
    Commented Sep 9, 2012 at 18:40
  • @RyanYu, If you like it at the top, have your structures in a different file and forward-declare any global functions defined in your main file.
    – Qaz
    Commented Sep 9, 2012 at 18:45
0

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.

0

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

0

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.

1
  • 1
    Forward declaration won't help here. Look at main... it uses struct Student and not a pointer to Student.
    – Eitan T
    Commented Sep 9, 2012 at 18:39
0
> 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;  
};
0

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[]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.