- Debugger: Getting mixed signals
- 'Packaging Infrastructure' Contract Weekly Report #4
- Haiku monthly activity report - 06/2015
- 'Packaging Infrastructure' Contract Weekly Report #3
- 'Packaging Infrastructure' Contract Weekly Report #2
- GCI 2014 winners trip report (mentor side)
- TeX Live and LyX; Changes to the boot code
- 'Packaging Infrastructure' Contract Weekly Report #1
- Beginning of 'Packaging Infrastructure' Contract
- Haiku monthly activity report - 05/2015
Lesson 5: Arrays, Strings, and Pointers
It's that time again, continuing in the journey from No Code to Know Code. This lesson marks the end of the first unit and is somewhere around halfway from complete neophyte to writing -- and understanding -- our first program for Haiku which uses windows and buttons. Learning to Program with Haiku, Lesson 5.
- darkwyrm's blog
- Login or register to post comments

Comments
Re: Lesson 5: Arrays, Strings, and Pointers
Thanks for the new lesson!
There are three little typos etc. you may want to correct:
// a regular variable and this would generate a compiler error. The
I'm afraid I haven't yet found out why you write in the project at the end: "When the user
finishes typing, gets() returns the string that was passed to it." [char * gets(char *inString);], but in the code, there's no return value of the gets() function [gets(inString);].
I was under the impression that gets() takes a pointer to our string (= char array) as parameter and writes the user input into it, not having any return value. That is, like it's done in the code.
Thanks!
Humdinger
Re: Lesson 5: Arrays, Strings, and Pointers
Thanks for the new lesson!
There are three little typos etc. you may want to correct:
I'm afraid I haven't yet found out why you write in the project at the end: "When the user
finishes typing, gets() returns the string that was passed to it." [char * gets(char *inString);], but in the code, there's no return value of the gets() function [gets(inString);].
I was under the impression that gets() takes a pointer to our string (= char array) as parameter and writes the user input into it, not having any return value. That is, like it's done in the code.
Thanks!
Humdinger
It doesn't make much sense, but it's not anything to worry about -- in a few lessons, we won't be using it anymore, anyway. You can safely ignore the return value. Thanks for the errata. It seems like no matter how many times you proofread, there are still mistakes which slip through. I've corrected them.
// Declare the array itself, which contains 5 elements.
int intArray[5];
That would actually be an array of 6 elements 0-5 unless you actually count from zero in which case you may very well be brain damaged lol
Overall I really like these tutorials I will be recommending them to friends.
I'm glad to hear it! In that example, though, there really are 5 elements in that array and they're numbered 0-4. It really is brain damaged, if you ask me.
Re: Lesson 5: Arrays, Strings, and Pointers
In that example, though, there really are 5 elements in that array and they're numbered 0-4. It really is brain damaged, if you ask me.
It makes more sense once you know why.
As in most things in C/C++, it all comes down to pointers. A pointer is just a number that is an address in memory. It's perfectly possible to declare a pointer and then go "*(pointer + 1)" to address the next memory address. (Whether the compiler will actually let you do that varies based on where in memory your variables are stored and things like that.) The reference operator (the star) simply takes whatever address is given it and tries to look up the value in that spot.
When you say "int array[5];", you are actually saying "make five ints in a row and give me a pointer named 'array' to the first one". "array[0] = 5;" and "*array = 5;" actually mean the same thing. What the subscript operator (the brackets) does is add the number within the brackets to the address the pointer is referencing. Thus, referencing "array[3]" means "start at the array's starting location, step forward three times, and give me the value", or in pointer terms, "*(array + 3)".
Because of all that, an array starts at "*(array + 0)" and ends at "*(array + 4)". Since we only declared five ints, we don't have any more ints to reference, so there's nothing at the address "array + 5", which is why the program crashes.
All this was originally decided back when programmers managed their own memory. They would get it in chunks from the "malloc" function with a pointer to the first space in the chunk, and then they could reference the rest of the chunk by adding to that pointer. Arrays were created as shortcuts to make it all easier.
Re: Lesson 5: Arrays, Strings, and Pointers
// Declare the array itself, which contains 5 elements.
int intArray[5];
That would actually be an array of 6 elements 0-5 unless you actually count from zero in which case you may very well be brain damaged lol
Overall I really like these tutorials I will be recommending them to friends.
Re: Lesson 5: Arrays, Strings, and Pointers
Thanks for all these classes, I have learned more with these course than with many other books and classes.