Hi, This comes more to the edge of coding language paradigms, personal preferences and readability. For enable in Rust everything that you define is "const" and if you want to modify it you should explicitly specify it. On another hand in C everything you define is modifiable unless you specify "const" to restrict unintended modification at compile time. This is one of many items that you can find in discussions why Rust is safer than the C.
The usage of "const" kind of improves compile time checking, but it adds more text to the lines and makes code harder to read. Like when I looked into https://github.com/apache/nuttx/pull/12683 I would maybe remove most of the "const" there in cases like "const mfs_t depth" or "const struct mfs_ctz_s new_ctz". But again this comes to personal preferences. Personally I'm trying to balance on the edge and inspire from POSIX sometimes. Of course POSIX could define "read" as "ssize_t read(const int fildes, void * const buf, const size_t nbyte);", but it is defined as "ssize_t read(int fildes, void *buf, size_t nbyte);" that seems to be more readable while still sacrifice compile time checking. This also becomes tricky when "const" may be used "when convenient" like in "jrnl_rdlog" from https://github.com/apache/nuttx/pull/12683. The "idx"/"pg" parameters of "jrnl_rdlog" seems also to need "const" like "const mfs_t depth" from "jrnl_wrlog", but just because author had a preference not to create a temporary variables "mfs_t tmp_pg = pg;"/"mfs_t tmp_idx = idx;" and it was "more convenient" to use "idx"/"pg" then const is omitted in that interface. So summarizing, there is no answer of what is the best or what should be used. Note: "const" before or after "*" are ANSI-C compatible, so has nothing to do with C++ feature enabled in C. Maybe there was something in between K&R-C and ANSI-C, but that really goes too far back in time. Best regards, Petro сб, 13 лип. 2024 р. о 17:45 <michal.lyszc...@bofc.pl> пише: > On 2024-07-11 23:36:22, Saurav Pal wrote: > > Hi all, > > > > Suppose I have a pointer that I want to pass through function parameters. > > In the function, neither is the pointer reassigned, nor is the thing it > > points to modified in any form. > > > > So, I would assume its function argument signature to be like FAR const > > struct my_struct * const ptr (The second "const" is my focus here). > > Similarly for things like const uint8_t my_num, etc. as well. > > > > However, I have not really seen much use of specifying constants using > > const (in the way I have mentioned above). Most of the examples I see are > > of FAR const struct my_struct * ptr. I have heard the compiler can make > > better optimizations for const, and because of this, I try to spam const > > anywhere and everywhere I can. But I don't see too many such usages > across > > the codebase, so I was wondering why that is (or I just maybe reading the > > code wrong, in which case, forgive me 🙏). > Because it doesn't really make much sense to do so. *EVERYTHING* in C is > passed as copy. Yes. Even pointers. Consider this > > | void foo(const void * const ptr); > | ... > | const void *mem = 0x123; > | foo(mem); > > It means that you won't change the memory at address 0x123. It's clear. > But second const actually only affects the function body itself. Even > without "const ptr", you won't be able to change users "mem" variable. > Even if you modify "ptr" inside "foo" you have only modified your local > ptr variable - same way as if you declared variable in body. > > So it adds basically nothing. And ptr in fact IS NOT constant, it's just > an integer (simplifying) on functions call stack. It belongs to you. > Second const just bloats declaration and gives almost nothing in return. > First const is very important, you know to expect function modifying (or > not) > what is rightfully yours. And those consts are promises anyway. Nothing > stops me from modifying your const if I wanted to (well... maybe except > for some hardware exception if data is really on RO medium ;)). > > And compiler can easily determine if something is const or not without > the const keyword. > > | int foo = 6; > | return foo * 4; > > Will be reduced to "24" anyway, even without "const int foo". > > -- > > .-----------------.-------------------.----------------------.-----------------. > | Michal Lyszczek | Embedded C, Linux | Company Address | .-. > opensource | > | +48 727 564 419 | Software Engineer | Akacjowa 10a; 55-330 | oo| > supporter | > | https://bofc.pl `----.--------------: Brzezinka Sredzka PL | /`'\ > & | > | GPG FF1EBFE7E3A974B1 | Bits of Code | NIP: 813 349 58 78 |(\_;/) > programer | > > `----------------------^--------------^----------------------^-----------------' >