Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Random elements showing up on the array in c

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 <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] = %li\n", i, myNumbers[i]);
    };
    len(myNumbers);

    return 0;
}


// File name:- 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;
}

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…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading