有人能解释一下为什么这个C程序编译时没有错误吗



嘿,我是一名学习C编程的学生,只想知道为什么这个程序在struct date *newdate, foo();行编译为foo,并声明为返回类型为struct-date的主函数的本地函数。由于foo已经被声明为一个函数,它应该给出一个类型冲突的错误,因为c不支持函数重载。有人能帮帮我吗?

#include <stdio.h>
#include <stdlib.h>
struct date {
int month;
int day;
int year;
};
struct date foo(struct date x) {
++x.day;
return x;
};
int main() {
struct date today = {10, 11, 2014};
int array[5] = {1, 2, 3, 4, 5};
struct date *newdate, foo();
char *string = "test string";
int i = 3;
newdate = (struct date *)malloc(sizeof(struct date));
newdate->month = 11;
newdate->day = 15;
newdate->year = 2014;
today = foo(today);
free(newdate);
return 0;
} 

foo被声明为主函数的本地函数

不正确。它声明但没有定义函数foo存在,并且它接受未知数量的参数并返回struct date

此声明与foo的实际定义兼容,因为返回类型匹配,并且声明不包含有关参数的声明。

最新更新