警告:函数的隐式声明-为什么我的代码仍然可以工作?
我经历了以下线程:
可能我的问题已链接.但是,尽管他们提供了在使用函数之前声明函数原型的解决方案,但我想探索一下函数名称不匹配时会发生什么.在我的测试中,它仍然可以正常工作.
Possibly my issue is linked. But while they offer the solution that the function prototype should be declared before the function is used, I wanted to explore what happens when the function name is not matching. In my test, it still works fine.
主C文件
#include "node.h"
int main(){
nd *head=NULL;
nd *tail=NULL;
create_node(&head, &tail, 10);
create_node(&head, &tail, 20);
create_node(&head, &tail, 15);
create_node(&head, &tail, 35);
create_node(&head, &tail, 5);
create_node(&head, &tail, 25);
print_list(head, tail);
create_node(&head, &tail, 55);
create_node(&head, &tail, 52);
create_node(&head, &tail, 125);
printf("%d\n",tail->data);
printf("%d\n",head->data);
print_list(head, tail);
return 0;
}
node.h
文件
node.h
file
#ifndef NODE_H
#define NODE_H
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
struct node *prev;
}nd;
void insert_node(nd **head, nd **tail, int data);
void print_list(nd *head, nd *tail);
#endif
node.c
文件
node.c
file
#include "node.h"
void create_node(nd **head, nd **tail, int d){
nd *temp=(nd *) malloc(sizeof(nd));
temp->data=d;
temp->next=NULL;
temp->prev=NULL;
/* Start of the Queue. */
if(*head==NULL && *tail==NULL){
*head=temp;
*tail=temp;
}
/* Linking with tail of the Queue. */
else if((*tail)->next==NULL){
(*tail)->next=temp;
temp->prev=*tail;
*head=temp;
}
/* Adding remaining elements of the Queue. */
else{
(*head)->next=temp;
temp->prev=*head;
*head=temp;
}
}
void print_list(nd *head, nd *tail){
if(NULL==head){
printf("Queue is empty\n");
}
else{
printf("Printing the list\n");
nd *temp;
for(temp=tail;temp!=NULL;temp=temp->next){
printf("%d ",temp->data);
}
printf("\n");
}
}
输出
Printing the list
10 20 15 35 5 25
10
125
Printing the list
10 20 15 35 5 25 55 52 125
在 node.h
中声明的函数的名称为 insert_node
,而在node.c中则为 create_node
.有人可以分享一些关于它为什么运行的见解吗?但是它会发出警告:
The name of the function declared in the node.h
is insert_node
whereas in node.c it is create_node
. Can someone share some insight on why is it running? It throws a warning though:
警告:函数的隐式声明
Warning: implicit declaration of function
首先,您已经声明了一个名为 insert_node
的函数,但这并不重要.可以声明函数,但不定义它们(即不提供其代码)是可以的,只要您不使用该函数即可.这在现实生活中经常发生:标头定义了许多功能,然后在链接时仅需要提供实际使用的功能.
First, you've declared a function called insert_node
, but that doesn't matter. It's ok to declare functions, but not define them (i.e. not provide their code), as long as you don't use the function. This happens often in real life: headers define a lot of functions, and then at link time only the functions that are actually used need to be provided.
警告与 create_node
有关.由于在编译主C文件时没有函数声明,因此编译器会对其参数类型进行一些假设.它会提升所有参数:小于 int
的整数类型(例如 char
和 short
)被提升为 int
; float
被提升为 double
;指针类型不转换.对于您的代码,这恰好起作用,因为
The warning concerns create_node
. Since there is no declaration of the function when you compile the main C file, the compiler makes some assumptions about its parameter types. It promotes all arguments: integer types smaller than int
(e.g. char
and short
) are promoted to int
; float
s are promoted to double
; pointer types are not converted. With your code, this happens to work because
- 您总是在传递正确类型的参数;
- 没有一个参数类型被提升.
如果将 data
参数的类型更改为 long
,则编译器将生成假定类型为 int
的代码来调用函数但是该函数需要一个 long
参数.在 int
和 long
具有不同大小的平台上,您可能会得到垃圾数据,崩溃或其他不良行为.
If you changed the type of the data
parameter to long
, then the compiler would generate code to call the function assuming an int
type but the function would expect a long
argument. On a platform where int
and long
have different sizes, you might get garbage data, a crash, or other misbehavior.
如果将 data
参数的类型更改为 char
,则编译器将生成假定类型为 int
的代码来调用函数但是该函数需要一个 char
参数.同样,您可能会发现代码使用了错误的数据,崩溃等.
If you changed the type of the data
parameter to char
, then the compiler would generate code to call the function assuming an int
type but the function would expect a char
argument. Again, you might find that the code uses wrong data, crashes, etc.
C通常会给您足够的绳索来吊住自己.如果您用错误的方式剪断了一条绳子,那可能恰好起作用.否则可能不会.
C generally gives you enough rope to hang yourself. If you cut a piece of rope in the wrong way, it might just happen to work. Or it might not.