openlang

Converting integer arrays into C strings

I'm attempting to turn some integer array elements into a C string. HEre is an example of what I'm doing:

int digits[] = {2,4,3,0};
char text[6];
for (int pos=0; pos<4; ++pos) {
    text[pos]=digits[pos];
}
printf("%s",text);

Instead I see heart shapes or smiling faces, and I don't understand the reason.

The conversation continues 2

Follow the reasoning
  1. Permalink

    Your displayed program cannot produce output because it fails to compile.

    It's unclear what you're trying to accomplsih, but consider this example:

    int main(void) {
        int digits[] = {2, 4, 3, 0};
        char text[sizeof(digits) / sizeof(digits[0]) + 1] = {0,};
        char pieces[sizeof(digits) / sizeof(digits[0])][12];
        for (int pos = 0; pos < 4; pos ++ ) {
            text[pos] = digits[pos] % 10 + '0';
            snprintf(pieces[pos], 12, "%d", digits[pos]);
        }
        printf("%s\n", text);
        for (int pos = 0; pos < 4; pos ++ ) {
            printf("%s\n", pieces[pos]);
        }
    }

Other sides of the same question

  1. Escaping percent characters when calling String.Format?

    My strings.xml file holds a SQL query, and I'm trying to construct the finished string in code with String.Format. The SELECT…

    Replies 3Latest activity java
  2. Formatting strings with F#

    I'm building an F# library for the first time Using string.Format gives me a complaint saying that the function doesn't exist…

    Replies 2Latest activity fsharp
  3. Extracting one section of a tcl string

    With tcl I'm attempting to break apart Reports/Report#2.rpt and keep just Report#2.rpt ; I tried this approach, but it produc…

    Replies 2Latest activity tcl