>int main(void) //means int main() will not pass any arguments (I'm still not
>clear on what an argument is though)
main will not accept any arguments. When it comes to functions, you will hear two terms most often: argument and parameter. An argument is the value passed to a function in a function call and a parameter is the declaration of a local variable that holds an argument in a function declaration.
void f ( int i ); // i is a parameter
f ( 10 ); // 10 is an argument
>//initializes objects row and col as integers
Neither row nor col are initialized.
>rptr = malloc(nrows * COLS * sizeof(int));
>//initializes rptr to I DON'T KNOW...help!
Initializes rptr to the first address of a block of memory (nrows * COLS * sizeof(int)) bytes.
>what is the value of COLS???
I have no idea, but the name suggests a macro. Something like this, perhaps:
>what happens when a double array is set equal to some value (i.e. 17)?
You're not setting the array to anything. You're setting the integer located at rptr[row][col] to 17.
>//to this day I don't know what that means
It's simple. Think of the C run-time environment calling your main function like so:
int rc = main();
/* Do something with rc */
If main doesn't return a value then rc will remain uninitialized, and all kinds of bad things may happen if an uninitialized variable is used.
This is great, I mean to you this is elementry, but I'm getting it, you know :mrgreen:
I do have a couple questions though...but first thank you for the breakdown of parameters and arguments...i've read books, asked college professors, and your example was the one...thanks to you and can't forget Dave I GOT IT NOW! I understand what "int main (void)" means and what rptr[row][col] = 17 means (just to think a couple days ago i didn't even understand arrays, let alone double arrays (go here: CLICK)
1. If row and col wasn't being initialized was it being Declared?
2. and why is it that row and col wasn't initialized, but rptr is?
3.
rptr = malloc(nrows * COLS * sizeof(int));
okay not let me see if i understand what malloc() is doing, tell me if im wrong, but going by what you and dave said:
You request from malloc space for 10 objects each of which are sizeof(int) bytes in size -- you ask for space for 10 ints.
rptr is the lvalue (i.e. address) of an object with a rvalue of memory that is going to be dynamically allocated in "blocks" using the function malloc(). Now with nrows, we are requesting 10 spaces (because nrows = 10) from malloc sizeof(int) which is 4 bytes for every integer (using a 32 bit Pentium processor). The same goes for COLS, but we don't how many spaces is being requested, because COLS could be a marco, meaning an alias.
4. Is my understanding of how a nested for loop works correct?
Note: lets say COLS = 20 for sake of argument.
(a)Does the 1st for loop create a row (i.e. row = 0), then moves to second for loop?
(b)Does the 2nd for loop create a col (i.e. col = 0), then sets the integer located at rptr[0][0] to 17?
(c)Does the second for loop create another col (i.e. col = 1), then sets the integer located at rptr[0][1] to 17?
(d)Does this go on until 20 col
's are created for row 0, then goes back to the 1st for loop and does the same for row 1 (i.e. row = 0)?
I guess what im saying would kind of llike this:
1st CYCLE: rptr = [0][0] = 17, then rptr=[0][1] = 17,........, then rptr=[0][19]= 17, end cycle
2nd CYCLE: rptr = [1][0] = 17, then rptr=[1][1] = 17,........,then rptr=[1][19], end cycle
.............and so on and so forth
5. Narue, your example of why int main() must return a value, is saying that after int main() is executed at runtime if you don't give it an integer value your program could start acting up? And, is int main(void) kind of an exception to the rule where a value ISN'T needed therefore, int main(void) isn't initialized after the program is executed at run time?
I KNOW THAT'S ALOT OF QUESTIONS (i tried to keep them as sort as possible), please help me, your answers are vital to my understanding of C++. I want to get better at programming and have a lot to learn.