在C语言中,数组上出现的随机元素。

huangapple go评论98阅读模式
英文:

Random elements showing up on the array in c

问题

这是我的代码

// 文件名:list.c

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

int main() 
{
    int myNumbers[] = {25, 50, 75, 100};
    // int length = len(myNumbers);
    push(myNumbers, 200, len(myNumbers));
    for(int i = 0; myNumbers[i]; i++){
        printf("array[%d] = %d\n", i, myNumbers[i]);
    };
    len(myNumbers);

    return 0;
}


// 文件名:l.c

#include "l.h"
#include <stdio.h>

int len(int arr[])
{
    int i;
    for(i = 0; arr[i]!='
// 文件名:list.c

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

int main() 
{
    int myNumbers[] = {25, 50, 75, 100};
    // int length = len(myNumbers);
    push(myNumbers, 200, len(myNumbers));
    for(int i = 0; myNumbers[i]; i++){
        printf("array[%d] = %d\n", i, myNumbers[i]);
    };
    len(myNumbers);

    return 0;
}


// 文件名:l.c

#include "l.h"
#include <stdio.h>

int len(int arr[])
{
    int i;
    for(i = 0; arr[i]!='\0'; i++){
        continue;
    };
    printf("Length is: %d\n", i);
    return i;
}

void push(int list[], int value, int length)
{
    // int length = len(list);
    list[length] = value;
}
'
; i++){
continue; }; printf("Length is: %d\n", i); return i; } void push(int list[], int value, int length) { // int length = len(list); list[length] = value; }

上面的代码确实给我提供了我期望的结果,即:

Length is: 4
array[0] = 25
array[1] = 50
array[2] = 75
array[3] = 100
array[4] = 200
Length is: 5

但是,当int myNumbers[] = {25, 50, 75, 100, 125};或数组中有4个以上的值时...

结果会给出意外的随机值,如下所示:

Length is: 5
array[0] = 25
array[1] = 50
array[2] = 75
array[3] = 100
array[4] = 125
array[5] = 200
array[6] = 2782528512
array[7] = 1952226512
array[8] = 1
Length is: 9

如何解决这个问题?
我甚至尝试了直接传递长度,甚至在不传递长度的情况下调用函数,但它们都没有起作用...
我检查了代码是否存在逻辑错误,但我找不到任何错误...

我期望以下结果...

Length is: 5
array[0] = 25
array[1] = 50
array[2] = 75
array[3] = 100
array[4] = 125
array[5] = 200
Length is: 6
英文:

So just like in python list I wanted to try and implement using c (I am new to c programming).
This is my code

// File name:- list.c

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &quot;l.h&quot;

int main() 
{
    int myNumbers[] = {25, 50, 75, 100};
    // int length = len(myNumbers);
    push(myNumbers, 200, len(myNumbers));
    for(int i = 0; myNumbers[i]; i++){
        printf(&quot;array[%d] = %li\n&quot;, i, myNumbers[i]);
    };
    len(myNumbers);

    return 0;
}


// File name:- l.c

#include &quot;l.h&quot;
#include &lt;stdio.h&gt;

int len(int arr[])
{
    int i;
    for(i = 0; arr[i]!=&#39;
// File name:- list.c
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &quot;l.h&quot;
int main() 
{
int myNumbers[] = {25, 50, 75, 100};
// int length = len(myNumbers);
push(myNumbers, 200, len(myNumbers));
for(int i = 0; myNumbers[i]; i++){
printf(&quot;array[%d] = %li\n&quot;, i, myNumbers[i]);
};
len(myNumbers);
return 0;
}
// File name:- l.c
#include &quot;l.h&quot;
#include &lt;stdio.h&gt;
int len(int arr[])
{
int i;
for(i = 0; arr[i]!=&#39;\0&#39;; i++){
continue;
};
printf(&quot;Length is: %d\n&quot;, i);
return i;
}
void push(int list[], int value, int length)
{
// int length = len(list);
list[length] = value;
}
&#39;; i++){ continue; }; printf(&quot;Length is: %d\n&quot;, i); return i; } void push(int list[], int value, int length) { // int length = len(list); list[length] = value; }

The above code does give me the result I expect, i.e

Length is: 4
array[0] = 25
array[1] = 50
array[2] = 75
array[3] = 100
array[4] = 200
Length is: 5

Whereas when int myNumbers[] = {25, 50, 75, 100, 125}; or anything more than 4 values in the array...

The Result is given unexpected random values like:-

Length is: 5
array[0] = 25
array[1] = 50
array[2] = 75
array[3] = 100
array[4] = 125
array[5] = 200
array[6] = 2782528512
array[7] = 1952226512
array[8] = 1
Length is: 9

How to fix this issue?
I had even tried by directly passing the length and even calling the function without passing the length, but none of them worked...
I went through the code for any logic error, I wasn't able to find any..

I expect this as my result...

Length is: 5
array[0] = 25
array[1] = 50
array[2] = 75
array[3] = 100
array[4] = 125
array[5] = 200
Length is: 6

答案1

得分: 7

C不会自动用零终止数组(除了字符串字面值)。如果你想在数组末尾标记零,你必须自己添加。

C不会自动扩展数组。你的push例程不会为新元素在数组中腾出空间。这会破坏你的程序。

要像这样往列表中添加数字,你可以声明myNumbers具有大量的空间,就像int myNumbers[100] = {25, 50, 75, 100, 0};一样,以保留100个元素的空间,并在前面几个元素末尾标记零。当你学习更多关于C的知识时,你会了解到其他管理内存的方法。

英文:

C does not automatically terminate arrays with zero (except for string literals). If you want a zero to mark the end of your array, you must put it there.

C does not automatically grow arrays. Your push routine will not make space in the array for a new element. It will corrupt your program.

To tinker with pushing numbers onto a list like that, you can declare myNumbers to have a lot of space, as with int myNumbers[100] = {25, 50, 75, 100, 0}; to reserve space for 100 elements and to mark the end of the first few with a zero. As you learn more about C, you will learn about other ways to manage memory.

答案2

得分: 6

定义为int myNumbers[] = { 25, 50, 75, 100 }; 的数组正好有4个元素,并且没有空终止符。因此调用 len() 或尝试推送一个元素都具有未定义的行为。

您可以将 myNumbers 定义为 int myNumbers[100] = { 25, 50, 75, 100 };,这样您的代码可以工作,最多可以有99个元素,因为其余的元素将被初始化为 0

还请注意,printf 格式说明符 %li 预期一个 long int 类型的参数值,而 myNumbers[i] 不是这种类型。您应该使用 %i%d 将此 int 值输出为十进制数。

英文:

The array defined by int myNumbers[] = { 25, 50, 75, 100 }; has exactly 4 elements and does not have a null terminator. Hence calling len() or trying to push an element both have undefined behavior.

You could define myNumbers as int myNumbers[100] = { 25, 50, 75, 100 }; and your code would work up to 99 elements because the remaining elements will have been initialized to 0.

Note also that the printf format specifier %li expects a long int argument value, which myNumbers[i] is not. You should use %i or %d to output this int value as a decimal number.

答案3

得分: 1

你的代码中的问题与数组边界和内存访问有关。当你在C中像这样声明一个数组:int myNumbers[] = {25, 50, 75, 100};,它有一个固定的大小,包含4个元素。在不超出初始大小的情况下,你不能直接添加元素到它,否则会导致未定义的行为,这就是为什么你会得到意外的随机值。

为了解决这个问题,你需要处理动态内存分配,以增加数组的大小,当你想要添加更多元素时。以下是修改过的代码版本,使用动态内存分配来实现这一点:

// 文件名: list.c

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

int main() 
{
    int* myNumbers = (int*)malloc(4 * sizeof(int)); // 为4个元素分配内存
    int length = 4;
    myNumbers[0] = 25;
    myNumbers[1] = 50;
    myNumbers[2] = 75;
    myNumbers[3] = 100;
    
    push(myNumbers, 125, &length);
    push(myNumbers, 200, &length);
    
    for (int i = 0; i < length; i++) {
        printf("array[%d] = %d\n", i, myNumbers[i]);
    }
    
    free(myNumbers); // 释放动态分配的内存
    
    return 0;
}
// 文件名: l.c

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

void push(int* list, int value, int* length)
{
    list[*length] = value;
    (*length)++;
}
// 文件名: l.h

#ifndef L_H
#define L_H

void push(int* list, int value, int* length);

#endif

在这个版本中,myNumbers数组使用malloc进行动态分配,初始大小为4。length变量用于跟踪数组中的元素数量。当调用push函数时,它将新元素添加到数组的末尾,并增加length变量。这样,你可以在初始大小之外添加元素而不会出现问题。

记得在完成数组操作后使用free(myNumbers)来释放动态分配的内存,以避免内存泄漏。

英文:

The issue in your code is related to array bounds and memory access. When you declare an array in C like this: int myNumbers[] = {25, 50, 75, 100};, it has a fixed size of 4 elements. You cannot directly add elements to it beyond its initial size without causing undefined behavior, which is why you are getting unexpected random values.

To fix this issue, you need to handle dynamic memory allocation to increase the size of the array when you want to add more elements. Here's a modified version of your code to achieve this using dynamic memory allocation:

// File name: list.c

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &quot;l.h&quot;

int main() 
{
    int* myNumbers = (int*)malloc(4 * sizeof(int)); // Allocate memory for 4 elements
    int length = 4;
    myNumbers[0] = 25;
    myNumbers[1] = 50;
    myNumbers[2] = 75;
    myNumbers[3] = 100;
    
    push(myNumbers, 125, &amp;length);
    push(myNumbers, 200, &amp;length);
    
    for (int i = 0; i &lt; length; i++) {
        printf(&quot;array[%d] = %d\n&quot;, i, myNumbers[i]);
    }
    
    free(myNumbers); // Free the dynamically allocated memory
    
    return 0;
}

// File name: l.c

#include &quot;l.h&quot;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

void push(int* list, int value, int* length)
{
    list[*length] = value;
    (*length)++;
}

// File name: l.h

#ifndef L_H
#define L_H

void push(int* list, int value, int* length);

#endif

In this version, the myNumbers array is allocated dynamically using malloc, and its size is initially set to 4. The length variable keeps track of the number of elements in the array. When you call the push function, it adds the new element to the end of the array and increments the length variable. This way, you can add elements beyond the initial size without any issue.

Remember to free the dynamically allocated memory using free(myNumbers) when you're done with the array to avoid memory leaks.

答案4

得分: 0

int myNumbers[]可以声明更多空间,如下所示:

int myNumbers[100] = {25, 50, 75, 100, 125};

英文:

you can declare int myNumbers[] with more space like -

int myNumbers[100] = {25, 50, 75, 100, 125}; 

huangapple
  • 本文由 发表于 2023年8月5日 02:13:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定