1

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...

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
3

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

And even with string literals if a char array is not long enough, the null-terminator is not guaranteed to be there. E.g. char str[5] = "hello";
Hello Eric, we posted at the same time and we both chose an arbitrary length of 100 for example's sake... I like that.
The final 0 isn't strictly necessary, because all elements not explicitly initialised are set to 0.
Okay understood, so you are suggesting that i initialize the myNumbers with the length of 100. Just asking out of curiosity, if I during the runtime keep increasing the memory allocation without malloc... would it cause a lot of problem?? like int myNumbers[100] = {25, 50, 75, 100, 0}; and -1 in the for loop for length and reinitialize 0 to my preferred number and add another 0 to it at the end??
6

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.

2 Comments

Ohh okay... I understood, makes sense for the undefined behavior. Asking out of curiosity why does it work for 4 elements and not 4+ ??
@MeheerJ: C arrays have a length fixed at the definition or allocation point and cannot be changed afterwards. Your definition specifies a length of 4 elements from the initializer. You cannot add more elements, accessing them as in len() or push() has undefined behavior and you observe that when you read elements beyond index 3 in your own code. Undefined behavior means anything can happen: random values, zero values, segmentation fault... anything.
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.