-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make array
& new_array
const-correct in test_is_string_in_array
.
#223
Conversation
There was an error running your pipeline, see logs for details. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 🚀
One note:
Since the new_array
variable is allocated on the heap and later free'd, it may be better to keep it char **
and instead explicitly cast it to const char **
when passing it as an argument to IsStringInArray
. The reason is that free(3)
takes a void *
as an argument (not const void *
).
No, how he did it is correct. This is a double pointer, #include <stdio.h> // printf()
#include <stdlib.h> // calloc()
int main(void)
{
const char **strings = calloc(3, sizeof(const char *));
// C compiler is not very strict with string literals
strings[0] = "hello"; // This works regardless of whether strings has const or not
// After assigning to a typed variable, the compiler will be more strict:
const char *world = "world";
strings[1] = world; // Const correctness is checked here.
printf("%s, %s\n", strings[0], strings[1]);
void *temp = strings;
// const void *temp = strings; // This would not work, const mismatch.
free(temp); // free(strings) would also work
return 0;
}
|
Thanks-- works much better! |
When building on Arch, I saw a few compilation errors of the form:
This small commit fixes them.