GP32 Could Somebody Explain These C++ Concepts To Me?


Azure

Trust the recursion...
Joined
May 21, 2003
Messages
3,805
Location
California, USA
I'm having a little trouble understand a few things, so I decided that I would take a picture of what I don't understand from my C++ book.

Here's the first one:
function_prototype.jpg

function_prototype_2.jpg

In the first picture, the book says that in the full name, the second argument for the two functions is "int sizeOfloatArray." But in the second pic, when the functions are called, their second argument is "int sizeOfArray." What's up with this? Is this a typo or am I not understanding function arguments properly?

Here's the second one:
string.jpg

The middle paragraph confuses me. I get confused on which string he refers to in the paragraph. Which one (the string that's refers to an array or the type string) "includes operations for copying, concatenating, capitalizing, knotting," and which "one avoids the overrun problems inherent with null terminated strings?"

Next one:
truth_table.jpg

truth_table_2.jpg

Now I get confused on what these are. What are they? Are they bitwise operators? I thought the bitwise operators were &, |, etc. Second, are they really necessary? I can't think of how I would need them when writing a program. The way that the book used them is that it used them to "not" some hex numbers and output their value afterward. Is that their only use? So, do I really need to memorize these tables?

Thanks.
 

Attachments

  • function_prototype.jpg
    function_prototype.jpg
    34.5 KB · Views: 122
  • function_prototype_2.jpg
    function_prototype_2.jpg
    34.2 KB · Views: 130
  • truth_table.jpg
    truth_table.jpg
    39.8 KB · Views: 133
  • string.jpg
    string.jpg
    37.3 KB · Views: 125
  • truth_table_2.jpg
    truth_table_2.jpg
    30.4 KB · Views: 124
  • function_prototype.jpg
    function_prototype.jpg
    34.5 KB · Views: 126
  • function_prototype_2.jpg
    function_prototype_2.jpg
    34.2 KB · Views: 109
  • string.jpg
    string.jpg
    37.3 KB · Views: 129
  • truth_table.jpg
    truth_table.jpg
    39.8 KB · Views: 120
  • truth_table_2.jpg
    truth_table_2.jpg
    30.4 KB · Views: 128
  • function_prototype.jpg
    function_prototype.jpg
    34.5 KB · Views: 124
  • function_prototype_2.jpg
    function_prototype_2.jpg
    34.2 KB · Views: 133
  • string.jpg
    string.jpg
    37.3 KB · Views: 127
  • truth_table.jpg
    truth_table.jpg
    39.8 KB · Views: 123
  • truth_table_2.jpg
    truth_table_2.jpg
    30.4 KB · Views: 122
1. It's a typo.

2.
string: is an array of characters terminated by a null character. This is waht most of us are referring to when we talk about a string.
String type: This is a specific class in C++. It wraps the concept of a string in a class that provides many operations such as the ones referenced in the book.

3. Erm...VERY important. C/C++ have two sets of AND/OR operaters.

Bitwise Operaters:
| is the bitwise OR
& is the bitwase AND

Logical Operators:
|| is the logical OR
&& is the logical AND

The table that you are looking at refers to the logical operators. These are actually more common/importnat then their bitwase counterparts.

The logical operators can be thought of like this:
true and true is true
false and true is false
false and false is false
When using the logical AND if eny part of the expression is FALSE, the whole thing is FALSE

true or true is true
false or true is true
false or false is false
When using the logical OR if any part of the expression is TRUE, the whole thing is TRUE

The bitwise operators are not as important as the logical ones but are used fairly often. Usually with a bitmask. I probably would not worry about this too much until the first time you runinto one.
 
Went to Oakland (2 hour drive) and got a lot of reading done, but I was a little confused at some points. So, I wrote them down on a notepad so I would remember to post them immediately when I got home. Well, I forgot :D (I got home a little over 3 hours ago). This time, there are a lot more, I don't know why I didn't understand so many concepts during this reading. Maybe it was because it was one whole long reading, in a shaky mini-van going 80mph on a freeway, with the noise of everybody talking. Anyways, here they are:

1.
Function_Name.jpg

Function_Name_2.jpg

Ok, I think I may have grasped the concept of functions incorrectly. Are there set functions, like specific ones for doing specifc tasks? That's how I thought it worked, until I saw these two pics. They both are functions that square a variable, but one is called "echoSquare" and the other is called "square." What's up with this? So, there aren't specific functions, you just name them whatever you want, and the only thing you need to worry about are the function arguments, return type, and what's in the function body?

2.
demotion_promotion.jpg

Okay, what I don't get in this one is, why is dVar demoted instead of iVar being promoted? Why is it like this? The same with the one below it.

3.
array_vs_pointer.jpg

I don't understand why this one doesn't work. Could somebody explain? Also, what's up with the missing "&"? I was so used it being used to tell the address of a variable to a pointer, but now it isn't used in the book anymore. Could somebody also explain what's up with this? How can you tell a pointer an address without the "&"?

4.
terminating_character_loop.jpg

I also don't understand this one (Top paragraph and first line of code). The last sentence is what confuses me the most. I don't see how that would be justifiable. What would happen when it reaches stringarray[\0], it would just stop? The conditional doesn't make any sense at all.

5.
int_and_point.jpg

int_and_point2.jpg

Okay, I spent A LOT of time on this one and I still couldn't figure it out. I'm talking about the line of code in the first pic, and the first parapgrah and line of code in the second pic. How's &intvar assigned to int*? It's not a pointer, from what I can tell. The second pic, the first sentence doesn't make sense at all (along with the code, but of course, if I understand the first sentence, I'll understand the code). I agree with the first half of the sentence, but the second half just doesn't seem true.

6.
months_of_the_year.jpg

How's this work? I mean, would this code really work or is it missing something? How would the user input nMonth? Am I missing something here or would this just not work out?

7.
error.jpg

error2.jpg

I'm guessing this is a typo: The second to last sentence in the second paragraph of the second pic says that d is 8 bytes from it's neighbor, f. But, from the first pic, isn't d 2 bytes from it's neighbor? Was he trying to say that d is 8 bytes from n?

Thanks.
 

Attachments

  • Function_Name.jpg
    Function_Name.jpg
    40.1 KB · Views: 122
  • Function_Name_2.jpg
    Function_Name_2.jpg
    39 KB · Views: 117
  • demotion_promotion.jpg
    demotion_promotion.jpg
    40.4 KB · Views: 114
  • array_vs_pointer.jpg
    array_vs_pointer.jpg
    34.1 KB · Views: 131
  • terminating_character_loop.jpg
    terminating_character_loop.jpg
    39.2 KB · Views: 114
  • int_and_point.jpg
    int_and_point.jpg
    39.4 KB · Views: 121
  • int_and_point2.jpg
    int_and_point2.jpg
    34 KB · Views: 120
  • months_of_the_year.jpg
    months_of_the_year.jpg
    26.1 KB · Views: 121
  • error.jpg
    error.jpg
    43.1 KB · Views: 120
  • error2.jpg
    error2.jpg
    42.8 KB · Views: 120
  • Function_Name.jpg
    Function_Name.jpg
    40.1 KB · Views: 125
  • Function_Name_2.jpg
    Function_Name_2.jpg
    39 KB · Views: 121
  • demotion_promotion.jpg
    demotion_promotion.jpg
    40.4 KB · Views: 118
  • array_vs_pointer.jpg
    array_vs_pointer.jpg
    34.1 KB · Views: 131
  • terminating_character_loop.jpg
    terminating_character_loop.jpg
    39.2 KB · Views: 119
  • int_and_point.jpg
    int_and_point.jpg
    39.4 KB · Views: 122
  • int_and_point2.jpg
    int_and_point2.jpg
    34 KB · Views: 124
  • months_of_the_year.jpg
    months_of_the_year.jpg
    26.1 KB · Views: 127
  • error.jpg
    error.jpg
    43.1 KB · Views: 126
  • error2.jpg
    error2.jpg
    42.8 KB · Views: 119
  • Function_Name_2.jpg
    Function_Name_2.jpg
    39 KB · Views: 113
  • Function_Name.jpg
    Function_Name.jpg
    40.1 KB · Views: 123
  • demotion_promotion.jpg
    demotion_promotion.jpg
    40.4 KB · Views: 112
  • array_vs_pointer.jpg
    array_vs_pointer.jpg
    34.1 KB · Views: 125
  • terminating_character_loop.jpg
    terminating_character_loop.jpg
    39.2 KB · Views: 110
  • int_and_point.jpg
    int_and_point.jpg
    39.4 KB · Views: 115
  • int_and_point2.jpg
    int_and_point2.jpg
    34 KB · Views: 115
  • months_of_the_year.jpg
    months_of_the_year.jpg
    26.1 KB · Views: 123
  • error.jpg
    error.jpg
    43.1 KB · Views: 123
  • error2.jpg
    error2.jpg
    42.8 KB · Views: 117
1. You can create any functions you want with any names you want. However, the include files - stdio.h etc. - have preset functions with standard names. These names are standard so that every programmer, no matter what compiler is he is using, should in theory have access to the same standard set of functions. square and echoSquare are just two user-defined new functions. But the standard set of functions does include a pow function.
Usage to square a variable would be pow( some_var, 2 );
Most coders however just use some_var*some_var; for speed.

2. Gah, Hungarian notation. Well anyways, there is no demotion going on.

Code:
int iVar;
double dVar = 10.0;
iVar = (int)dVar;

Only conversion. dVar is the value 10.0 as a float. When you cast it as an int using (int), the compiler converts a copy it to an int of value 10 (obviously) and assigns that to iVar. iVar is now 10. dVar is still 10.0. dVar's value of 10.0 is 'demoted' in a way, I guess, as it gets changed to 10 (float -> int conversion).

The second is similar.

3. My pointer knowledge is a little rusty since I took on super-high-level languages, but I think *p = q is equivalent to p = &q. The reason the second example doesn't work involves some knowledge of memory allocation. char charArray[128] allocates 128 bytes of memory for the array. You now have 128 'slots' you can mess with. Hence messing with charArray[10] affects the 11th slot (remember, we start from 0).

charArray itself (without square brackets) is just a pointer to the start of that 128-byte block. When you do charArray[5], you are actually just calling *(charArray + 5) !! This is shown by the example. The statements charArray[10] = '0' and *(charArray + 10) are identical.

But when we just make a pointer without allocating memory, we can't really use it. char *pArray is what we called an uninitialised pointer, in that it points to nothing! What you would have to do is either:

char *pArray = malloc( 128 ); /* or something */
char *pArray = new char[128]; /* i think this works in C++ */

The former is more common. Then the pointer is assigned to something, and the array can be accessed. What's the point, then, you might say. Well this memory is dynamic. It is allocated by the malloc() function and can be freed by the ... er.. free() function. Whereas memory like char[128] is allocated all the time. It really helps when you want to load and unload stuff while the program is running. Hope this helps.

4. It makes sense, but it's a bit hackerish (optimisation over readability). A more correct conditional would be
Code:
for ( i = 0; stringArray[i] == '\0'; i++ )
But yeah, that's what it does. It iterates through the string until the char it's at is \0 (the end) and stops! The reason we can simplify the conditional is explained in the top paragraph. While we're in the string, every character is NOT going to be \0, so the conditional is true and we keep going. When we get to the end, it's going to be \0, and the conditional is false (remember, only \0 will evaluate as false).

There is no stringArray[\0]. We start with stringArray[0] and it isn't \0, so we keep going until we get to the end. For a string like 'marzipan', i would be 8 by the time we're finished. Such a loop could be used to get the length of a string, or convert the string to lowercase, or whatever.

5. More pointers, so I'll leave this one out. I think my p=&q thing is wrong in light of this, so I'm definately not qualified to comment on this particular question.

6. It's another user defined function. It needs to be called. I seriously suggest reading up on functions before you tackle pointers :S but here's what you would do elsewhere in the code:

char *monthName;
monthName = int2month( 12 );
printf( "%s", monthName );

// output: December

7. Hmm... d is 16 bytes away from n, though. Doubles take up more space than chars - 8 bytes to be precise. So if there is a variable AFTER a double, it SHOULD be 8 bytes away from it... (because 8 bytes are allocated for the double). However, I suspect typos, because not all of that makes sense to me.

Anyways this is more to save someone who actually knows what they're talking about, like Dalto, the trouble of doing all seven of your questions. And boredom. Hope it somewhat helped :)
 
Let me see if I can jump in and fill in a couple of the blanks.

Let's start with the easy one.

7. Any number that starts wih 0x is in HEX. So, if you convert 0x58 to decimal it is 88. and if you convert 0x60 to decimal it is 96. 96-88=8.

4. I completely agree with Rico on this one. While this clearly works, it is completely unintuitive and should not be in a teaching book. That is, unless its in a section entitled: Things you can do but you shouldn't.

5. Pointers:

So, a little theory first. Every variable is stored in memory. So when you declare a variable like this: int monkey; A section of memory is set aside for the variable monkey. This section of memory has an ADDRESS.

When you work with monkey, you are referring to the value of what is at that address where monkey is stored. So when you say monkey=1; Your program goes out to the address where monkey is stored and puts 1 there.

Pointers refer to the addresses instead of the values. So if you declare int *monkeyptr; It creates a pointer that points to the address of an integer value.

The unary & operator is called the "address of" operator. So it gives you the address of a variable. So &monkey refers to the address of monkey or the location in memory where it is stored.

Putting it all together monkeyptr=&monkey; Assign the monkeyptr to the address of monkey. Now the is one more operator we need to deal with: the *. monkeyptr would not be very useful unless you could get back to it's value somehow. When you have a pointer and you want to get back to it's value you "dereference" it. The * does this for you. So here we go:

Code:
int monkey;   // Declares a variable of type integer
int *monkeyptr;  // Declares of variable that is a pointer to an integer

monkeyptr=&monkey;  // Points monkeyptr to monkey
*monkeyptr=12;           //  Sets the value of the address that monkeyptr points to 12
cout << "The monkey is: " << monkey;

The output it is: The monkey is 12

Pointers can be compilcated at first and you will understand more the more you work with them but hopefully this helps a little.
 
Curses! I knew it was hex but did a decimal subtraction anyways. Thanks for that.

Just something that might help... I always read out (in my mind) the operators & and * as 'the address of' and 'the value at address'. It might make more sense to read *p as 'the value at address p'. Might :)
 
Actually, I already read the functions section and the pointer section very thoroughly. I think I understand functions pretty well too. I just got confused here and there. I already know how the operators * and & work, I just didn't know you could substitue *pointer = var1 for *pointer = &var1.

So, I still need number 5 and 6 (That code that you included, did you write that in C? Like, what's up with printf?) clarified.
 
Azure posted on Jul 28 2004 at 01:43 AM said:
Yeah at but still at where's the "cin" or where the users inputs in a number for the month?
There ya go! You do understand!
You have just made the connection that something is missing, and you know what it is too!

The function int2month() needs a parameter passing to it that specifies the month that is returned. Where that parameter comes from is up to you! .. user input, from a file, system time, etc...

The function that calls int2month() just isn't shown.


As for (5) I think it's a typo .. intVar is not a pointer to an int
That's where Hungarian notation has it's strength. A pointer to an int would be pintVar!
nb. Hungarian notation is a coding style, not a compiler syntax!

Take these books with a pinch of salt. If you think they're wrong -- it's prolly a typo <_<
Have more belief in yourself!


azure said:
I just didn't know you could substitue *pointer = var1 for *pointer = &var1.
You did mean = right? Pointer maths is never a good idea :|
This is valid C if *pointer points to the same type as &var.
Turns out it's a pointer to a pointer! Not the same as *pointer=var1
This is where C is not a beginners language -- you can get tied up with pointers. :wacko:
 
Last edited by a moderator:
Ah, thanks for the clarification guys! I'm such an idiot! :D

I didn't get to get anything C++ wise done today, so that means no questions...for now :)
I'm going to try creating my Part 2 Summary Program tomorrow, then I'll contine with part 3, which seems to be dedicated to "classes." (The book has 5 parts, well, really it's 4, because the 5th is just extra things about C++. So, a whole part dedicated to these things called "classes?" They must be very important! :D )
 
Azure posted on Jul 27 2004 at 11:02 PM said:
I just didn't know you could substitue *pointer = var1 for *pointer = &var1.
Just for the record. Those are NOT the same.

*pointer = var1: This assigns the value of var1 to the value of whatever address *pointer points to. This is probably a sensible thing to do.

*pointer = &var1: This assigns the address of var1 the value of whatever address *pointer points to. This is probably NOT a sensible thing to do.
 
Last edited by a moderator:
I thought the second one gives the address to the pointer and that's it. Or would the second one have to be worded without the splat: pointer = &var1, and then *pointer = var2 (Puts the value of var2 in var1)?
 
Azure posted on Jul 31 2004 at 05:04 PM said:
I thought the second one gives the address to the pointer and that's it. Or would the second one have to be worded without the splat: pointer = &var1, and then *pointer = var2 (Puts the value of var2 in var1)?
Exactly, you need to omit the * in the first expression. So, in your example:

pointer=&var1;
*pointer=var2;

As you said, the value of var2 is placed in var1. It's the same as: var1=var2;
 
Last edited by a moderator:
I emailed Stephen Davis about number 5. I don't know, it might be right. It might not be a typo because than means he made a typo in the sentence, in the code snippet afterward, and in the whole paragraph after that to help convey the message to the reader more clearly about what we think is a typo.
 
Azure posted on Jul 31 2004 at 08:57 PM said:
I emailed Stephen Davis about number 5. I don't know, it might be right. It might not be a typo because than means he made a typo in the sentence, in the code snippet afterward, and in the whole paragraph after that to help convey the message to the reader more clearly about what we think is a typo.
It's not a typo. The reason you need the * there is because he is declaring the variable. He is declaring an int pointer. It's confusing because * is used for so many things.

a*b --- multiplication
int *a -- pointer declaration
*a -- pointer dereferencing

as an example

int *a=&b;

is the same as
int *a;
a=&b;

but is NOT the same as
*a=&b;
 
Last edited by a moderator:
The way to understand that is to see the int *a as

int * a;

instead of

int *a;

You're making a variable of type 'int pointer' if you get my meaning. Else it looks like *a is a variable and you can mess with it etc. which may be true in some cases but is almost certainly going to lead to trouble.
 
Rico posted on Jul 31 2004 at 10:09 PM said:
The way to understand that is to see the int *a as

int * a;

instead of

int *a;

You're making a variable of type 'int pointer' if you get my meaning. Else it looks like *a is a variable and you can mess with it etc. which may be true in some cases but is almost certainly going to lead to trouble.
Hmm...it is easier logically but somewhat dangerous.

for instance:
what does this do?
int* a, b;

it declares a pointer a and an integer b because the * actually still refers to the a.
 
Last edited by a moderator:
Back
Top