Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You seem to worry a lot about type safety where there is no need for it. Since this is C and not C++, there is no strong typing system in place. Conversions from <code>unsigned char</code> to <code>char</code> are <em>usually</em> harmless, as long as the "sign bit" is never set. The key to avoiding problems is to actually understand them. The following problems/features exist in C:</p> <ul> <li>The default <code>char</code> type has implementation-defined signedness. One should never make any assumptions of its signedness, nor use it in arithmetic of any kind, particularly not bit-wise operations. <code>char</code> should only be used for storing/printing ASCII letters. It should never be mixed with hex literals or there is a potential for subtle bugs.</li> <li>The integer promotions in C implicitly promote all small integer types, among them <code>char</code> and <code>unsigned char</code>, to an integer type that can hold their result. This will in practice always be <code>int</code>.</li> <li>Formally, pointer conversions between different types could be undefined behavior. But pointer conversions between unsigned char and char are in practice safe.</li> <li>Character literals '\0' etc are of type <code>int</code> in C.</li> <li>printf and similar functions default promote all character parameters to int.</li> </ul> <p>You are also casting the void* result of malloc, which is completely pointless in C, and potentially harmful in older versions of the C standard that translated functions to "default int" if no function prototype was visible.</p> <p>And then you have various weird logic-related bugs and bad practice, which I have fixed but won't comment in detail. Use this modified code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; void display(const char *buff, int len) { for(int i = 0; i &lt; len; i++) { printf("buff[%d] = %c\n", i, buff[i]); } } int main() { int len = 10; unsigned char* buff = malloc(len * sizeof(unsigned char)); if(buff == NULL) { // error handling } char ch = 'A'; for(int i=0; i&lt;len; i++) { buff[i] = (unsigned char)ch + i; printf("char = %c\n", buff[i]); } printf("\nDisplaying array in main.\n"); for(int i = 0; i &lt; len; i++) { printf("buff[%d] = %u\n", i, buff[i]); } printf("\nDisplaying array in func.\n"); display((char*)buff, len); free(buff); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload