I feel so lost now after page 63


#1

I felt I was understanding everything up to chapter 7. Then chapter 8 came in and I didn’t really understand much of it and then chapter 9’s example on page 63 is really messing with my head. I don’t even know what questions to ask. If someone wants to try and explain it in the easiest terms to understand it would be appreciated if not its all good. I just can’t seem to wrap my head around these last 2 chapters.


#2

The awful and great thing about pointers is that you can’t fake your way through them.

My advice? Go back to chapter 8 and don’t leave it until you feel confident about it. Use chapter 9 as a gauge for whether you really understand chapter 8.

If you need more material, use your favorite search engine and look for help. There are plenty of tutorials out there; work through all of them until the concept really sinks in.

A few samples:
[ul][li]http://boredzo.org/pointers/[/li]
[li]http://pw1.netcom.com/~tjensen/ptr/pointers.htm[/li]
[li]http://www.codeproject.com/Articles/627/A-Beginner-s-Guide-to-Pointers[/li]
[li]http://cplus.about.com/od/learningc/ss/pointers.htm[/li][/ul]

I’m inclined to think that writing code is the best way to learn to write code, so don’t be shy about experimenting with the examples in the book, and ask here for more challenge problems to solve.


#3

What exactly made you feel lost?
Is Objective-C the first programming language you are learning?


#4

Yes this is the first programming language I am learning and I am trying to teach myself it. I really want to do iPhone programming and I even bought a laptop just for this book lol. It was all the money I had but I had to take a chance on myself. I wish there were quizzes or tests I could take after the chapters as practice. But I am gonna reread it again and try to make sense of it. I feel like there was a lot of new stuff in that example on pg. 63.


#5

C is a tough gig for a first programming language, no doubt about it.

If you get too frustrated, you might try Code Year; it’s an online experiment in learning to code interactively in your web browser, with new lessons on a weekly basis (IIRC). Even as an experienced programmer I found it of some value, and as a new programmer you might find it to be a more gentle introduction.

It won’t, however, directly assist with the problem of pointers, merely let you get more comfortable with basic programming concepts before you try to tackle them again.


#6

If that’s the case, don’t rush. Take your time and learn basics first; it will pay off.

Read this book first: "The C Programming Language, Brian W. Kernighan and Dennis M. Ritchie."
And make sure that you firmly understand:

  • functions;
  • structs;
  • unions;
  • sizeof operator;
  • arrays;
  • malloc (), free (), memcpy (), strcpy (), printf, sprintf, etc.
  • pointers (of all kinds);
  • null-terminated char arrays.

As you read the book, use of Xcode and create toy projects for practice. Because Xcode is both good and bad for you, also learn some basic command line skills: compiling and linking programs, editing program files using the vi editor, etc.

And come back to Objective-C. The good news is that there really is no big difference between C and Objective-C, apart from the syntax and the run time.


#7

And, of course, don’t hesitate to come back to us with questions!

Aaron and I don’t get to spend as much time answering questions as we’d like (but we are around and dedicate some time to it!), but this forum is a flourishing community with folks eager to help, such as ibex10, macintux, and many others whose names escape me at the moment (sorry!).


#8

Thanks guys I appreciate everything it means a lot. Im looking forward to the day that I get to post and thank the members of the community and the authors of the book for learning this. I suppose I will check all the links y’all posted and go back over the chapter again since I have taken a break for a few days.


#9

I, too, felt so lost when I got to this. I am also new at programming and have never done this before. It really reminds me of the old days working on computers with the green screen and the c:\ prompt (not sure what that’s called). Does that give you an idea of how old I am??? I’m trying really hard to wrap my head around it because I love everything to do with computers and have waited for apps to come out that just haven’t so I thought . . . “I’ll just make them myself! I’m retired! I have time!”. I don’t know if Cluvdowns has figured it out yet but I found this explanation that helped me. Check out this link:

alumni.cs.ucr.edu/~pdiloren/C++_ … /index.htm

Hope this helps someone like it did me!


#10

[quote=“dkbateman”]I, too, felt so lost when I got to this. I am also new at programming and have never done this before. It really reminds me of the old days working on computers with the green screen and the c:\ prompt (not sure what that’s called). Does that give you an idea of how old I am??? I’m trying really hard to wrap my head around it because I love everything to do with computers and have waited for apps to come out that just haven’t so I thought . . . “I’ll just make them myself! I’m retired! I have time!”. I don’t know if Cluvdowns has figured it out yet but I found this explanation that helped me. Check out this link:

alumni.cs.ucr.edu/~pdiloren/C++_ … /index.htm

Hope this helps someone like it did me![/quote]

This did indeed help me, dkbateman. Thank you. I’ve got a better grasp on what exactly each of the new notations do now.

The basic failing of this chapter (and even of the tutorial you pointed out to some degree) is that there is almost no mention of WHY you would want to use pointers. For me, learning the WHY behind a particular concept is pretty key to understanding what it’s all about. I’m still not sure I’m there. For instance, I now understand that I could code the following:

int MartyMcFly = 1985; int *DocBrown; DocBrown = &MartyMcFly; printf("MartyMcFly's year of origin is %d.\n", *DocBrown);

And the results would be:

MartyMcFly’s year of origin is 1985.

By why on earth would I ever need to do this, instead of simply saying:

int MartyMcFly = 1985; printf("MartyMcFly's year of origin is %d.\n", MartyMcFly);

or even (assuming I had some other use for the DocBrown variable):

int MartyMcFly = 1985; int DocBrown = MartyMcFly; printf("MartyMcFly's year of origin is %d.\n", DocBrown);

I’m sure pointers are very powerful tools, but right now I’m just not seeing it. What can I do with pointers and addresses that I can’t do more simply by referring to the variables directly? I guess maybe I’ll figure it out as the book progresses, but until then, it feels like I’m missing a key ingredient to true understanding.


#11

Indeed, pointers are a bit like the Calculus of computer science. It takes a great deal of work and review and understanding to truly understand them, and even long after you’ve mastered them you’ll discover quirks and implementation details here and there that will blow your mind.

If you’ll permit me, I’d like to paste a quote from Chapter 2: [quote]Learning to program a computer is also difficult learning. You will be baffled from time to time – especially here at the beginning. This is fine. In fact, it’s kind of cool. It is a little like being six again.
Stick with this book; I promise that the bewilderment will cease before you get to the final page. (p.15)[/quote]

Pointers are difficult. It’s also difficult to learn about a tool without first learning about how the tool is to be used. I beg you to bear with us, however. The why is coming.

That said, in this particular case, we do need to see that the tool exists before we see what it can do for us, because the purpose they serve can only be served by pointers. Explaining the why first would be like trying to learn about masonry without first learning of the existence of stone.

The Marty McFly example you provide is, as you suspect, totally useless as written. Pointers are not used for handling variables within a function. They’re used for variables whose values need to be available outside the function, even after the function returns and the variables don’t exist to store them anymore. This is a difficult concept, and will take some re-reading and exploration to fully understand.

Bear with it. Get through some of the chapters on objects. Ask questions as you go. Refer back to the pointers chapter as necessary. We’re here to help.


#12

The chapter also probably misses a beat by using the Cartesian to Polar conversion as its example. If you’re going to use mathematical formulas as the basis for examples in your book, you’ve got a couple of choices, as I see it:

  1. Stick with formulas that the vast majority of your audience are going to be already familiar with. (For example, the area of a circle, or the pythagorean theorem. These are formulas that most everyone has at least heard of, even if they can’t necessarily recall them off the top of their head).

  2. Explain the formulas before you launch into the code. (Okay, maybe you really wanted to include this Cartesian to Polar example. Before we even see line one of code, you’ve got to make it clear that r = the square root of ( x squared + y squared ) and theta = the inverse tangent of ( y / x ). You probably should go so far as to explain what the inverse tangent is, or at least the name of the function used to compute it.

This is all just my humble opinion. I’m not a math slouch, but still, it’s been quite a few years since I had to use any trigonometric functions. In my opinion, in a book on programming, my time is going to be best spent trying to figure out the code I’m looking at. That’s going to be a lot more difficult if I’m trying to figure out the math at the same time. Doubly so if I have little to no background in trig.


#13

This chapter killed me. I went through it a second time and realized it’s not the programming and understanding pointers I have trouble with, it’s the math.

A lot of the math in this chapter is new or long forgotten. Theta, radians, sine? I couldn’t tell you what they mean if my life depended on it.

So, I’m just moving forward. I know I’m not going to be a master programmer after just one book, but everything I read will add up and it all helps.


#14

Can I suggest to remove the code about fractionPart completely on page 63, except the #include <math.h> on the exercise on polar to Cartesian coordinates?
That would clear up some page space to give a bit more explanation on what the code does.
I agree that the math in here was far from being at the ready in my brain, so I just let go and tried to follow the logic of what the function was returning,and where were the ones passed by reference?
Thanksgiving is coming, so maybe we could build up an example of a function returning values for time to defrost and time to cook when being given the weight of the turkey as input: both defrost and cook time being passed by reference. :slight_smile: Recycling here!
On to structures now.


#15

We thank you all for this valuable feedback. We’ll certainly take it into consideration for the next edition of the book.

Of course, I want to help as much as I can with any hurdles you encounter, so please continue posting in these forums with any questions that come up!

Happy coding!


#16

I have to agree with most peoples complaints about using overall “complicated math” as your examples. As the reader trying to understand this book, I found myself going out of my way (google, wikipedia, etc…) trying to understand the math behind your examples, when I realized I was kinda wasting my time since I am not trying to learn math here but programming.

I’ve had to go back and forth between three or four c programming books because of stuff like this.

I believe it would be better to use other, more simple examples, for those of us who are not too keen on math.


#17

OK, your complaint about math is noted. I’ll try to find another simple example for the next edition.


#18

Does anyone have a simple example of a pointer? I feel that I’m somewhat understanding it but if I can see an example where you would actually need to use it, unlike steelhorse’s martymcfly example, it would help.


#19

Here is a simple example.

A function passing information to its caller through a pointer - pointer to a pointer to char.

We call a function to delete a file. If the file could not be deleted, the function gives us the reason by passing the reason text through a pointer argument.

//  main.m

#import <Foundation/Foundation.h>

typedef const char * reason_type;

// Delete a file
// Return YES if deletion has occurred, otherwise set the reason and return NO
//
BOOL DeleteFile (const char *fileName, reason_type *reason);

int main (int argc, const char * argv[])
{
    const char *file = "~/tmp/junk";
    reason_type reason;
    BOOL status = DeleteFile (file, &reason);
    if (!status)
        printf ("Error: unable to delete the file: %s: reason: %s", file, reason);
    else
        printf ("Deleted the file: %s", file);
    
    return 0;
}

BOOL DeleteFile (const char *fileName, reason_type *reason)
{
    reason_type NOTEXISTS     = "File does not exist";
    reason_type DIRECTORY     = "It's a directory file";
    reason_type NOPERMISSION  = "NO permission to delete";
    
    BOOL status = NO;
    
    // simulate the status of the delete operation;
    // we don't really one to delete any files in this example.
    const long RC = arc4random_uniform (4);
    if (RC == 0)
        *reason = NOTEXISTS;
    else if (RC == 1)
        *reason = DIRECTORY;
    else if (RC == 2)
        *reason = NOPERMISSION;
    else
    {
        status = YES;
    }
    
    return status;
}

The above example uses a pointer to a pointer to char: that is, a char**.

if you look closely, the main function is passing the address of a pointer-to-char variable to the DeleteFile function:
BOOL status = DeleteFile (file, &reason);

Here is another example.

Pointer to a function.

This example is based on the previous example, but it uses a pointer to a function instead of a pointer to a pointer to char. The DeleteFile function’s second argument is a pointer to a function. If the file could not be deleted, the DeleteFile function calls the function whose address is passed via the pointer.

//  main.m

#import <Foundation/Foundation.h>

typedef const char * reason_type;
typedef const char * file_name_type;

// A typename for a pointer to a function taking two args
//
typedef void (*delegate_type) (file_name_type, reason_type);

// Delete a file
// Return YES if deletion has occurred, otherwise invoke the delegate with the reason
// and return NO
//
BOOL DeleteFile (file_name_type fileName, delegate_type delegate);

void printReason (file_name_type, reason_type);

int main (int argc, const char * argv[])
{
    file_name_type file = "~/tmp/junk";
    BOOL status = DeleteFile (file, printReason);
    if (status)
        printf ("Deleted the file: %s", file);
    
    return 0;
}

BOOL DeleteFile (file_name_type fileName, delegate_type delegate)
{
    reason_type NOTEXISTS     = "File does not exist";
    reason_type DIRECTORY     = "It's a directory file";
    reason_type NOPERMISSION  = "NO permission to delete";
    
    reason_type reason;
    BOOL status = NO;
    
    // simulate the status of the delete operation;
    // we don't really one to delete any files in this example.
    const long RC = arc4random_uniform (4);
    if (RC == 0)
        reason = NOTEXISTS;
    else if (RC == 1)
        reason = DIRECTORY;
    else if (RC == 2)
        reason = NOPERMISSION;
    else
    {
        status = YES;
    }
    
    if (!status)
    {
        // operation failed, call the delegate
        delegate (fileName, reason);
    }
    return status;
}

void printReason (file_name_type fileName, reason_type reason)
{
    printf ("%s: Error: unable to delete the file: %s: reason: %s", __PRETTY_FUNCTION__, fileName, reason);    
}

This example has used a pointer to a function - void ()(const char, const char*).

Again if you look closely, the main function is passing the address of a function (printReason) to the DeleteFile function:
BOOL status = DeleteFile (file, printReason);

You should run these examples in Xcode by creating a command-line tool project. Please do not hesitate to ask for clarification if there is anything that is unclear or which you don’t understand.


#20

Thanks ibex. Those examples and some videos I’ve found are really helping. I think now I just need to go over the * and & to remember what they mean.

So the * is saying that is a pointer.

The & is saying that it wants the address of the variable.

Is that correct?