gpt4 book ai didi

c - 声明另一个函数时使用函数名和昵称(由 typedef 制成)有什么区别?

转载 作者:太空宇宙 更新时间:2023-11-04 06:10:12 25 4
gpt4 key购买 nike

当我使用 typedef 中的名称时,出现了很多错误。但是在我将名称修改为原始名称后,它就可以工作了。我想知道它们之间有什么不同。

void SwapPoint(Point *pos1, Point *pos2);
//errors

void SwapPoint(struct point *pos1, struct point *pos2);
//it works


void SwapPoint(Point *pos1, Point *pos2);
typedef struct point
{
int xpos;
int ypos;
}Point;

void SwapPoint(Point *pos1, Point *pos2)
{
Point temp;
temp = *pos1;
*pos1 = *pos2;
*pos2 = temp;
}


there's no ')' in front of '*'
there's no '{' in front of '*'
there's no ';' in front of '*'

最佳答案

两个函数声明:

void SwapPoint(Point *pos1, Point *pos2);
//errors

void SwapPoint(struct point *pos1, struct point *pos2);
//it works

即使您为第二个函数声明写了“it works”,在 C 中也是无效的。请注意 visual-studio 编译器存在许多错误,并且其自身的语言扩展不满足 C 标准。

第一个声明无效,因为尚未定义名称 Point

第二个函数声明无效,因为 struct point 类型在函数声明之外是不可见的,并且不表示在函数声明下方定义的类型。那就是参数类型和这个结构体声明的不一样

typedef struct point
{
int xpos;
int ypos;
}Point;

来自 C 标准(6.2.1 标识符的范围)

  1. ... If the declarator or type specifier that declares the identifier appears within the list of parameter declarations in a function prototype (not part of a function definition), the identifier has function prototype scope, which terminates at the end of the function declarator.

因此将 typedef 放在两个函数声明之前。

关于c - 声明另一个函数时使用函数名和昵称(由 typedef 制成)有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58606746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com