【问题标题】:Creating a new function in a seperate file and using a header file在单独的文件中创建新函数并使用头文件
【发布时间】:2016-09-26 08:34:00
【问题描述】:

我的下一步是将我的程序分解为主代码之外的几个函数,在 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;
}

我的功能导致错误

#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");
}

当前编译错误

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

if (strcmp(*code[x], hold) == 0) ^ morseToAlpha.c:34:55:警告:赋值使指针从整数而不进行强制转换 [-Wint-conversion] print_array[print_array_index++] = 字母[x]; ^ morseToAlpha.c:45:13:警告:带有值的“返回”,在函数中返回 void 返回0; ^

【问题讨论】:

  • 添加您遇到的错误。
  • 您定义了morseToAlpha() 以将char*s 作为第二和第三个参数,但您传递了char**。此错误完全与您分发源代码的文件数量无关。
  • 建议两个数组是错误的数据结构(很难检查),数组sstruct { char letter;字符 * 莫尔斯; } 会更好

标签: c


【解决方案1】:

您在此处创建的内容:

char *morse[]....;
char *alpha[]....;

不是指向数组的指针。读作,

(char *)(morse[])..;
(char *)(alpha[])..;

*morse 是指向char 的指针。

在函数strcmp(*code[x], hold)中,

code 是指向char 的指针。

code[x]char

但你在做*code[x]

产生同样错误的例子是,

int main() {
    char c = 'h';
    printf("%c\n", *c);
}

【讨论】:

    【解决方案2】:

    我不确定你的问题是什么,但这是我的想法。

    使用单独的文件拆分您的工作,并将所需的不同function prototype 放在头文件中。

    如果项目小,你可以把所有东西放在同一个目录下,如果项目大,最好分成多个目录,使用 Makefile 编译你的项目。

    srcs/file1.c
    srcs/fileX.c
    incs/header.h
    Makefile
    

    问题标题是:在单独的文件中创建新函数并使用头文件

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

    然后编译你的项目:

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

    但是你应该看看how to build a Makefile

    【讨论】:

      【解决方案3】:

      代码:

      char *morse[] = {"/", ...};
      char *alpha[] = {" ", ...};
      

      首先将morsealpha 声明为指向char 的指针数组,然后初始化这些数组。现在,像"/" 这样的字符串文字实际上是一个字符数组:"/" = {'/', '\0'}。因此,数组morse(将填充指向char 的指针)被初始化为包含指向char 的指针(即指向字符文字'/' 的指针)作为其第一个元素。

      当你想用一个函数处理一个数组时,你用数组的名字调用这个函数,并将一个指向数组第一个元素的指针传递给函数。所以你的函数调用应该是这样的:

      morseToAlpha(input, alpha, morse);
      

      这会传递一个指向 input[0] 的指针,它是一个指向 char 的指针,一个指向 alpha[0] 的指针,它是一个指向 char 的指针(即指向字符串的指针),以及一个指针到 morse[0],它也是一个指向 char 的指针。因此,您的函数应声明为:

      void morseToAlpha(char *in, char **letters, char **code);
      

      我希望这会有所帮助:)

      【讨论】:

        猜你喜欢
        • 2019-01-01
        • 2017-12-04
        • 2019-04-07
        • 1970-01-01
        • 2016-04-06
        • 1970-01-01
        • 2011-06-30
        • 1970-01-01
        • 2019-02-04
        相关资源
        最近更新 更多