r/C_Programming • u/Ill-Exchange-2377 • 20d ago
File pointers are everywhere
And try as I might to avoid them, I am struggling to find resources that list out all the file control functions that use file descriptors instead of file pointers, and I was wondering if someone could point me toward a resource that can do something at least somewhat similar to listing out the libc functions that take file descriptors. I of course know close(), write(), read(), the basics, but i am looking for alternatives for other abstractions that annoy me with their use of file pointers, like fgets and fputs.
58
Upvotes
View all comments
3
u/DamsLcs4421 20d ago
You shouldn't avoid pointers or try to postpone use "until the last moment". When will that last moment be?
Pointers are fundamental in binary programs, assembly can treat a value differently wether it should "just take the value" as in
mov rax, 0x401000(move the value 0x401000 in rax), or "interpret this value as a memory address and operate up there" as inmov rax, [0x401000](move the value contained at memory address 0x401000 in rax).C encapsulate all these operations under the notions of pointers and tables. I remember as a kid, I was in pain trying to understand the semantics and the real behaviours of passing tables to functions, etc... I admit pointers kinda clicked a little later in my programming journey, but it was a glass ceiling, as understanding this fundamental concept is central to understand, use, read and write C, and makes every higher level concept relying on them clearer to you. (Indexing a table is exactly like pointer arithmetic, for example. Understanding function pointers, ...)
I say this because other languages try to get away from pointers (like Python, JS, PHP, ...) or aim to avoid them as much as possible (Rust with
unsafe, C#, ...), but they are at the core of C and C++ and managing our own memory inevitably comes with these two languages, and having full control of memory is one key reason why people chose these languages for projects and librairies, thus most of the code you'll interact with is likely to heavily rely on pointers, as does virtually anything in C and C++.There's no harm done, though, wanting to avoid them at all cost... but beware it's really a niche. If you really don't want pointers, pick JS or Python (which backends also rely heavily on pointers internally, but unfortunately you'll never get to understand how ;p)