ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
the above function - memcmp receiving a1 as pointer to void which is 'const' this means content of the a1 can not be changed.
Question:
const void *a1;
We can not dereference a1 this must be compiler error because its pointing to an address which is void then why memcmp should receive a1 as "const void*" instead of "void *".
We can not dereference a1 this must be compiler error because its pointing to an address which is void then why memcmp should receive a1 as "const void*" instead of "void *".
A reasonable implementation of memcmp must effectively cast the pointer to const char* before using it.
A reasonable caller of memcmp might have cast the pointer from const XXX* for some type XXX before passing the pointer to memcmp.
If memcmp took the pointer as void* then the caller would be casting from const XXX* to void*, which would appear to be a bug.
What is your real question? Eg:
Q: What 'const' means in 'const void *a1'?
A: memcmp won't change the memory pointed by a1
Q: What 'void *' means?
A: Generic pointer.
>> My question was, why memcmp or for that reason any
>> C function which receives void *arg as argument if
>> i add 'const' qualifier - what is the use.
> I believe it is only a documentation aid. It makes
> sure that the programmer knows the parameters are
> not modified.
It is not 'only' a documentation aid. As already mentioned, const void *a1 means
a1 is a pointer to constant data. The compiler can use this information to do useful things such as being clever about optimising the code. And if you provided your own definition of memcmp which did modify a1, then the compiler could tell you that you had made a mistake. It's much better to be told about such mistakes at compile time than to have to try to find them when the program misbehaves at run time.
For comparison, observe that memcpy is declared as memcp(void *dest, const void *src, size_t n) because the copy process does modify the contents pointed at by dest pointer.
what was not mentioned, actually memcmp does not take care about the meaning of the content. it has [got] two memory addresses, and compares the content byte by byte (compares size number of bytes). So you can say, the type of the data is irrelevant, and void * means exactly that: a pointer to something, whatever, without knowing/handling its type, structure. const means the function will not change the content of that data.
void * const Var; /* The pointer is constant,
* the data its self can change. */
const void * Var; /* The data is constant,
* the pointer can change */
const void * const Var;
OR
void const * const Var; /* the pointer and data are both constant */
As for the keyword void, that would indicate that the function makes no assumption about the type of data the pointer is referencing. The function would make an appropriate internal cast to allow byte by byte comparisons. (For speed purposes it might actually do a 32-bit word by 32-bit word comparison then compare the 4 bytes in the word that doesn't match).
I always use the following hint from Wikipedia on how C/C++ handle the const keyword:
Quote:
To render the syntax for pointers more comprehensible, a rule of thumb is to read the declaration from right to left. Thus, everything to the left of the star can be identified as the pointee type and everything to the right of the star are the pointer properties. (For instance, in our example above, int const * can be read as a mutable pointer that refers to a non-mutable integer, and int * const can be read as a non-mutable pointer that refers to a mutable integer.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.