I can't wrap my head around this bug...
12 20 Jun 2015 05:29 by u/frankis
So I'm taking an online course in Objective C, and the current homework is a program that takes in a value from the user between 1-50 and outputs a grid with "N N^2 N^3 N^4" at the top (see here) and the related values up to the inputted max below. I have the program written already and if I were to submit it I'd surely get a decent grade, but it's not due until Sunday so I started messing around.
I wrote a segment that instead of statically printing the header will do so dynamically based on the number of columns defined by a constant. The problem I'm running into is that it works for only about half the numbers in our given range (1-50). What's really odd is the range is defining the number of rows, not columns. It seems to be putting a random number, or asterisk, or parentheses between the N and the ^; or sometimes just dropping the N entirely. Here is the segment of code I have as it stands, any thoughts would be greatly appreciated!:
const int MAX_COLUMNS = 4;
char notation = 'N';
for (int col = 0; col < MAX_COLUMNS; col++) {
if (col == 0) {
printf("%s%11c", ¬ation, ' ');
} else {
printf("%s^%i%9c", ¬ation, col + 1, ' ');
}
}
6 comments
10 u/scotomaniac 20 Jun 2015 06:17
In languages I know, %s is for null-terminated strings. Notation is a char, not a null-terminated string. Try %c instead of %s when printing chars (and drop the &)?
Edit: that is almost certainly the problem. In the "50" example, it's printing "N2", because 50 happens to be the ASCII code for the character "2". In the "8" example, it appears to print nothing because 8 is the ASCII code for backspace (so, it prints the "N", and then moves the cursor back one space; when it prints the ^, the N gets overwritten).
I bet if you choose 65, you'll see "NA", "NA^2", and so on.
Under the hood, your printf is printing whatever it finds in memory starting from the address you gave it, until it finds a byte with value "0". It just so happens that MAX_COLUMNS is located there, so the bytes that make up MAX_COLUMNS get printed as if they were part of the string.
2 u/frankis [OP] 20 Jun 2015 07:43
Yup, that was it. I couldn't figure out what it was complaining about and this was the fix. Dropped the & and changed to %c (I had %c and ¬ation there previously which Xcode complained about).
1 u/112365365321 20 Jun 2015 06:24
In your print statements, why did you use %s for a char and then pass a pointer? This seems like the most likely cause to me. Maybe try
printf("%c%11c", notation, ' ');I think that should work.
I think what is happening is you are giving printf the address to a char which gets represented as an int, so effectively you are passing an int to be printed as a string. I could be wrong about that though.EDIT: Yeah, I was wrong. Thanks to /u/taxation_is_slavery for pointing this out below.EDIT: I'd also like to add that you could even do something like:
printf("%11c", notation);Although it might mess with the spacing, you might need to increment that value or something but it eliminates the need to pass the space char into the function to get it to space out. There is a width and a length flag and I'm not sure which is being used instead of which in this case, but I do know it's shorter and more efficient.4 u/taxation_is_slavery 20 Jun 2015 06:37
Actually he was giving the address of that char, but %s format expects null terminated strings, not just a single char, so printf continued until it hit a zero byte somewhere in the memory space after it.
0 u/frankis [OP] 20 Jun 2015 05:54
Here are two examples of times when it fails: 50 8
0 u/frankis [OP] 20 Jun 2015 10:27
For any interested, here's the just about final code I think I'll go with: http://pastebin.com/qQbyZwHB