C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 7,804
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #41
Mar 26th, 2005
>1. If row and col wasn't being initialized was it being Declared?
Defined, actually. But declarations and definitions can be tricky. A definition is always a declaration, but a declaration is not always a definition.

In C it gets so confusing that a definition may look like a definition but still only be a declaration. However, you can save learning that until you want to consider yourself an expert in C.

>2. and why is it that row and col wasn't initialized, but rptr is?
Because row and col aren't assigned an explicit value while rptr is (through malloc's return value).

>3.
malloc returns a block of memory. Memory is just a bunch of bytes all stuck together, and malloc returns a block of N bytes where N is a value that you pass to it. Assuming COLS is 10 and sizeof ( int ) is 4, you would be asking for 400 bytes, or 10 * 10 * 4. malloc doesn't care how you use the memory, it returns the equivalent of a pointer to unsigned char. When you assign the block to a pointer of type int, you can treat the memory as a sequence of 100 integers, or 10 * 10 because even though an int is 4 bytes, we're now treating it as a single unit.

The same thing would happen if rptr were of type double. Assuming sizeof ( double ) is 8, you would be allocating 800 bytes, but still treating that block as a sequence of 100 units (this time of type double).

>4. Is my understanding of how a nested for loop works correct?
You're overcomplicating things. The outer loop executes the inner loop. So if the inner loop initializes one row, the outer loop initializes all of the rows.

>if you don't give it an integer value your program could start acting up?
If you don't return a value then you've invoked undefined behavior, and that's bad. However, the rules are different depending on what version of C or C++ you're using.

>is int main(void) kind of an exception to the rule where a value ISN'T needed therefore
No, don't confuse parameters with return values. int main(void) simply means that main takes no arguments, but it still must return an integer.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #42
Mar 28th, 2005
Originally Posted by Narue
>1. If row and col wasn't being initialized was it being Declared?
Defined, actually. But declarations and definitions can be tricky. A definition is always a declaration, but a declaration is not always a definition.

In C it gets so confusing that a definition may look like a definition but still only be a declaration. However, you can save learning that until you want to consider yourself an expert in C.

>2. and why is it that row and col wasn't initialized, but rptr is?
Because row and col aren't assigned an explicit value while rptr is (through malloc's return value).

>3.
malloc returns a block of memory. Memory is just a bunch of bytes all stuck together, and malloc returns a block of N bytes where N is a value that you pass to it. Assuming COLS is 10 and sizeof ( int ) is 4, you would be asking for 400 bytes, or 10 * 10 * 4. malloc doesn't care how you use the memory, it returns the equivalent of a pointer to unsigned char. When you assign the block to a pointer of type int, you can treat the memory as a sequence of 100 integers, or 10 * 10 because even though an int is 4 bytes, we're now treating it as a single unit.

The same thing would happen if rptr were of type double. Assuming sizeof ( double ) is 8, you would be allocating 800 bytes, but still treating that block as a sequence of 100 units (this time of type double).

>4. Is my understanding of how a nested for loop works correct?
You're overcomplicating things. The outer loop executes the inner loop. So if the inner loop initializes one row, the outer loop initializes all of the rows.

>if you don't give it an integer value your program could start acting up?
If you don't return a value then you've invoked undefined behavior, and that's bad. However, the rules are different depending on what version of C or C++ you're using.

>is int main(void) kind of an exception to the rule where a value ISN'T needed therefore
No, don't confuse parameters with return values. int main(void) simply means that main takes no arguments, but it still must return an integer.

I got it! Thank you for answering all my questions, I think we're fine tuning my understanding now. I only have a couple questions

1. >No, don't confuse parameters with return values. int main(void) simply means that main takes no arguments, but it still must return an integer

Im glad this came up, because now we are at a point where what i thought i knew i actually don't

it was my understanding that a function's parameter was the variable (like, i) and the argument was the return value of the variable (like, 10). If int main(void) means main takes no arguments but must still return an integer, wouldn't the integer be a value (i.e. an argument)??? I'M CONFUSED

another thing that has me all confused was this

2. >You're overcomplicating things. The outer loop executes the inner loop. So if the inner loop initializes one row, the outer loop initializes all of the rows.

^^^I'm with you about the outer loop executing the inner. I don't know what you mean after that.

Does the outer loop create row? And for every row created by the outer loop does the inner loop then creates 10 col's (because we said COLS is 10)? Also, is 17 the value placed in every address of created by a[row][col]?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,433
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #43
Mar 28th, 2005
Regarding parameters and return values: would an example like this help distinguish the two?
  1. #include <stdio.h>
  2.  
  3. int lengthof ( const char *parameter )
  4. {
  5. int len = 0;
  6. while ( parameter[len] != '\0' )
  7. {
  8. ++len;
  9. }
  10. return len; /* len is the return value */
  11. }
  12.  
  13. int main ( void )
  14. {
  15. const char text[] = "a text string";
  16. int length = lengthof ( text ); /* text is the argument to lengthof */
  17. printf ( "the string \"%s\" has a length of %d\n", text, length );
  18. return 0;
  19. }
  20.  
  21. /* my output
  22. the string "a text string" has a length of 13
  23. */
If you write a function to count the characters in a string (as above), you'd probably pass a pointer the string and return a count of the number of characters.

If write a cube root function, you'd probably pass some value, calculate the cube root, and then return the result.

The parameter(s) is what the function gets to do whatever it is the function does. The return value is whatever the value returns. The relationship between the two depends on what the function does.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #44
Mar 29th, 2005
Originally Posted by Dave Sinkula
Regarding parameters and return values: would an example like this help distinguish the two?
  1. #include <stdio.h>
  2.  
  3. int lengthof ( const char *parameter )
  4. {
  5. int len = 0;
  6. while ( parameter[len] != '\0' )
  7. {
  8. ++len;
  9. }
  10. return len; /* len is the return value */
  11. }
  12.  
  13. int main ( void )
  14. {
  15. const char text[] = "a text string";
  16. int length = lengthof ( text ); /* text is the argument to lengthof */
  17. printf ( "the string \"%s\" has a length of %d\n", text, length );
  18. return 0;
  19. }
  20.  
  21. /* my output
  22. the string "a text string" has a length of 13
  23. */
If you write a function to count the characters in a string (as above), you'd probably pass a pointer the string and return a count of the number of characters.

If write a cube root function, you'd probably pass some value, calculate the cube root, and then return the result.

The parameter(s) is what the function gets to do whatever it is the function does. The return value is whatever the value returns. The relationship between the two depends on what the function does.
Okay Dave help me out!!!!!!!!!!!

  1.  
  2. #include <iostream>
  3.  
  4. int myfunction(int parameter1, int parameter2)
  5. {
  6. return parameter1 + parameter2;
  7. }
  8.  
  9. int main(void)
  10. {
  11. myfunction(argument1, argument2);
  12.  
  13. int argument1 = 4, argument2 = 5;
  14.  
  15. return 0;
  16. }
  17.  
  18.  
  19.  
  20. /*"ComeauTest.c", line 10: error: identifier "argument1" is undefined
  21.   myfunction(argument1, argument2);
  22.   ^
  23.  
  24. "ComeauTest.c", line 10: error: identifier "argument2" is undefined
  25.   myfunction(argument1, argument2);
  26.   ^
  27.  
  28. "ComeauTest.c", line 12: warning: variable "argument1" was declared but never
  29.   referenced
  30.   int argument1, argument2;
  31.   ^
  32.  
  33. "ComeauTest.c", line 12: warning: variable "argument2" was declared but never
  34.   referenced
  35.   int argument1, argument2;
  36. */

Why doesn't this compile, what am I doing wrong?

I can get it down to 2 errors, but I left it up like this for Narue to help understand the difference between declaring and identifying an object.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #45
Mar 29th, 2005
Hello,

I see a problem with the following code:
int main(void)
{
myfunction(argument1, argument2);

int argument1 = 4, argument2 = 5;

return 0;
}
Simple. You create your variables after you try and pass them to myfunction(). Rather, your code should be:
int main(void)
{
int argument1 = 4, argument2 = 5;

myfunction(argument1, argument2);

return 0;
}
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #46
Mar 29th, 2005
Originally Posted by Stack Overflow
Hello,

I see a problem with the following code:
int main(void)
{
myfunction(argument1, argument2);

int argument1 = 4, argument2 = 5;

return 0;
}
Simple. You create your variables after you try and pass them to myfunction(). Rather, your code should be:
int main(void)
{
int argument1 = 4, argument2 = 5;

myfunction(argument1, argument2);

return 0;
}
- Stack Overflow

Thanks for stepping in Stack!!!!

It makes sense

I really appreciate it

Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC