r/C_Programming • u/neonge1674 • Sep 01 '26
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;
}
7
Upvotes
View all comments
Show parent comment
-1
u/P-39_Airacobra Sep 02 '26
order already is positional. except now there’s layers of positional depth on top. operator precedence complicates parsing and creates ambiguity in best coding practices
“a system that will require them to remember that it works differently” except this is already how standard languages are. either you pull up the operator precedence table or you defensively put parens everywhere so future readers don’t have to. note that most programmers recommend the latter method to avoid confusion around precedence. then i ask, why even have these rules to begin with? arbitrary complexity is nothing to admire.
you cite that it’s standard in math, as if programming languages don’t require you to learn concepts about numbers outside of basic arithmetic anyways. If you can teach a newbie how unsigned numbers work, or how a tertiary conditional works, then you can teach them to read left-to-right instead of down and up a table depending the specific operator being used
it’s really not rocket science, there’s nothing to remember. For operator precedence, however, that’s an entirely different matter. Open up the JavaScript precedence table, as a first example, and come back and tell me again that there’s more to remember in a left-to-right system than in a precedence system. your points are all very much nil