Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
brycecampbell
2,112 PointsObjective-c : Arrays
I need help on code challenge:Array with task 4 where says "Print the results. Your output should look like "letters a b". when I enter printf("*letters a b= %c\n", *letters); it displays: i can the correct string, but the wrong format
please some suggestions.
5 Answers
christo
3,525 PointsHi Bryce,
So looking at the code challenge, the task is to print letters a b
In the previous code tasks, you've assigned the characters 'a' and 'b' to the variables alpha and bravo of type char. So if you want to print letters a b, you want to represent the letters 'a' and 'b' with the alpha and bravo variables which we've assigned to the letters array. In objective-c, you use placeholders to represent those variables in a string. In this case, because you want to use char variables, you use the placeholder %c. If it was a string, you could use %@, a float %f, etc. So all you would do it just replace the letters 'a' and 'b' in the string "letters a b" with %c:
char alpha = 'a';
char bravo = 'b';
char letters[2];
letters[0] = alpha;
letters[1] = beta;
printf("letters %c %c", letters[0], letters[1]);
between the quotes, you just write out the string output that you want. When you come to a part of the string that you want represented by a variable, replace that part with the placeholders, %c, %@, %f or whatever you want. You can see the different placeholder variables here: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/Strings/Articles/formatSpecifiers.html
And then after you're done writing out the string you want, you have to follow it up with what those placeholder variables represent. In our case, we placed two placeholders, %c %c. So we have to define what those are: letters[0] and letters[1].
Hope this helps!
Adeeb Syed
3,275 PointsHey Bryce,
I just recently finished this so I can help you out. In your printf statement, you don't need to include the * before letters. Also, when it asks for a and b, they want you to print them via their respective variables. So try something like this:
printf("letters %c %c\n", alpha, bravo);
brycecampbell
2,112 Pointsthank you man!
brycecampbell
2,112 Pointscool I appericated i.... for the notes
jacknoren
24,134 PointsYou can also shorten the code up by writing it like this :
char letters[ ] = {alpha, bravo}
printf{" letters %c %c\n", letters[0], letters[1])
this is just makes the code a lot cleaner