2 Questions about the solution

So after a ton of trial and error, I figured out the answer. However, There are certain things I coded that I’m not sure why. I have posted the questions as comments inside my code. If anyone could help me answer these questions, I’d highly appreciate it!

         [b]//Why does "*question" have to be a pointer and not just a variable?[/b]
		const char *question = readline("Where should I start counting? ");
		NSString *questionInObjCString = [NSString stringWithUTF8String: question];
	[b]//How come "answeFromQuestion" does not have to be a pointer?[/b]

NSInteger answerFromQuestion = [questionInObjCString integerValue]; for (NSInteger i = answerFromQuestion; i >=0; i --) { NSLog(@"%ld", (long)i); if ((i % 5) == 0) { NSLog(@"Found one!"); } }

A1: This is an uncommon case, but this is because the ‘char’ type only stores a single character. Like the letter ‘a’. that’s it. What you’re doing here is declaring a variable that, instead of /containing/ a character, will be a reference to an array of characters somewhere else in memory. An array of chars is also called a C String. I strongly recommend having a look at the C Strings and C Arrays chapters at the back of the book for a deeper understanding of what’s going on here, but I think you’ll be fine if you save it until after you finish this part of the book.

A2: There are a small handful of types that Apple provides that /look/ like classes but aren’t, and are actually scalar / primitive types. NSInteger, for example, is an int on modern hardware. NSTimeInterval is a double. CGFloat is also a double on modern hardware. We generally don’t use pointers to these primitive types in our programs (except when passing by reference, as examined in the Pointers and Addresses and Passing By Reference chapters).

For what it’s worth, don’t feel discouraged. There is a lot of trial and error in programming. What’s important is that you then try to understand how certain answers work, as you are now.

[quote]//Why does “*question” have to be a pointer and not just a variable?

const char *question = readline ("Where should I start counting? ");
NSString *questionInObjCString = [NSString stringWithUTF8String: question];

[/quote]
The readline function returns a value, which is the address of the first byte of a buffer in memory containing the characters read from the input. You can assign that value only to a variable of type either char * or void *. That’s why the variable question has to be a pointer to char.

The readline function reads a sequence of characters from the input, stores them in a buffer in memory, and returns not the sequence of characters but the address of that buffer. The reason it returns the address of the buffer, not the characters, is that the number of characters read from the input can exceed the storage capacity associated with a variable of primitive type, such as char, int, long, etc.

Similarly, the method call [questionInObjCString integerValue] returns a value, which is stored in a memory buffer consisting of a fixed, small number of bytes. That value can fit in the buffer represented by the variable answerFromQuestion of type NSInteger. That’s why the variable answerFromQuestion is not a pointer.

As implied above, all variable names are actually names of buffers in memory. For example:

double result;
long   offset;
char   key;
char * string;

The variable names result, offset, key, and string are names of buffers in memory. The size of a buffer is determined by the type of the corresponding variable. For example, on my machine’s architecture, both result and offset each takes 8 bytes in memory; key takes 1 byte, and string takes 8 bytes. Note that unlike the other buffers, the string buffer is used to store the address of a sequence of characters.

This was all very helpful and I understood a lot thanks to both of you.

Thank you very much! :stuck_out_tongue: