gpt4 book ai didi

c - 只有标题中的第一个函数有效,但最后两个函数在 c 中不起作用

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

我为一些程序功能制作了这个标题:

#ifndef STRING_H_INCLUDED
#define STRING_H_INCLUDED

#include <stdio.h>

int s_length(char *test_string){ //returns string length
int i=0;
while(*test_string){
i++;
test_string++;
}
return i;
};

void s_insert(char *string_one){ //inserts string
scanf("%s",string_one);
};

void s_output(char *string_one){ //outputs string
printf("%s",string_one);
};

#endif

我调用了 c 文件中的函数,每次调用 2 次。但是他们中的最后两个得到了这个:warning: implicit declaration of function ‘s_insert’ and undefined reference to 's_insert'。对于这两个函数。

这是什么意思,我做错了什么?

这可能与我在其中调用函数的主 c 文件有关。

主程序:

#include <stdio.h>
#include <stdlib.h>
#include "string.h"

char *name,*surname;


void menu(){
int choise;
do{
printf("1: incerici dati\n");
printf("2: output dati\n");
printf("3: calcola lungezza\n");
printf("0: ecsi\n");
printf("incerici: ");
scanf("%d", &choise);
printf("------------------\n");
switch(choise){
case 1:
printf("String 1:");
s_insert(name);
printf("String 2:");
s_insert(surname);
printf("------------------\n");
break;
case 2:
s_output(name);
s_output(surname);
printf("------------------\n");
break;
case 3:
printf("string 1: %s lungezza: %d \n",name,s_length(name));
printf("string 2: %s lungezza: %d \n",surname,s_length(surname));
printf("------------------\n");
break;
case 0:
printf("prgram closed!!\n");
break;
default:
printf("Errore: %d schelta invalida\n",choise);
break;
}
}while(choise);
};

int main(int argc, char *argv[]){
name=malloc(sizeof(char)*20);
surname=malloc(sizeof(char)*30);
menu();
return 0;
}

ps fot the people wondering,打印的文本是意大利语,因为这是一项学校练习。

最佳答案

我把你的代码编译成.c文件,编译通过。我希望得到错误

string.h : 没有那个文件或目录

但是,string.h也是编译器给我的标准C库的名字

[警告] 函数“s_insert”的隐式声明 [-Wimplicit-function-declaration]

希望上面的解释能给你一点线索。

最有可能:

  1. 您的 header 和源代码不在同一个文件夹中。

  1. 您的包含路径未设置为正确的位置

3.除了string.h

之外,你还有其他的 header 名称

注意:将您的 header 代码复制到我创建的 string.h 中效果很好。

enter image description here

关于c - 只有标题中的第一个函数有效,但最后两个函数在 c 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28581204/

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