| | |
C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
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]?
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? 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.
C++ Syntax (Toggle Plain Text)
#include <stdio.h> int lengthof ( const char *parameter ) { int len = 0; while ( parameter[len] != '\0' ) { ++len; } return len; /* len is the return value */ } int main ( void ) { const char text[] = "a text string"; int length = lengthof ( text ); /* text is the argument to lengthof */ printf ( "the string \"%s\" has a length of %d\n", text, length ); return 0; } /* my output the string "a text string" has a length of 13 */
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
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
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?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.C++ Syntax (Toggle Plain Text)
#include <stdio.h> int lengthof ( const char *parameter ) { int len = 0; while ( parameter[len] != '\0' ) { ++len; } return len; /* len is the return value */ } int main ( void ) { const char text[] = "a text string"; int length = lengthof ( text ); /* text is the argument to lengthof */ printf ( "the string \"%s\" has a length of %d\n", text, length ); return 0; } /* my output the string "a text string" has a length of 13 */
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> int myfunction(int parameter1, int parameter2) { return parameter1 + parameter2; } int main(void) { myfunction(argument1, argument2); int argument1 = 4, argument2 = 5; return 0; } /*"ComeauTest.c", line 10: error: identifier "argument1" is undefined myfunction(argument1, argument2); ^ "ComeauTest.c", line 10: error: identifier "argument2" is undefined myfunction(argument1, argument2); ^ "ComeauTest.c", line 12: warning: variable "argument1" was declared but never referenced int argument1, argument2; ^ "ComeauTest.c", line 12: warning: variable "argument2" was declared but never referenced int argument1, argument2; */
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.
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: Simple. You create your variables after you try and pass them to myfunction(). Rather, your code should be: - Stack Overflow
I see a problem with the following code:
int main(void)
{
myfunction(argument1, argument2);
int argument1 = 4, argument2 = 5;
return 0;
}int main(void)
{
int argument1 = 4, argument2 = 5;
myfunction(argument1, argument2);
return 0;
} 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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
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:Simple. You create your variables after you try and pass them to myfunction(). Rather, your code should be:int main(void) { myfunction(argument1, argument2); int argument1 = 4, argument2 = 5; return 0; }- Stack Overflowint main(void) { int argument1 = 4, argument2 = 5; myfunction(argument1, argument2); return 0; }
Thanks for stepping in Stack!!!!
It makes sense
I really appreciate it
![]() |
Other Threads in the C++ Forum
- Previous Thread: Exercise using: unsigned int datecode(int year, int month, int day);
- Next Thread: Break Out Of A Function~
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






