I'm looking for an "append" (Python) or "push_back" (C++) equivalent in C to fill an array of strings (char). Does it exists?
Asked
Active
Viewed 381 times
0
-
2Short answer: Nope. Long answer: I'm sure some third party library has implemented stuff like this (maybe not published as a part of their API though), but the closest thing the C standard provides is `realloc`, which is both harder to use and needs more custom code to work with than the easy stuff from higher level languages. – ShadowRanger Jan 06 '21 at 22:33
-
This is answered here: https://stackoverflow.com/questions/3536153/c-dynamically-growing-array – mkrieger1 Jan 06 '21 at 22:43
-
C isn't any different from C++ in this case (in regards to adding to arrays). – Jan 06 '21 at 23:56
-
Basically, no. You have to do it yourself. There are no libraries to do it for you. – kill -9 Jan 07 '21 at 01:13
1 Answers
2
The question doesn't make sense because you cannot add elements to an array in C. In C, you set the size of the array when you create it, and the size cannot change.
If you want to "append", you have to create a big array (e.g. 1000 items), and use a separate variable to remember how many items you're actually using (the rest are spare).

user253751
- 57,427
- 7
- 48
- 90
-
1The most we can do is setting the last "unused/free" (whatever it means) element to a given value, but such a built-in function doesn't exist. – limserhane Jan 06 '21 at 22:43