我目前在将字符数组从我的主函数传递到某个计算数组中元素数量的函数时遇到问题。
我使用getchar()函数要求2个单独的字符串字符。
为澄清起见,这是我的代码段:
我已经尝试过将scanf用于&myArray [0]作为替代<-可以吗?假设用户输入了5个字符,程序是否会为每个后续字符自动将&myArray [++]递增?
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_LEN 20
int match(char s[], char t[] )
{
int i = 0;
int j = 0;
printf("begin: ivaL: %d, jval: %d\n", i, j);
while(s[i] != '\0')
i++;
while(t[j] != '\0')
j++;
printf("i val: %d, jval: %d ", i, j); /*J value is messed up, it prints 20+ when ive only typed 5 characters or so*/
}
int main()
{
int cur = 0;
char char1[ARRAY_LEN];
char char2[ARRAY_LEN];
char c;
char f;
printf("Enter char: ");
while((c=getchar())!=EOF && c!='\n')
{
char1[cur++] = c;
}
cur = 0;
printf("\n2nd Char: ");
while((f=getchar())!=EOF && f!='\n')
{
char2[cur++] = f;
putchar(f);
}
putchar('\n')
printf("Cur value: %d\n", cur); //here cur accurately prints the correct number of while loops done when counting the characters
match(char1, char2); /*here i tried to fetch the number of elements in char2 before i passed it to match function and in here char2 is showing correct value, something happens when i pass char2 to the match function*/
}
答案 0 :(得分:1)
您有语法错误需要解决:
putchar('\n') // Missing semicolon.
在读取一组字符后,默认情况下不添加空字符。
while((c=getchar())!='\n')
{
char1[cur++] = c;
}
char1[cur] = '\0'; // Adding a null terminator to make the identifier a C Style string.
第二次类似。
除此之外,您还有其他问题。
int match(char s[], char t[] )
应返回一个整数。您可能会考虑做类似的事情:
return i==j;
捕获所有编译器警告(例如在gcc中使用-Wall
)
我可以像下面这样重写这段代码::
#include <stdio.h>
#define ARRAY_LEN 30
int match(char * restrict str1,char * restrict str2)
// If str1 and str2 is is the sole agencies dealing with the strings
// then 'restrict' helps compiler with some optimizations.
{
size_t count1=0,count2=0;
while(str1[count1++] != '\0')
;;
while(str2[count2++] != '\0')
;;
return count1==count2;
// Ideally count1-1 == count2 -1
// But does that make any difference?
}
int main(int argc,char * argv[])
{
char str1[ARRAY_LEN];
char str2[ARRAY_LEN]; // No harm doing so.
signed x; // If you want to check against EOF
int count=0;
while((x=getchar()) != '\n' && x!=EOF )
// You need to implement bounds check.
{
if(count < (ARRAY_LEN - 1))
{
str1[count++]=x;
}
else
{
// C input is buffered
// so you need to clear the buffer if the string first entered was larger than 30 characters
while((x=getchar()) != '\n' && x!=EOF )
;;
break;
}
}
// C input is buffered
// so you need to clear the buffer if the string first entered was larger than 30 characters
str1[count] = '\0' ; // Null terminating
count = 0; // Reset count
while((x=getchar()) != '\n' && x!=EOF )
// You need to implement bounds check.
{
if(count < (ARRAY_LEN - 1))
{
str2[count++]=x;
}
else
{
// C input is buffered
// so you need to clear the buffer if the string first entered was larger than 30 characters
while((x=getchar()) != '\n' && x!=EOF )
;;
break;
}
}
str2[count] = '\0' ; // Null terminating
printf("Arrays are of %s length\n",match(str1,str2)?"same":"different");
return 0;
}
编辑::EOF
宏定义为-1
。为了容纳该x
,必须为有符号整数。内联阅读此答案以及this答案。
答案 1 :(得分:1)
您的match()
函数在您的char
数组中进行迭代,直到找到空终止符为止,但是您实际上从未将空终止符放在数组中的任何位置。
printf("Enter char: ");
while((c=getchar()) !='\n'){
char1[cur++] = c;
}
char1[cur] = '\0';
cur = 0;
printf("\n2nd Char: ");
while((f=getchar()) !='\n'){
char2[cur++] = f;
}
char2[cur] = '\0';