Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在单独的文件中创建新函数并使用头文件_C - Fatal编程技术网

C 在单独的文件中创建新函数并使用头文件

C 在单独的文件中创建新函数并使用头文件,c,C,我的下一步是在主代码之外、spate文件中和使用头文件将程序分解为几个函数 我移动的第一个函数是对大写函数的更改,这非常有效 我现在尝试创建一个新函数,将摩尔斯电码转换为字母数字。似乎有很多错误 我已经附上了我的主程序,后面是函数程序,后面是头文件 主程序 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #include <ctyp

我的下一步是在主代码之外、spate文件中和使用头文件将程序分解为几个函数

我移动的第一个函数是对大写函数的更改,这非常有效

我现在尝试创建一个新函数,将摩尔斯电码转换为字母数字。似乎有很多错误

我已经附上了我的主程序,后面是函数程序,后面是头文件

主程序

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>

#include "morse.h"

int main(void)
{

char *morse[] = {"/",".-","-...","-.-.","-..",".","..-.","--        .","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-        .","...","-","..-","...-",".--","-..-","-.--","--..","-----",".----        ","..---","...--","....-",".....","-....","--...","---..","----."};
char *alpha[] =  {" ", "A", "B",    
    "C",   "D",  "E","F",   "G",  "H",   "I", "J",   "K",  "L",   "M", "N",         "O",  "P",   "Q",   "R",  "S",  "T","U",  "V",   "W",  "X",   "Y",   "Z"        ,   "0",    "1",    "2",    "3",    "4",    "5",    "6",    "7",    "8",         "9"};


  char *print_array[50];
  int print_array_index = 0;

  char hold[50];
  int hold_index = 0;

  char input[200];
  int i = 0;

//Requesting input from the user
printf("Welcome to the Morse translator.\n");
    printf("Enter input: ");
    fgets(input, sizeof(input), stdin);

// Call to a function that converts user input to UPPERCASE
turnUpCase(input);

    if (input[0]=='-' || input[0]=='.')
    {
    //Calls the function to convert Morse code to Alpha
    morseToAlpha(input, *alpha, *morse);
}
    // this section is now in a seperate function
/*      while (input[i] !='\0') {

        if (input[i] ==' ' || input[i] == '\n')
        {
            hold[hold_index] = '\0';

            bool found = false;

            for (int x = 0; x < sizeof(morse) / sizeof(char *); x++)
            {
                if (strcmp(morse[x], hold) == 0)
                {
                    print_array[print_array_index++] = alpha[x];

                    found = true;

                    break;
                }
            }

            if (!found)
            {
                fprintf(stderr, "Invalid Morse code!");
                return 0;
            }

            hold_index = 0;
        }
        else
        {
            hold[hold_index++] = input[i];
        }

        i++;
    }
    for (int x = 0; print_array_index > x; x++)
    //for (int x = 0; x < print_array_index; x++)
    {
        printf("%s", print_array[x]);

    }

    printf("\n");
}

*/

else if (isalnum(input[0]))
{
    while (input[i]!='\0' && input[i] !='\n')
    {
        bool found = false;

        for (int x=0; x < sizeof(alpha)/sizeof(char*);x++)
        {
            if (*alpha[x]==input[i])
            {
                print_array[print_array_index++] =
   morse [x];


                found = true;
                break;
            }
        }
        if (!found)
        {
            fprintf(stderr, "Invalid input!\n");
            return 0;
        }
        i++;

    }
    printf("%s",print_array[0]);
    for (int x=1; x < print_array_index; x++)
    {
        printf(" %s",print_array[x]);
    }
    printf("\n");
}           
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括“morse.h”
内部主(空)
{
“,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
字符*alpha[]={”、“A”、“B”,
“C”、“D”、“E”、“F”、“G”、“H”、“I”、“J”、“K”、“L”、“M”、“N”、“O”、“P”、“Q”、“R”、“S”、“T”、“U”、“V”、“W”、“X”、“Y”、“Z”、“0”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”};
字符*打印_数组[50];
int print_array_index=0;
charhold[50];
int hold_索引=0;
字符输入[200];
int i=0;
//请求用户输入
printf(“欢迎使用莫尔斯翻译程序。\n”);
printf(“输入:”);
fgets(输入,sizeof(输入),标准输入);
//调用将用户输入转换为大写的函数
翻盖(输入);
如果(输入[0]='-'| |输入[0]='.)
{
//调用函数将摩尔斯电码转换为阿尔法
莫尔斯-阿尔法(输入,*阿尔法,*莫尔斯);
}
//此部分现在处于单独的功能中
/*while(输入[i]!='\0'){
如果(输入[i]=''| |输入[i]='\n')
{
保持[保持索引]='\0';
bool-found=false;
对于(int x=0;xx;x++)
//对于(int x=0;x
我的函数会导致错误

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


void morseToAlpha(char *in, char *letters, char *code)
{
int i=0;
int x=0;

char *print_array[50];
int print_array_index =0;

char hold[50];
int hold_index = 0;



while (in[i] !='\0') 
{

        if (in[i] ==' ' || in[i] == '\n')
        {
            hold[hold_index] = '\0';

            bool found = false;

            for (int x = 0; x < sizeof(code) / sizeof(char *); x++)
            {
                if (strcmp(*code[x], hold) == 0)
                {
                    print_array[print_array_index++] = letters[x];

                    found = true;

                    break;
                }
            }

            if (!found)
            {
                fprintf(stderr, "Invalid Morse code!");
                return 0;
            }

            hold_index = 0;
        }
        else
        {
            hold[hold_index++] = in[i];
        }

        i++;
    }
    for (int x = 0; print_array_index > x; x++)
    //for (int x = 0; x < print_array_index; x++)
    {
        printf("%s", print_array[x]);

    }

    printf("+++++\n");
}
#包括
#包括
#包括
#包括
#包括“morse.h”
void morseToAlpha(字符*输入,字符*字母,字符*代码)
{
int i=0;
int x=0;
字符*打印_数组[50];
int print_array_index=0;
charhold[50];
int hold_索引=0;
while(在[i]!='\0'中)
{
如果(在[i]=''| |在[i]='\n'中)
{
保持[保持索引]='\0';
bool-found=false;
对于(int x=0;xx;x++)
//对于(int x=0;x
当前编译错误

morseToAlpha.c:在函数“morseToAlpha”中: morseToAlpha.c:32:29:错误:一元“*”的类型参数无效(具有“int”)

如果(strcmp(*代码[x],保持)=0) ^ c:34:55:警告:赋值从整数生成指针,而不进行强制转换[-Wint转换] print_array[print_array_index++]=字母[x]; ^ morseToAlpha.c:45:13:警告:函数返回void时,返回值为“return” 返回0;
^我不确定你的问题是什么,但我就是这么想的

使用单独的文件分割您的工作,并将所需的不同文件放入头文件中

如果项目很小,您可以将所有内容放在同一个目录中,如果项目较大,最好将其拆分为多个目录,并使用Makefile来编译您的项目

srcs/file1.c
srcs/fileX.c
incs/header.h
Makefile
问题的标题是:在单独的文件中创建新函数并使用头文件

所以在这种情况下。我们将把你的主要功能放在
main.c
文件中,把morse-to-alpha原型放在
morse.c
文件中,把morse-to-alpha原型放在头文件
morse.h

并使用以下内容编译您的项目:

gcc -Wall -Wextra -Werror main.c morse.c morse.h

但您应该看看您在这里创建的内容:

char *morse[]....;
char *alpha[]....;
不是指向数组的指针。它被解读为

(char *)(morse[])..;
(char *)(alpha[])..;
*morse
是指向
字符的指针

在strcmp(*code[x],hold)功能中

code
是指向
char
的指针

code
int main() {
    char c = 'h';
    printf("%c\n", *c);
}
char *morse[] = {"/", ...};
char *alpha[] = {" ", ...};
morseToAlpha(input, alpha, morse);
void morseToAlpha(char *in, char **letters, char **code);