🌐
Quora
quora.com › What-is-the-length-of-a-list-in-C
What is the length of a list in C++? - Quora
Length%2==0? Length/2–1: Length/2 is pretty much an if statement and can be used outside of the for loop too. ... It’s a for loop clause. length is a variable defined before this loop statement. ... A for loop statement is defined like this for example: FOR (Starting Condition; IF Clause; What to do to increment/Detriment
🌐
Reddit
reddit.com › r/roguelikedev › what's the best way to make a variable-length list of items or monsters in c?
r/roguelikedev on Reddit: What's the best way to make a variable-length list of Items or Monsters in C?
July 13, 2021 -

TL/DR: Are linked lists the best solution for an Entity list in C or is there a better solution?

I'm currently in the process of refactoring a small tutorial roguelike I made in C in order to make the code conform to best practices. In my game I have a list of Items and a list of Monsters which I can iterate through in order to make monsters take turns or see if the player is able to pick up an Item. Until recently, I had these lists implemented simply as arrays of pointers with a respective int counter to keep count of how many Items or Monsters there were at any given time:

Item* items[15] = { NULL };
int n_items = 0;

The arrays were bounded with a hard-coded limit (15 in this example). This limit could easily be observed when creating a new level and keep the number of items below the max, but once the player was given the option to 'drop' items, this limit became a problem, as any item carried over from other levels and dropped in a newly created dungeon level would now be added back into the item array and could easily overflow it.

In order to solve this issue I've implemented a singly linked list struct as:

typedef struct List 
{
    union {
        Actor* actor;
        Item* item;
    };
    struct List* next;
} List;

This List struct uses the following function to add new Items to the list:

void appendItem(List* head, Item* item)
{
    List* temp = head;
    
    while (temp->next)
    {
        temp = temp->next;
    }
    temp->next = malloc(sizeof(List));
    temp = temp->next;
    temp->item = item;
    temp->next = NULL;
}

The list can now be initiated as:

List* items = malloc(sizeof(List));
items->item = NULL;
items->next = NULL;

And I can iterate through the list with:

List* temp = items;
while (temp = temp->next)
{
    checkSomething(temp->item);
}

This setup now allows me to forget about a limit on items or actors and just add them as needed. However, before I continue with this structure and refactor all other arrays to use this, I wanted to ask if anyone knows whether this would be the best solution for the problem of the item and monster lists in a C roguelike. I've delved into the original Rogue source code and saw that the THING union has a prev and next pointers so I believe that it uses a similar setup, but I don't know if that is a good modern C solution. If any C developers would share any best-practices that they know of regarding this issue, it would be much appreciated. Thanks for taking the time to read through this and for any reply!

TL/DR: Are linked lists the best solution for an Entity list in C or is there a better solution?

Discussions

C Program Length of Array
int is a 32-bit (= 4 byte) data type, so sizeof(array) returns the number of elements times the size in bytes of a single object. A common way of getting the length of an array in C is sizeof(array)/sizeof(array[0]). More on reddit.com
🌐 r/code
6
3
February 21, 2022
List input in C and length argument - Code Golf Meta Stack Exchange
When dealing with list input in C, as in this question, is it acceptable to add extra argument to indicate the length of the list? It does give advantages because other language needs functions li... More on codegolf.meta.stackexchange.com
🌐 codegolf.meta.stackexchange.com
How do I determine the size of my array in C? - Stack Overflow
How do I determine the size of my array in C? That is, the number of elements the array can hold? More on stackoverflow.com
🌐 stackoverflow.com
(C#) find length of a List<String>?
This is a common error. But stop to think about it. Let's say I have this: List mylist = new List(); mylist.Add("test1"); mylist.Add("test2"); How many items are there? 2 right. And since indexes are zero based it would be: mylist[0] = "test1" mylist[1] = "test2" Now, take a look at your look again and see if you can find the problem. More on reddit.com
🌐 r/learnprogramming
10
7
January 13, 2012
People also ask

How do you find the length of an array in C?
You can find the length of an array in C using methods like the `sizeof()` operator or by iterating through the array with a loop. For character arrays, a common approach is to use the null terminator `'\0'` to indicate the end, while for integer arrays, you might use a sentinel value or pointer arithmetic.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › length of an array in c
Find the Length of an Array in C: Methods & Examples
Why is `sizeof` used to determine the length of an array in C?
The `sizeof` operator returns the size of a variable or data type in bytes. For an array, `sizeof(array)` gives the total number of bytes the array occupies in memory. To get the number of elements, divide by `sizeof(element)`, which gives the size of each element in the array.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › length of an array in c
Find the Length of an Array in C: Methods & Examples
What is the length of an empty array in C?
C does not allow truly empty arrays with size 0. You must specify a positive size during declaration.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › length-of-array
How to Get Length (Size) of Array in C? With Examples
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › length of an array in c
Find the Length of an Array in C: Methods & Examples
April 30, 2025 - Learn how to find the length of an array in C using different methods like pointer arithmetic, loops, and the size of operator with examples and explanations.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-program-for-finding-length-of-a-linked-list-iterative-and-recursive-approach
C Program For Finding Length Of A Linked List - GeeksforGeeks
July 23, 2025 - Following is the Iterative implementation of the above algorithm to find the count of nodes in a given singly linked list. ... // Iterative C program to find length or count // of nodes in a linked list #include<stdio.h> #include<stdlib.h> // Link list node struct Node { int data; struct Node* next; }; /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list.
🌐
Cplusplus
cplusplus.com › reference › list › list › size
std::list::size
Returns the number of elements in the list container.
🌐
WsCube Tech
wscubetech.com › resources › c-programming › length-of-array
How to Get Length (Size) of Array in C? With Examples
1 week ago - Learn in this tutorial how to find the length (size) of an array in C with examples. Understand the concept clearly and improve your C programming skills.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › length-of-array-in-c
Length of Array in C - GeeksforGeeks
This gives the number of elements in the array, which is 20/4 = 5 · We can also calculate the length of an array in C using pointer arithmetic.
Published   October 17, 2025
Find elsewhere
Top answer
1 of 6
14

Not for all languages

I estimate 15% of my Python golfs with list input could be shortened by taking in its length, if that were allowed. Hundreds of golfs in mainstream languages could be improved by mechanically replacing "len(l)" or similar with an input parameter.

These submissions strongly suggest that golfers wouldn't guess this to be allowed without knowing the rule specifically. This is a hidden rule of the worst kind -- broadly useful, unexpected, and likely to make golfs more boring on average.

I'm sympathetic to the problems languages like C have with cumbersome input processing, especially as they already have many disadvantages. Golfing languages can be designed around such issues, but C is stuck with them.

But, I want to avoid the trend of giving all languages an easy extra workaround because one language really wants it. The result is a laundry list of liberties with input that go beyond taking it conveniently and naturally for the language, to doing parts of the golfing task in the input format, justified by citing obscure meta threads about other languages.

I'd rather say that this is a property of C that golfers need to deal with, or that a C-specific rule be made. Either one would be better than changing the rules for all languages.

2 of 6
12

This is an interesting indication of the way PPCG has changed since the early days. I remember when a lot of questions included the length as a separate input and people commented with requests to make it optional because their high-level languages didn't need it.

In most high-level languages an array is effectively a struct with a pointer and a length. I don't see that there's any point to creating a standard struct template. However, it does seem perfectly reasonable to interpret "array" in a question as meaning "pointer and length, as encapsulated in your language". In the case of C the simplest "encapsulation"* is as two variables.

* Yes, I get the point that it's not really encapsulation if you can split them up, hence the scare quotes. But such pedanticism is not the point here.

🌐
Sentry
sentry.io › sentry answers › c › determine the size of an array in c
Determine the size of an array in C | Sentry
May 15, 2023 - Being of the same type, all elements in a C array will also be of the same length, so we can use the first element (my_array[0]) to do this: size_t list_length = sizeof(my_array) / sizeof(my_array[0]);
🌐
IONOS
ionos.com › digital guide › websites › web development › c: array length
How to determine the length of an array in C
December 10, 2024 - Total size of the array: 20 bytes Size of a single element: 4 bytes Number of elements in the array: 5c · Pointers them­selves don’t contain in­for­ma­tion about the size or length of an array.
🌐
DigitalOcean
digitalocean.com › community › tutorials › find-length-of-a-linked-list
How to Find Length of a Linked List? | DigitalOcean
August 3, 2022 - */ void insert(int num) { /* Create a new Linked List node */ struct node* newNode = (struct node*) malloc(sizeof(struct node)); newNode->data = num; /* Next pointer of new node will point to head node of linked list */ newNode->next = head; /* make new node as the new head of linked list */ head = newNode; printf("Inserted Element : %d\n", num); } int getLength(struct node *head){ int length =0; while(head != NULL){ head = head->next; length++; } return length; } /* Prints a linked list from head node till the tail node */ void printLinkedList(struct node *nodePtr) { while (nodePtr != NULL) {
🌐
Cppreference
en.cppreference.com › w › cpp › container › list › size
std::list<T,Allocator>::size
May 8, 2025 - Returns the number of elements in the container. ... #include <list> #include <iostream> int main() { std::list<int> nums {1, 3, 5, 7}; std::cout << "nums contains " << nums.size() << " elements.\n"; }
Top answer
1 of 16
1754

Executive summary:

int a[17];
size_t n = sizeof(a)/sizeof(a[0]);

Full answer:

To determine the size of your array in bytes, you can use the sizeof operator:

int a[17];
size_t n = sizeof(a);

On my computer, ints are 4 bytes long, so n is 68.

To determine the number of elements in the array, we can divide the total size of the array by the size of the array element. You could do this with the type, like this:

int a[17];
size_t n = sizeof(a) / sizeof(int);

and get the proper answer (68 / 4 = 17), but if the type of a changed you would have a nasty bug if you forgot to change the sizeof(int) as well.

So the preferred divisor is sizeof(a[0]) or the equivalent sizeof(*a), the size of the first element of the array.

int a[17];
size_t n = sizeof(a) / sizeof(a[0]);

Another advantage is that you can now easily parameterize the array name in a macro and get:

#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))

int a[17];
size_t n = NELEMS(a);
2 of 16
1120

The sizeof way is the right way iff you are dealing with arrays not received as parameters. An array sent as a parameter to a function is treated as a pointer, so sizeof will return the pointer's size, instead of the array's.

Thus, inside functions this method does not work. Instead, always pass an additional parameter size_t size indicating the number of elements in the array.

Test:

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

void printSizeOf(int intArray[]);
void printLength(int intArray[]);

int main(int argc, char* argv[])
{
    int array[] = { 0, 1, 2, 3, 4, 5, 6 };

    printf("sizeof of array: %d\n", (int) sizeof(array));
    printSizeOf(array);

    printf("Length of array: %d\n", (int)( sizeof(array) / sizeof(array[0]) ));
    printLength(array);
}

void printSizeOf(int intArray[])
{
    printf("sizeof of parameter: %d\n", (int) sizeof(intArray));
}

void printLength(int intArray[])
{
    printf("Length of parameter: %d\n", (int)( sizeof(intArray) / sizeof(intArray[0]) ));
}

Output (in a 64-bit Linux OS):

sizeof of array: 28
sizeof of parameter: 8
Length of array: 7
Length of parameter: 2

Output (in a 32-bit windows OS):

sizeof of array: 28
sizeof of parameter: 4
Length of array: 7
Length of parameter: 1
🌐
RIT
se.rit.edu › ~swen-250 › slides › instructor-specific › Rabb › C › 08-C-Lists.pdf pdf
Personal Software Engineering Lists in C
This is the purpose of the sizeof operator! ... NOTE: all pointers to any type have the same size! ... Because array arguments are really pointers! ... Padding is dictated by the way CPU's access memory. ... We keep a pointer to the first node in a list head pointer.
🌐
Scaler
scaler.com › home › topics › how to find the length of an array in c?
How to Find the Length of an Array in C? - Scaler Topics
August 16, 2022 - The sizeof() operator in C calculates the size of passed variables or datatype in bytes. We cannot calculate the size of the array directly using sizeof(), we will use the programming logic defined above to find the length.
🌐
Reddit
reddit.com › r/learnprogramming › (c#) find length of a list?
r/learnprogramming on Reddit: (C#) find length of a List<String>?
January 13, 2012 -

I am attempting to grab a List<String> and iterate through each string in the list then Iterate through each Character in each string.

I can get the length of the list, but I am having a hard time getiing the length of the String.

List<String> myList;
 for (int y = 0; y <= myList.Count(); y++)
        {
 for (int x = 0; x <= myList[y].Length ; x++)
{

And I keep getting an index out of bounds error. I know I am probably going about this all wrong. So I will ask here for advice and come back tomorrow with a fresh head and see if it makes any more sense.

Thanks!

🌐
Rip Tutorial
riptutorial.com › array length
C Language Tutorial => Array length
In fact, that particular error ... return size of 'int *' instead of 'int []' [-Wsizeof-array-argument] int length = sizeof(input) / sizeof(input[0]); ^ note: declared here int BAD_get_last(int input[]) ^...
🌐
Quora
quora.com › How-is-the-length-of-an-array-in-C-determined
How is the length of an array in C determined? - Quora
Dereferencing to *(&arr + 1) gives ... we can subtract the pointer to the first element to get the length of the array: *(&arr + 1) - arr....
🌐
W3Schools
w3schools.com › c › c_arrays_size.php
C Get the Size of an Array
You learned from the Data Types chapter that an int type is usually 4 bytes, so from the example above, 4 x 5 (4 bytes x 5 elements) = 20 bytes. Knowing the memory size of an array is great when you are working with larger programs that require ...