鱼C论坛

 找回密码
 立即注册
查看: 5124|回复: 7

typedef的用法是怎样的?

[复制链接]
发表于 2013-8-19 16:42:37 | 显示全部楼层 |阅读模式
1鱼币
#include "stdio.h"
#include "string.h"

typedef struct Student
{
        int age;
        int sid;
        char name[20];
} ST,*PST;

void main()
{
        ST student;
        student.age=17;
        student.sid=55555;
        strcpy(student.name,"lose");
        printf("name:%s  age=%d  sid=%d\n",student.name,student.age,student.sid);
        PST pst=&student;
        strcpy(pst->name,"xtr");
        pst->age=19;
        pst->sid=18181;
        printf("name:%s   age=%d  sid=%d\n",student.name,student.age,student.sid);
        printf("%c%c%c%c\n",student.name[0],student.name[1],student.name[2],student.name[3]);
}
//这个程序中用typedef给结构体起了个别名ST,还有就是指向该结构体的指针类型名PST,但是为什么编译的时候还会出现这样的问题呢:
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.c
F:\C语言\code\空白测试程序\main.c(18) : error C2275: 'PST' : illegal use of this type as an expression
        F:\C语言\code\空白测试程序\main.c(9) : see declaration of 'PST'
F:\C语言\code\空白测试程序\main.c(18) : error C2146: syntax error : missing ';' before identifier 'pst'
F:\C语言\code\空白测试程序\main.c(18) : error C2065: 'pst' : undeclared identifier
F:\C语言\code\空白测试程序\main.c(18) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct Student *'
F:\C语言\code\空白测试程序\main.c(19) : error C2223: left of '->name' must point to struct/union
F:\C语言\code\空白测试程序\main.c(19) : error C2198: 'strcpy' : too few actual parameters
F:\C语言\code\空白测试程序\main.c(20) : error C2223: left of '->age' must point to struct/union
F:\C语言\code\空白测试程序\main.c(21) : error C2223: left of '->sid' must point to struct/union
执行 cl.exe 时出错.


求大神指导,感激不尽

最佳答案

查看完整内容

C语言函数声明要放在最前面,试试将后缀名改为.cpp就可以了,你用的应该是vc6吧 如果你坚持要用C语言,那就把PST pst;这句放在前面,后面再直接使用
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-19 16:42:38 | 显示全部楼层
C语言函数声明要放在最前面,试试将后缀名改为.cpp就可以了,你用的应该是vc6吧


如果你坚持要用C语言,那就把PST pst;这句放在前面,后面再直接使用
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-19 16:54:27 | 显示全部楼层
本帖最后由 苹果沃珂 于 2013-8-19 17:01 编辑

看了一下,没有问题。直接复制,编译也没有问题。效果图附上

                               
登录/注册后可看大图

BaiduShurufa_2013-8-19_16-54-27.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-19 18:35:22 | 显示全部楼层
在我这边编译没有出错,不过 你可以把PST pst=&student;放到ST student;的下面!!!试试
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-19 19:10:58 | 显示全部楼层
你是用的什么编译器编译的?
我记得有的编译器比较严格,如果你是用的严格的,那么可能是PST pst=&student;的位置不对。
你这句话的意思是,定义变量,并且初始化,这些应该放到函数入口处(像tube C的编译器就很严格)
例如:
int a;
int b;
printf("hello world\n");
是正确的。
但是
int a;
printf("hello world\n");
int b;
就可能编译不过。

测试方法也可以如下:

  1. #include "stdio.h"
  2. #include "string.h"

  3. typedef struct Student
  4. {
  5.         int age;
  6.         int sid;
  7.         char name[20];
  8. } ST,*PST;

  9. void main()
  10. {
  11.         ST student;
  12.         student.age=17;
  13.         student.sid=55555;
  14.         strcpy(student.name,"lose");
  15.         printf("name:%s  age=%d  sid=%d\n",student.name,student.age,student.sid);

  16.         {
  17.         PST pst=&student;
  18.         strcpy(pst->name,"xtr");
  19.         pst->age=19;
  20.         pst->sid=18181;
  21.         printf("name:%s   age=%d  sid=%d\n",student.name,student.age,student.sid);
  22.         printf("%c%c%c%c\n",student.name[0],student.name[1],student.name[2],student.name[3]);
  23.         }
  24. }


复制代码
就是把后面的代码用大括号括起来,变为新的模块代码,这样可以测试编译
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-19 21:24:03 | 显示全部楼层
565123 发表于 2013-8-19 16:42
C语言函数声明要放在最前面,试试将后缀名改为.cpp就可以了,你用的应该是vc6吧

嗯嗯,就是你说的这样!!后面我改成这样就能通过了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-19 21:25:10 | 显示全部楼层
佐少love 发表于 2013-8-19 18:35
在我这边编译没有出错,不过 你可以把PST pst=&student;放到ST student;的下面!!!试试

有的编译器可以通过,我用的是VC++6.0不能,就是要把声明放在前面
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-19 21:26:02 | 显示全部楼层
wangjie5540 发表于 2013-8-19 19:10
你是用的什么编译器编译的?
我记得有的编译器比较严格,如果你是用的严格的,那么可能是PST pst=&student ...

嗯嗯,是这样的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-5 08:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表