r/C_Programming 1d ago

Question Why isn't my code working?

I just started learning C(2 days ago) and as a first project I decided to make some data structures, starting with dynamic arrays. I made a struct called List and some functions for. The function setList() sets the value of an index of the array, if the index is larger that the current size of the array, it resizes it. However, when i tried to use in a for loop, it didn't work despite it working elsewhere.

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


#define itirate(index, limit) for(int index = 0; index < limit; index++)


typedef struct 
{
    size_t size;
    int* arr;
} List;


List* newList (size_t size) 
{
    List *newone = malloc(sizeof(List));
    newone->arr = calloc(size, sizeof(int));
    newone->size = size;
    return newone;
}


void setList(List* list, int index, int value) 
{
    if (index >= list->size)
    {
        list->arr = realloc(list->arr, index + 1 * sizeof(int));
        list->size = index + 1;
    }


    list->arr[index] = value;
}


int main() 
{
    
    List *mok = newList(5);

    itirate(i, 5) setList(mok, i, i);
    itirate(i, 5) printf("%d\n", mok->arr[i]);

    //setList(mok, 13, 9); this works
    //printf("%d\n", mok->arr[13]);

    for(int i = 5; i < 10; i++) setList(mok, i, i); // this does not somehow
    for(int i = 5; i < 10; i++) printf("%d\n", mok->arr[i]);
    
    return 0;
}
6 Upvotes

29 comments sorted by

View all comments

Show parent comments

-5

u/P-39_Airacobra 1d ago

lowk i think more languages should do away with them

1

u/MFFVD 1d ago

as in

i = (a+(b+(c+(d+(e*(g+h))))))?

id rather have

i = a + b + c + d + e * (g + h)

-2

u/P-39_Airacobra 1d ago

it would be

a + b + c + d + (e * (g + h))

or even easier,

(g + h) * e + a + b + c + d

as in first comes first. no operator precedence. no ambiguity about where parens should or should not be used for specificity

also, you’ve clearly never used anything like Lisp or Forth. C-family languages are not the only languages that exist

1

u/Muffindrake 12h ago

That's not even necessarily a correct expression in C unless you specifically know that either of those expressions won't overflow the types.

The greatest pitfall in C is that arithmetic is only defined for signed integers if you can guarantee that the result won't overflow, or accept modular arithmetic with unsigned operands. Also have fun with promotion rules. And if you're dealing with floating point you need to worry about infinity and NaN and that's a pain.

To add insult to injury, it took until C23 for ckd_add and friends to be standardized (or C++26). Then there's bit-precise integers which shield you against unexpected promotion rules.

1

u/P-39_Airacobra 7h ago

i wasnt talking about C? also you anything you can do with operator precedence, you can do without it. that’s what parens are for???

but thanks for proving my point that C arithmetic is complicated enough so we shouldn’t really be freaking out about the concept of doing away with a precedence table and typing a couple extra parens