-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
translate-c: C code that uses string literals as non-const char * #9126
Comments
then what exactly non-const about them? |
They're effectively const; It's just that it's a quirk of the C standard that string literals have the type #include <stdlib.h>
char foo(char *s) {
return s[0];
}
int main(void) {
if (foo("bar") != 'b') abort();
} |
are they able to coerce to a |
Yes, but we can't change the signature of |
In C the type of string literals is `char *`, so when using them in a non-const context we have to cast the const away. Fixes ziglang#9126
In C the type of string literals is `char *`, so when using them in a non-const context we have to cast the const away. Fixes ziglang#9126
In C the type of string literals is `char *`, so when using them in a non-const context we have to cast the const away. Fixes ziglang#9126
In C the type of string literals is `char *`, so when using them in a non-const context we have to cast the const away. Fixes ziglang#9126
In C the type of string literals is `char *`, so when using them in a non-const context we have to cast the const away. Fixes #9126
In C string literals have the type
char[N]
(not const, but modifying the contents is undefined behavior). C code that uses this isn't compatible with Zig's[]const u8
string literals:This can occur in variable declarations, function calls, and struct and array initializers - did I miss any?
I noticed in
getExprQualType
we turnchar *
intoconst char *
but I'm not sure why or what the implications of removing that are. Another solution would be to detect situations where a string literal is used as achar *
and insert intToPtr / ptrToInt to cast away the constness.The text was updated successfully, but these errors were encountered: