C 通过'的参数1;功能';从不带强制转换的整数生成指针

C 通过'的参数1;功能';从不带强制转换的整数生成指针,c,C,我必须编写一个函数,它要求数组及其大小,然后打印它的值。主函数必须调用另外两个函数,它们都有两个参数(数组及其大小)。第一个请求填充数组的值,第二个请求打印每个值 我是C语言和编程新手,所以欢迎提供任何提示和有用的信息。我对指针也知之甚少 void requestarray(int a[], int size){ int i; for (i=0;i<size;i++) { printf("Insert value for %d position of the arr

我必须编写一个函数,它要求数组及其大小,然后打印它的值。主函数必须调用另外两个函数,它们都有两个参数(数组及其大小)。第一个请求填充数组的值,第二个请求打印每个值

我是C语言和编程新手,所以欢迎提供任何提示和有用的信息。我对指针也知之甚少

void requestarray(int a[], int  size){       
int i;
 for (i=0;i<size;i++)
 {
  printf("Insert value for %d position of the array\n",i);
  scanf("%d\n",&a[i]);
 }
}

void printarray(int a[], int size){     
int i;

 for (i=0;i<size;i++){
 printf("%d\n",a[i]);
 }
}

int main(void){
int size;

  printf("Insert array length\n");
  scanf("%d\n",&size);

int a[size];
requestarray(&a[size],size);
printf("\n");
printf("The array is\n");
printarray(&a[size],size);

return 0;
}
然后我打印a的值,它返回一个带有size长度和随机值(主要是大数字)的数组。然后我在后面放置一个断点

 int a[size];
requestarray(&a[size],size);
(这将修改数组的值),但它返回的值与上一个断点中打印的值相同

如果我像这样更改主函数中的代码

 requestarray(a[size],size);
printf("\n");
printf("The array is\n");
printarray(a[size],size);
调试器给了我这个警告

arreglo1.c: In function ‘main’:
arreglo1.c:33:14: warning: passing argument 1 of ‘requestarray’ makes pointer from integer without a cast [-Wint-conversion]
resquestarray(a[size],size);
              ^
arreglo1.c:8:6: note: expected ‘int *’ but argument is of type ‘int’
 void requestarray(int a[], int  size){
      ^~~~~~~~~~~~
arreglo1.c:36:16: warning: passing argument 1 of ‘imprimearreglo’ makes pointer from integer without a cast [-Wint-conversion]
 printarray(a[size],size);
            ^
arreglo1.c:17:6: note: expected ‘int *’ but argument is of type ‘int’
 void printarray(int a[], int size){
      ^~~~~~~~~~~~   
scanf(“%d\n”和&a[i])
完全错误(请注意,必须按Enter键或提供比预期更多的输入?
scanf
将不会以这种方式处理转义字符。相反,文本
'\n'
会导致
scanf
忽略所有空白,迫使您在返回前输入非空白字符

您键入的任何字符导致
scanf
返回,但与格式字符串中的任何字符都不匹配,导致该字符在
stdin
中处于未读状态,并将用作下一次尝试输入

这就是为什么…,您必须通过检查所使用的每个输入函数的返回值来验证每个输入。例如在
main()
中,例如

    printf ("Insert array length: ");
    if (scanf ("%d", &size) != 1) {     /* validate EVERY input */
        fputs ("error: invalid integer input.\n", stderr);
        exit (EXIT_FAILURE);
    }
同样在
requestarray()
中,例如

    for (i = 0; i < size; i++) {
        printf ("Insert value for %d position of the array: ", i + 1);
        if (scanf ("%d", &a[i]) != 1) {     /* validate EVERY input */
            fputs ("error: invalid integer input.\n", stderr);
            exit (EXIT_FAILURE);    /* handle error condition as desired */
        }
    }
注意传递
int a[]
没有什么错。还要注意,代码的间隔要宽一点,以便更容易阅读(尤其是对于老年人的眼睛而言)

总而言之,你可以做到:

#include <stdio.h>
#include <stdlib.h>     /* for EXIT_FAILURE define */

void requestarray (int *a, int  size)
{       
    int i;

    putchar ('\n');         /* tidy up with newline before looping for input */

    for (i = 0; i < size; i++) {
        printf ("Insert value for %d position of the array: ", i + 1);
        if (scanf ("%d", &a[i]) != 1) {     /* validate EVERY input */
            fputs ("error: invalid integer input.\n", stderr);
            exit (EXIT_FAILURE);    /* handle error condition as desired */
        }
    }
}

void printarray (int *a, int size)
{     
    int i;

    for (i = 0; i < size; i++){
        printf ("%d\n", a[i]);
    }
}

int main(void) {

    int size;

    printf ("Insert array length: ");
    if (scanf ("%d", &size) != 1) {     /* validate EVERY input */
        fputs ("error: invalid integer input.\n", stderr);
        exit (EXIT_FAILURE);
    }

    int a[size];            /* Variable Length Array of 'size' elements */

    requestarray (a, size);

    puts ("\nThe array is\n");  /* there is no convertion, puts will do */
    printarray (a ,size);

    return 0;
}
请仔细检查,如果您还有其他问题,请告诉我。

scanf(“%d\n”、&a[i])
完全错误(请注意,必须按Enter键或提供比预期更多的输入?
scanf
将不会以这种方式处理转义字符。相反,文本
'\n'
会导致
scanf
忽略所有空白,迫使您在返回前输入非空白字符

您键入的任何字符导致
scanf
返回,但与格式字符串中的任何字符都不匹配,导致该字符在
stdin
中处于未读状态,并将用作下一次尝试输入

这就是为什么…,您必须通过检查所使用的每个输入函数的返回值来验证每个输入。例如在
main()
中,例如

    printf ("Insert array length: ");
    if (scanf ("%d", &size) != 1) {     /* validate EVERY input */
        fputs ("error: invalid integer input.\n", stderr);
        exit (EXIT_FAILURE);
    }
同样在
requestarray()
中,例如

    for (i = 0; i < size; i++) {
        printf ("Insert value for %d position of the array: ", i + 1);
        if (scanf ("%d", &a[i]) != 1) {     /* validate EVERY input */
            fputs ("error: invalid integer input.\n", stderr);
            exit (EXIT_FAILURE);    /* handle error condition as desired */
        }
    }
注意传递
int a[]
没有什么错。还要注意,代码的间隔要宽一点,以便更容易阅读(尤其是对于老年人的眼睛而言)

总而言之,你可以做到:

#include <stdio.h>
#include <stdlib.h>     /* for EXIT_FAILURE define */

void requestarray (int *a, int  size)
{       
    int i;

    putchar ('\n');         /* tidy up with newline before looping for input */

    for (i = 0; i < size; i++) {
        printf ("Insert value for %d position of the array: ", i + 1);
        if (scanf ("%d", &a[i]) != 1) {     /* validate EVERY input */
            fputs ("error: invalid integer input.\n", stderr);
            exit (EXIT_FAILURE);    /* handle error condition as desired */
        }
    }
}

void printarray (int *a, int size)
{     
    int i;

    for (i = 0; i < size; i++){
        printf ("%d\n", a[i]);
    }
}

int main(void) {

    int size;

    printf ("Insert array length: ");
    if (scanf ("%d", &size) != 1) {     /* validate EVERY input */
        fputs ("error: invalid integer input.\n", stderr);
        exit (EXIT_FAILURE);
    }

    int a[size];            /* Variable Length Array of 'size' elements */

    requestarray (a, size);

    puts ("\nThe array is\n");  /* there is no convertion, puts will do */
    printarray (a ,size);

    return 0;
}

仔细检查一下,如果您还有其他问题,请告诉我。

只需将
a
作为参数传递即可。顺便说一句,您在标题中引用的错误消息并非来自该代码。请参阅。您的代码基本正确,
scanf(“%d\n”和&a[i])中的
'\n'
是错误的,只是
scanf(“%d”,&a[i])就可以了。(除
“%c”
“[…]”
之外的所有转换说明符都使用前导空格)也
请求数组(a,size)是所有需要的。而
void requestarray(int*a,int size)
将显示对发生的数组/指针转换的理解。请参阅-这是令人震惊的UI/UX的缩影,让人们在完成当前预期输入之前预测下一步需要输入什么。只需将
a
作为参数传递即可。顺便说一句,您在标题中引用的错误消息并非来自该代码。请参阅。您的代码基本正确,
scanf(“%d\n”和&a[i])中的
'\n'
是错误的,只是
scanf(“%d”,&a[i])就可以了。(除
“%c”
“[…]”
之外的所有转换说明符都使用前导空格)也
请求数组(a,size)是所有需要的。和
void请求数组(int*a,int size)
将显示对发生的数组/指针转换的理解。请参阅-这是骇人听闻的UI/UX的缩影,让人们在完成当前预期输入之前预测下一步需要输入什么。
scanf
不会处理转义字符,因为编译器在扫描时会处理转义字符字符串文字。它成功了,谢谢!scanf中的\n是我在执行这些程序和其他使用它的程序时注意到的一个反复出现的问题。我不太明白为什么需要对每个输入进行验证。@AgustinOsiecki验证是必要的,因为如果用户在读取
'5'
时不小心滑倒并键入
'r'
,则会出现匹配失败,
scanf
退出从
stdin
提取字符,在
stdin
中保留
'r'
未读,下次用
%d
调用
scanf
读取下一个
int
时,
'r'
仍然存在,导致(以及每次后续尝试)失败。如果你尝试在一个循环中读取而没有验证每一次读取,那么这个“失误”会让你陷入一个无限循环——这不好<代码>:)
开头的段落不准确。否,此
scanf
中不会因为该
\n
而出现匹配失败。
scanf
格式字符串中的空白字符不是在输入中查找该字符的请求。所有空白字符的处理方式都不同。任何空白字符都是指向scanf
t的命令