0

所以我现在正在尝试学习 C,并且我有一些基本的结构问题想要解决:

基本上,一切都围绕着这段代码:

#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;
}

但是,当我尝试编译程序时,我收到一堆错误,说有一个“未知类型名称 Student”。我究竟做错了什么?

谢谢!

4

6 回答 6

1

将类型定义移动到Student-typedef ..之后#define MAX_NAME_LEN 127,即在它被引用之前。

于 2012-09-09T18:32:06.087 回答
0

您需要将Student结构的声明移到其他代码第一次引用它的上方 - 否则这些函数将不知道它是什么。

于 2012-09-09T18:33:23.690 回答
0

结构声明需要在使用之前定义,所以你需要移动你的 Student

于 2012-09-09T18:33:35.080 回答
0

正如 cnicutar 所说,移动 typedef - 这样做的原因是在使用之前必须知道类型。或者,您可以转发声明类型。

于 2012-09-09T18:34:08.637 回答
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;  
};
于 2012-09-09T18:50:49.470 回答
0

C程序的基本结构是:

//======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(...,...,...)
{
}
//================================

main 函数是 C 程序开始的地方。主函数通常还可以访问程序执行时提供给程序的命令参数。

通常你有:

int main(void);
int main();

int main(int argc, char **argv);
int main(int argc, char *argv[]);
于 2012-09-09T18:52:12.133 回答