c - 我的循环不会检查 C 中数组的所有元素

标签 c arrays loops for-loop

#define _CRT_SECURE_NO_WARNINGS

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

void displayString (const char *sPtr);
void getString (char *[]);
int determinIfConvert (char);

int main ()
{
    char originalString[11] = { 0 };
    char convertedString[11];
    getString (originalString);
    displayString (originalString);

    // this loop runs through the "originalString" to check for the char: 'a'
    for (int i = 0; i < 11; i++) {
        determinIfConvert (originalString[i]);
    }
    system ("pause");
}

void getString (char *a[])  // this function gets a string
{
    printf ("enter 11 char string: \n");
    scanf ("%s", a);
}

// this program displays the inputstring
void displayString (const char *sPtr)
{
    for (; (*sPtr != '\0'); ++sPtr) {
        printf ("%c", *sPtr);
    }
}

int determinIfConvert (char *a)
{
    if (a == 97)            // this is a test condition. The goal is to
                            // check for all lowercase, but now i'm
                            // only entering "aaaaa"
    {
        printf ("Works");   // if it prints multiple"works"
                            // then i can continue my program
                            // but it only prints ONE "works" and freezes.
    }

}

目前我在 main() 中的 For 循环没有完成时遇到问题。目标是输入一串字符,然后检查小写字符。这将通过函数 DeterminIfConvert(char) 完成。但是,当我逐个元素运行循环时,它在第二个元素之后卡住。我的测试数据是“aaaa”,它打印了“aaaa”,所以我知道我的前两个函数工作得很好。我进入循环,它遍历第一个元素,打印“works”然后卡住。 :/

最佳答案

多处错误

void getString(char *a[])

应该是

void getString(char a[])

由于您发送的是 char 数组 的基地址,而不是 char 指针数组

char *a[];  // array of pointer to char
char a[];   // array of char

 int determinIfConvert(char *a)

应该是

int determinIfConvert(char a)

由于您发送的是 char,而不是 char 指针

char * a;    // pointer to char
char a;      // char

注意: 使用 main()

的标准定义
int main(void) //if no command line arguments.

关于c - 我的循环不会检查 C 中数组的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36466705/

相关文章:

python - 在 Python 中,根据该数组特定列中的值将数组行分成组

php - 将 PHP 数组保存到 MySQL?

php - 将相关的 post_content 显示到自定义帖子标题切换类

c - pthread 中的读/写锁是如何实现的?

c - int expecting 发现语法错误; C ICC12 编译器

c - 不使用随机函数生成随机数

java - 制作命令行,如果语句不起作用

c - 读取结构中包含的 char 会导致访问冲突异常

C++ 初始值设定项列表 std::array 最佳性能

java - 与嵌套 for 循环作斗争