943,584 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 52925
  • C++ RSS
You are currently viewing page 5 of this multi-page discussion thread; Jump to the first page
Mar 26th, 2005
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 28th, 2005
0

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

Quote 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]?
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 28th, 2005
0

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

Regarding parameters and return values: would an example like this help distinguish the two?
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 29th, 2005
0

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

Quote originally posted by Dave Sinkula ...
Regarding parameters and return values: would an example like this help distinguish the two?
C++ Syntax (Toggle Plain Text)
  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!!!!!!!!!!!

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 29th, 2005
0

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

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Mar 29th, 2005
0

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

Quote 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

Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Jul 26th, 2010
-1
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
plz explain call by reference also
Reputation Points: 9
Solved Threads: 0
Newbie Poster
A.Rehman Amjad is offline Offline
2 posts
since Jul 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Password verification C++
Next Thread in C++ Forum Timeline: Creating set as a tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC