Skip to content

printf %s does not allow char * argument #112

@kvanhees

Description

@kvanhees

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions