Is this the correct way to print char*? *I know c has the value I want because of the print statement below it. The o is not giving the value that I expect.

        char   out[50];
        char  *o = out;
        int    i = 118;
        *o++ = c;
        printf("o is %d (%c)\n", o[0], o[0]);
        printf("o is %d (%c)\n", o[1], o[1]);
        printf("o is %d (%c)\n", o[2], o[2]);
        printf("c is %d \n", c);

Recommended Answers

So you incremented your pointer o. You're sort of lucky the compiler initialized your array.

Example? http://ideone.com/qijRC1

So at line 3 o is point at out(0) and after line 4 o is pointing to out[1]. Code is not broke?

Jump to Post

All 3 Replies

So you incremented your pointer o. You're sort of lucky the compiler initialized your array.

Example? http://ideone.com/qijRC1

So at line 3 o is point at out(0) and after line 4 o is pointing to out[1]. Code is not broke?

You're using post increment instead of pre increment. Which means the value of o changes after you assign c to it. Using pre increment o will increment then c will be assigned to it:

    *++o = c;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.