Skip to content
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

printf %s does not allow char * argument #112

Open
kvanhees opened this issue Nov 21, 2024 · 0 comments
Open

printf %s does not allow char * argument #112

kvanhees opened this issue Nov 21, 2024 · 0 comments

Comments

@kvanhees
Copy link
Member

The printf action's %s (and %S) conversion accepts an array of char or a string as argument, but not char*. This is documented as such, but there is not really any good technical reason I can see why that would be the case. The documentation says:

The argument must be an array of char or a string. Bytes from the array or string are read up to a terminating null character or the end of the data and interpreted and printed as ASCII characters. If the precision is not specified, it is taken to be infinite so that all characters up to the first null character are printed. If the precision is specified, only the portion of the character array that is displayed in the corresponding number of screen columns is printed. If an argument of type char * is to be formatted, it should be cast to string or prefixed with the D stringof operator to indicate that DTrace should trace the bytes of the string and format them.

But if %s is specified for the argument then it is abundantly clear that the intent is to have the char * argument formatted as a string in the output. It would therefore make sense to do an automatic promotion to string rather than insisting on an explicit (string) cats or using the stringof() operator. In general, D does consider char * to be compatible with string, so why not here?

From the docs:

You can also assign to a string variable an expression of a type that is compatible with strings. In this case, the D compiler automatically promotes the source expression to the string type and performs a string assignment. The D compiler permits any expression of type char * or of type char[n], that is, a scalar array of char of any size, to be promoted to a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant