字符数组传递给函数
Character arrays passing into functions
我目前在将字符数组从我的主函数传递到某个计算数组中元素数量的函数时遇到问题。
我使用 getchar() 函数请求 2 个单独的字符串字符。
为了澄清,这是我的代码片段:
我试过使用 scanf to &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] != '[=10=]')
i++;
while(t[j] != '[=10=]')
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*/
}
您有语法错误需要解决:
putchar('\n') // Missing semicolon.
读取一组字符后默认不添加空字符。
while((c=getchar())!='\n')
{
char1[cur++] = c;
}
char1[cur] = '[=11=]'; // Adding a null terminator to make the identifier a C Style string.
第二个也是如此。
除此之外,您还有其他问题。
int match(char s[], char t[] )
应该return一个整数。您可能会考虑做类似的事情:
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++] != '[=14=]')
;;
while(str2[count2++] != '[=14=]')
;;
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] = '[=14=]' ; // 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] = '[=14=]' ; // Null terminating
printf("Arrays are of %s length\n",match(str1,str2)?"same":"different");
return 0;
}
编辑: EOF
宏被定义为 -1
。为了适应 x
需要是一个带符号的整数。阅读此答案内嵌 this 答案。
您的 match()
函数遍历您的 char
数组,直到它找到一个空终止符,但您实际上从未在数组中的任何地方放置一个空终止符。
printf("Enter char: ");
while((c=getchar()) !='\n'){
char1[cur++] = c;
}
char1[cur] = '[=10=]';
cur = 0;
printf("\n2nd Char: ");
while((f=getchar()) !='\n'){
char2[cur++] = f;
}
char2[cur] = '[=10=]';
我目前在将字符数组从我的主函数传递到某个计算数组中元素数量的函数时遇到问题。
我使用 getchar() 函数请求 2 个单独的字符串字符。
为了澄清,这是我的代码片段:
我试过使用 scanf to &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] != '[=10=]')
i++;
while(t[j] != '[=10=]')
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*/
}
您有语法错误需要解决:
putchar('\n') // Missing semicolon.
读取一组字符后默认不添加空字符。
while((c=getchar())!='\n')
{
char1[cur++] = c;
}
char1[cur] = '[=11=]'; // Adding a null terminator to make the identifier a C Style string.
第二个也是如此。
除此之外,您还有其他问题。
int match(char s[], char t[] )
应该return一个整数。您可能会考虑做类似的事情:
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++] != '[=14=]')
;;
while(str2[count2++] != '[=14=]')
;;
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] = '[=14=]' ; // 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] = '[=14=]' ; // Null terminating
printf("Arrays are of %s length\n",match(str1,str2)?"same":"different");
return 0;
}
编辑: EOF
宏被定义为 -1
。为了适应 x
需要是一个带符号的整数。阅读此答案内嵌 this 答案。
您的 match()
函数遍历您的 char
数组,直到它找到一个空终止符,但您实际上从未在数组中的任何地方放置一个空终止符。
printf("Enter char: ");
while((c=getchar()) !='\n'){
char1[cur++] = c;
}
char1[cur] = '[=10=]';
cur = 0;
printf("\n2nd Char: ");
while((f=getchar()) !='\n'){
char2[cur++] = f;
}
char2[cur] = '[=10=]';