Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
spacem18447yI didnt use c for a few years but aren't arrays just pointers so you can realloc their size at any time as needed right?
-
@spacem Yeah. Arrays are pointers to the first memory location of a contiguous block.
You can resize them by reallocating a bigger block and copying elements over, just like vector in cpp or ArrayList in java does when you add and remove items -
@Lasagna I didn't mean the realloc function when i said reallocating. What i meant is something like this
1.get number of items in original array: sizof(arr)/sizeof(type)
2.Create a new block with malloc: malloc(size * 2)
3.copy all elements to new block.
4.Append new items at the end.
When the block is almost full, do the same thing.
not using realloc () -
@Lasagna K&R page 83
" ..the name of an array is a synonym for the location of the initial element.."
In fact K&R treat them as pointers to the first element and can increment or decrement them to access the array elements -
DrEmann2537yTry this:
char arr[100];
sizeof(arr); // 100
sizeof(&arr[0]); // 8 on 64 bit
Arrays are not pointers, but they are implicitly coerced to pointers in a lot of places (e.g. Function calls)
Related Rants
My teacher told us, that the array size in C is useless, becouse the array is dynamic and can be bigger or smaller everytime you want.. Happy overriding..
rant
c