Re: Using const in function arguments.

2024-07-14 Thread Saurav Pal
Hi Greg, I apologize for the slightly misleading example I provided. It was just a simple example name for a function that would let the people viewing it know it doesn't modify anything. A better example would be something like this: ssize_t mfs_read_page(FAR const struct mfs_sb_s * const sb,

Re: Using const in function arguments.

2024-07-14 Thread Saurav Pal
Hi all, First up, I am extremely sorry for the late response. I traveled the entire day and finally reached a place with network coverage. Thank you, Michal, this clears up my confusion. I was wondering if the compiler is smart enough for more complex cases. And compiler can easily determine if

Re: Using const in function arguments.

2024-07-14 Thread Nathan Hartman
That is correct. I was not suggesting to change the interface. We were only using read() as an example. We were basically saying this: If a function promises not to modify a buffer, its definition should contain const like this: void function_that_does_not_modify_buffer(const uint8_t * buffer);

Re: Using const in function arguments.

2024-07-13 Thread Gregory Nutt
The prototypes of standard interface functions  are specified in the POSIX standard.  It would be a POSIX violation to use whatever prototype you happen to like most.  No one gets to "improve" POSIX. read() is specified here https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html

Re: Using const in function arguments.

2024-07-13 Thread Nathan Hartman
Replying inline below... On Sat, Jul 13, 2024 at 3:21 PM Petro Karashchenko < petro.karashche...@gmail.com> wrote: > 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

Re: Using const in function arguments.

2024-07-13 Thread Petro Karashchenko
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 "c

Re: Using const in function arguments.

2024-07-13 Thread michal . lyszczek
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 b

Re: Using const in function arguments.

2024-07-12 Thread Nathan Hartman
Hi Saurav, I think it comes down to whether a parameter like "const uint8_t * const" is C89-compatible. I can't seem to find a reference that gives me a definitive answer to this question, but it *might* have originally been a C++ feature that became standard C at some point. Whether that happened

Re: Using const in function arguments.

2024-07-12 Thread Saurav Pal
Hi Alan, Thank you for the information. However, for my case, it's passed through function parameters. For example, let's say it's a helper function to read mem[2]. `mem` in this case is a function argument. It isn't reassigned, so the pointer is a const, and it isn't used to modify the underlyin

Re: Using const in function arguments.

2024-07-12 Thread Alan C. Assis
Hi Saurav, I don't know why it is not much used, maybe Greg or Xiang have some idea. I think the most common use of const for variables that you want to keep in flash to avoid keeping it in RAM (to save RAM space for MCU with low RAM memory). The side effect on this case is Flash access is slowe

Using const in function arguments.

2024-07-11 Thread Saurav Pal
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 secon