Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Fundamentals of C Variables

Problems with Variables - XCode

Hello There! I have tried the "char" variable in XCode as part of Stage 1 of Objective C Development. Upon entering the variable, I am always greeted with an error that states, "Incompatible pointer to integer conversion assigning to 'char' from 'char [2]".

My code reads:

int main( )

{
    int days_in_a_week = 7;
    float cm_to_in = 2.54;

    char the_w = "W";

    printf("%d days in a week.\n", days_in_a_week);
    printf("%f cm per in.\n", cm_to_in);
    printf("The %c is a cool hotel.\n", the_w);

    return 0;
}

The output reads:

7 days in a week. 2.540000 cm per in. The 4 is a cool hotel. Program ended with exit code: 0

Please help with the error and why "W" is output at "4".

3 Answers

Stone Preston
Stone Preston
42,016 Points

try using single quotes around your char. chars use single quotes, whereas strings use double quotes. what happens when you use double quotes is that it thinks you want to make an array of characters, which explains the error message

int main()
{
    int days_in_a_week = 7;
    float cm_to_in = 2.54;

    char the_w = 'W';

    printf("%d days in a week.\n", days_in_a_week);
    printf("%f cm per in.\n", cm_to_in);
    printf("The %c is a cool hotel.\n", the_w);

    return 0;
}

Oh yes... no wonder it was not working. Just a stupid beginner's question. Thanks for helping out so quickly.

Also, when I had put up the code in my question, why was the code beginning only from "char the_w = "W";" shown? Was it one of those formatting errors?

Stone Preston
Stone Preston
42,016 Points

yes. the below animation shows how to post a code block. you have to skip a line after any previous text if you want it to be shown correctly. Ill edit your original post for you so you can see what I mean. just click edit and you can see my additions

alt text

Thank you so much! I had done it myself. :D