Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么编译器显示意外结果?_C_Arrays - Fatal编程技术网

C 为什么编译器显示意外结果?

C 为什么编译器显示意外结果?,c,arrays,C,Arrays,我是编程新手。在目前学习C语言的时候,我被一个问题弄糊涂了,这个问题在运行后会出现意想不到的结果 问题是- 我想制作一个程序,比用户输入42时多输入一次,然后停止输入,并打印用户在42到来之前输入的所有数字 例如-输入: 1 2 88 42 99 输出: 1 2 88 我的节目- #include <stdio.h> int main() { int i = 1, j, temp; printf("enter numbers:\n"); while (

我是编程新手。在目前学习C语言的时候,我被一个问题弄糊涂了,这个问题在运行后会出现意想不到的结果

问题是-

我想制作一个程序,比用户输入
42
时多输入一次,然后停止输入,并打印用户在
42
到来之前输入的所有数字

例如-输入:

1
2
88
42
99
输出:

1
2
88
我的节目-

#include <stdio.h>

int main()
{
    int i = 1, j, temp;

    printf("enter numbers:\n");
    while (i)
    {
        scanf("%d", &temp);
        i++;

        int a[i];

        a[i - 2] = temp;
        if (temp == 42)
        {
            scanf("%d", &a[i - 1]);
            for (j = 0 ; j < i - 1 ; j++)
            {
                if (a[j] == 42)
                    break;
                else 
                    printf("%d\n",a[j]);
            }        
        }
        if (temp == 42)
            break;
    }    
    return 0;
}

请帮忙。对不起,我的英语不好。先谢谢你。

你在做一些不必要的事。 你必须检查一下这个--

在我看来,我已经根据您的代码编辑了您的代码,以存储
42
之前的所有数字

#include<stdio.h>
int main()
{
    int i=1,j,temp;
    int a[10010];
    printf("enter numbers:\n");
    while(i)
    {
        scanf("%d",&temp);
        i++;
        a[i-2]=temp;
        if(temp==42)
        {
            for(j=0; j<i-2; j++)
            {
                if(a[j]==42)
                    break;
                else
                    printf("%d\n",a[j]);
            }

        }
        if (temp == 42)
            break;
    }

    return 0;
}
#包括
int main()
{
int i=1,j,温度;
INTA[10010];
printf(“输入数字:\n”);
而(i)
{
scanf(“%d”、&temp);
i++;
a[i-2]=温度;
如果(温度=42)
{

对于(j=0;j,您正在打印未初始化的值,因为如果
i
的值变为
3
和更大的值,之后您从未设置
a[0]
以及
a[i-2]
之前的所有值

因此,在以下行中导致

printf("%d\n", a[j]);
有些值未初始化,您仍尝试打印它们

您应该注意到,
a
在每次迭代中都被重新声明,并且您没有初始化值,因为上一次
a
中的值仅在上一次迭代中有效

a
变量仅在
while
循环的范围内有效,这不仅是因为它没有在范围外声明,还因为它在超出范围时被解除分配。也许举个例子可以让它更清楚

假设您有以下程序

int main(void)
{
    int *pointer = NULL;
    if (pointer == NULL)
    {
        int array[5] = {1, 2, 3, 4, 5};
        pointer = array;
    }
    /* `poitner' points to `array` but array was deallocated */
    for (int i = 0 ; i < 5 ; ++i)
        printf("%d\n", pointer[i]); /* This is UB */
    return 0;
}
int main(无效)
{
int*指针=NULL;
if(指针==NULL)
{
int数组[5]={1,2,3,4,5};
指针=数组;
}
/*'poitner'指向'array',但数组已解除分配*/
对于(int i=0;i<5;++i)
printf(“%d\n”,指针[i]);/*这是UB*/
返回0;
}
但是,正如您所看到的,这将进行编译,它将调用未定义的行为

由于不可能理解代码的功能,我建议如下

#include <stdio.h>

int main()
{
    int i = 1;
    int a[100];

    printf("enter numbers:\n");
    while (i++ < 100)
    {
        if (scanf("%d%*c", &a[i - 2]) != 1)
            return -1; /* this was invalid input, it will cause UB */
        if (a[i - 2] == 42)
        {
            if (scanf("%d%*c", &a[i - 1]) != 1)
                return -1; /* this was invalid input, it will cause UB */
            for (int j = 0 ; j < i - 1 ; j++)
            {
                if (a[j] == 42)
                    break;
                else
                    printf("%d\n", a[j]);
            }
        }

        if (a[i - 2] == 42)
            break;
    }

    return 0;
}
#包括
int main()
{
int i=1;
INTA[100];
printf(“输入数字:\n”);
而(i++<100)
{
如果(scanf(“%d%*c”,&a[i-2])!=1)
return-1;/*这是无效的输入,它将导致UB*/
如果(a[i-2]==42)
{
如果(scanf(“%d%*c”,&a[i-1])!=1)
return-1;/*这是无效的输入,它将导致UB*/
对于(int j=0;j

这至少消除了未定义的行为,并使代码更具可读性,但仍然不清楚此程序的用途或应该做什么。

我认为您的问题是试图声明(并重新声明)一个[I]多次使用不同的值。编译器需要在编译时知道数组的大小。因此,编译器为一个包含一个元素的整数数组留出内存,然后代码的动态部分尝试将其重新声明为运行时。请尝试将while循环移到外部,并使用定义良好的最大num声明它整数值的ber,如:

#define MAX_NUMS 1000

int a[MAX_NUMS];

上面还列出了一些其他问题,但我用GCC 4.9编译器对此进行了测试,它似乎给出了期望的结果。

一般情况下,由于输入值的数量未知,您需要为新输入的值动态分配存储

因此,我将构建一个包含输入值的列表。另一种方法是在输入新值时对整个数组使用
malloc
realloc
。在我看来,这是一种比构建列表更糟糕的解决方案

程序可以按以下方式运行

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

int main( void )
{
    struct node
    {
        int value;
        struct node *next;
    } * head = NULL;

    struct node **current = &head;
    int value;

    int next = -1;

    while ( next != 1 && scanf( "%d", &value ) == 1 )
    {
        if ( next == 0 || value == 42 ) ++next;

        if ( next == -1 )
        {            
            *current = malloc( sizeof( struct node ) );
            ( *current )->value = value;
            ( *current )->next  = NULL;
            current = &( *current )->next;
        }
    }

    while ( head )
    {
        printf( "%d\n", head->value );
        struct node *tmp = head;
        head = head->next;
        free( tmp );
    }        
}    
然后程序输出将是

1
2
3
4
5
6
7
8
9
该列表既不存储42,也不存储42之后的下一个值,尽管您可以修改程序,使其也存储这些值

至于你的代码,那么至少这句话

   a[i - 2] = temp;
当i等于1并导致程序行为未定义时,则为错误。
我看不出我会被改变。

总是检查scanf()返回的值(而不是参数值)为了确保操作成功,您的代码有很多问题,例如,它完全无法读取。我不明白您在代码中使用了什么逻辑。请使用代码中的注释进行澄清。尝试一些。我怀疑您会发现,有几个基本概念您还没有完全掌握,并且应该了解更多信息。例如,尝试向想象中的鸭子解释
inta[i];
a[i-2]=temp;
行应该做什么。请一致地缩进代码。建议在每个左大括号“{”后加4个空格,在每个右大括号“}前取消缩进“。为了可读性,请用空行分隔代码块。切勿使用制表符进行缩进,因为每个编辑器/文字处理器的制表位/制表符宽度设置不同。我认为
a[i-2]
是正确的,因为它将在i-2元素中分配temp的值。@iharob例如,如果我的第一个输入是2,那么
i=2
a[i-2]=2
a[0]=2
如果
i==3
怎么办?
a[1]
可能是初始化的,如果
scanf()
没有失败,并且
a[2]
也没有失败,但是您保持
a[0]
未初始化,并且仍然在
printf(“%d\n”,a[j])中打印它
这是未定义的行为。我知道这之前是未定义的。请检查我上面的评论。我确信
1 2 3 4 5 6 7 8 9 42 10
1
2
3
4
5
6
7
8
9
   a[i - 2] = temp;