c - 学习C、段错误

标签 c

我学完 Java 后刚刚学习 C,但我过得很艰难...... 该程序应该是一个简单的命令提示符程序,它接受“sum 1 2”之类的命令并添加输出“3”。该程序将输入按空格标记为二维数组。因此第一个元素将具有命令,后面的整数将用于算术。一旦我输入“sum 1 2”,我就会收到段错误错误并且程序崩溃。

我将二维数组中的每一行设置为“NULL”,以便我知道何时停止迭代行。我到处寻找,我想知道这是否是错误的做法,以及是否有更有效的方法。

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

#define tLength 20
#define tRow 7
char input[tLength];
char tokens[tRow][tLength];
char p[tLength];
char y[tLength];
int counter = 0;

void getTokens(char inp[]) {
    strcpy(y, inp);
        strcpy(p, strtok(y," "));
    strcpy(tokens[counter], p);
        counter = 1;
    while (p!=NULL){
            strcpy(p,strtok(NULL, " "));
        if (counter < tRow) {
            strcpy(tokens[counter], p);
                        counter++;
        }
        else {
            printf("Cannot process more lines");
                        counter = 0;
                        break;
        }
    }
        counter = 0;
}

void commandLine() {
    int count;
    for (count=0;count<tRow;count++){
        strcpy(tokens[count],"NULL");
    }
    printf(">");
    fgets(input, tLength, stdin);
    getTokens(input);

    if (strcmp("quit", tokens[0]) == 0 || strcmp("Quit", tokens[0]) == 0) {
        printf("Bye!");
    }

    else if (strcmp("sum", tokens[0]) == 0 || strcmp("Sum", tokens[0]) == 0) {
        int sum = 0;
        counter = 1;
        while (strcmp("NULL",tokens[counter])!=0) {
            sum += atoi(tokens[counter]);
            counter++;
        }
        counter = 0;
        printf("%d", sum);
    }

    else if (strcmp("prod", tokens[0]) == 0 || strcmp("Prod", tokens[0]) == 0) {
        int temp = 0;
        int prod = 1;
        counter = 1;
        if (atoi(tokens[1]) == 0) {
            printf("%d", 0);
        }
        else {
            while (strcmp("NULL",tokens[counter])!=0) {
                prod *= atoi(tokens[counter]);
                counter++;
            }
        }
        printf("%d", prod);
    }

    else {
        printf("Error, unknown command");
    }
}

void main(void) {
    commandLine();
}

最佳答案

几个提示:

  1. 不必要的全局变量是不好的
  2. 宏使用大写字母
  3. 初始化变量比未初始化更好
  4. 使用描述性变量名称(not、p、y 等)
  5. 空字符串不能与“NULL”进行比较
  6. POSIX 提供了 strcasecmp,它是 strcmp 的不区分大小写的版本
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXINPUTLEN 20
#define MAXTOKENS 7

void getTokens( char* input, char tokenized[][MAXINPUTLEN] )
{
    int counter = 0;
    char* token = strtok( input, " " );

    while( token != NULL )
    {
        if( counter < MAXTOKENS )
        {
            strncpy( tokenized[counter], token, MAXINPUTLEN );
            counter++;
        }
        else
        {
            printf( "Cannot process more lines" );
            counter = 0;
            break;
        }

        token = strtok( NULL, " " );
    }

    counter = 0;
}

void commandLine()
{
    char input[MAXINPUTLEN] = {0};
    printf( ">" );
    fgets( input, MAXINPUTLEN, stdin );
    char tokens[MAXTOKENS][MAXINPUTLEN] = {{0}};
    getTokens( input, tokens );

    if( strcasecmp( "quit", tokens[0] ) == 0 )
    {
        printf( "Bye!" );
    }
    else if( strcasecmp( "sum", tokens[0] ) == 0 )
    {
        int sum = 0;

        for( int i = 1; tokens[i][0] != 0; ++i )
        {
            sum += atoi( tokens[i] );
        }

        printf( "%d", sum );
    }
    else if( strcasecmp( "prod", tokens[0] ) == 0 )
    {
        int prod = 1;

        for( int i = 1; tokens[i][0] != 0; ++i )
        {
            prod *= atoi( tokens[i] );
        }

        printf( "%d", prod );
    }
    else
    {
        printf( "Error, unknown command" );
    }
}

int main( void )
{
    commandLine();

    return 0;
}

关于c - 学习C、段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35695358/

相关文章:

c - 当我执行此代码时,我遇到段错误(核心转储)...

c++ - 如何避免坏的 FD_SET 缓冲区溢出崩溃?

c - C 中的字符串队列

c - "Bring to front"OS-X 上的 GTK/C 应用程序

c - GET 请求总是返回一个横幅,就像 HEAD 请求一样

c - C 中的写/读文件性能问题

C 编程 - 将空格分隔的字符串读入 BST

c - 在Linux中,pthread_exit()和pthread_cancel()如何调用清理例程?

c - 打印多维数组仅打印第一列

c - IAR 汇编器 BKPT 立即数作为输入操作数