#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/