An array is defined by a type and a size (number of elements):
#define SIZE 1000
int array[SIZE];
// ~~~ ~~~~
// tipo tamaño
Since the indices start at 0, the last element will be SIZE-1:
int ultimo = array[SIZE - 1];
In C you can't do like in Python. Python is a much more modern language that has been developed in a much more benign environment than C.
When C was born, the computers of the time struggled to have a few KB of RAM and disks whose capacity was easily measured in MB. At that time any unnecessary operation consumed precious clock cycles.
Due to these serious limitations, the language was designed on the basis that the language was not going to give more than the essential help.
So, while in Python there is a sequence that checks if the position is negative and, if not, starts traversing the array from the end, in C the number you put in square brackets is directly interpreted as an offset in memory.
What has been said, they are languages raised at very different moments in history
I can think of a scenario where for some strange reason you don't have a way to know the size of the array, either because there are too many modules in the spaghetti code, or because the code is too long, or because they let you work fast on one part of a code that assumes you have an array. Or also make it a text string:
int array_size = sizeof(unknown_size_array)/sizeof(unknown_size_array[0]);
int last_element = unknown_size_array[array_size-1];
A bit of a far-fetched example, but just to test this:
#include <stdio.h>
#define unknown_size 9
void fill_array_incrementally(int* array_to_fill, int size) {
for (int i = 0; i < size; ++i)
array_to_fill[i] = (i+1)*100;
}
void print_array(int* array_to_print, int size) {
printf("Array content: ");
for (int i = 0; i < size; ++i)
printf("%i,", array_to_print[i]);
printf("\n");
}
int main() {
/*Supon que no conocemos de aqui(...)*/
int unknown_size_array[unknown_size];
fill_array_incrementally(unknown_size_array, unknown_size);
print_array(unknown_size_array, unknown_size);
/*(...) hasta aca*/
int array_size = sizeof(unknown_size_array)/sizeof(unknown_size_array[0]);
int last_element = unknown_size_array[array_size-1];
printf("last: %i\n", last_element);
return 0;
}
This strategy would also work with types char[], such as char palabra[] = "hola", but the last character that matters to us would not be the last, but the penultimate (that is, instead of subtracting one, we subtract two, -2 to the size), since the last character is always the character null.
An array is defined by a type and a size (number of elements):
Since the indices start at 0, the last element will be SIZE-1:
In C you can't do like in Python. Python is a much more modern language that has been developed in a much more benign environment than C.
When C was born, the computers of the time struggled to have a few KB of RAM and disks whose capacity was easily measured in MB. At that time any unnecessary operation consumed precious clock cycles.
Due to these serious limitations, the language was designed on the basis that the language was not going to give more than the essential help.
So, while in Python there is a sequence that checks if the position is negative and, if not, starts traversing the array from the end, in C the number you put in square brackets is directly interpreted as an offset in memory.
What has been said, they are languages raised at very different moments in history
You could also use the technique of
sizeof
.I can think of a scenario where for some strange reason you don't have a way to know the size of the array, either because there are too many modules in the spaghetti code, or because the code is too long, or because they let you work fast on one part of a code that assumes you have an array. Or also make it a text string:
A bit of a far-fetched example, but just to test this:
Returning this:
This strategy would also work with types
char[]
, such aschar palabra[] = "hola"
, but the last character that matters to us would not be the last, but the penultimate (that is, instead of subtracting one, we subtract two, -2 to the size), since the last character is always the character null.