Some Doubts

Well I was going through the tutorial on this site, and I had a couple of doubts.

1) If I create an Multi Dimensional Array, say int Array [5][3][4]
how can I assign values to it.We can Assign a value to a Single-Dimensional Array by simply putting up the values in {} separated by commas. What about the other dimensions?

2) I didn't understand the difference between passing the value of a function parameter by value and by reference.Can someone explain it?

3)What is the use of inline functions.

4) I am having difficulty understanding pointers. can someone help me?
1) The syntax is the same: int Array [5][3][4] = {{{1,2,3,4},{1,2,3,4},{1,2,3,4}}, ... };
2) A reference is simply an alias for the variable it is initialized with. So if you change a variable that was passed by reference, you'll change the original value. If you pass an object by value, a copy will be made and any changes you make to it inside the function do not affect the original object. You can also pass an object by const reference simply to avoid a copy being made, which can be expensive for complex objects (or impossible, if there is no public copy constructor).
3) inline is a hint to the compiler that it should inject the code of the function at the place it is called. Depending on the situation, this can give anything from a tiny performance improvement to tremendous gains in performance.
However, compilers will automatically inline when they can, so you don't need to use this keyword (except when defining a non-template, non-class function in a header).
4) What part of them do you have trouble with?
You might want to watch this video: http://www.youtube.com/watch?v=UvoHwFvAvQE
Thanks.
1)In the Array part, I was not including the outer {}, i.e., My code was Array[2][3][4] = {1,2},{1,2,3}
this gave the compiler an error

2)So it would mean if I create a function:
1
2
3
4
5
 int Add (int &a, int &b)
{
   a += 1;
   return (a+b);
}

And call it with values, x = 2 & y = 3;
x will become 3 as well?

4)I didn't understand how to use them and where. I get the theory, but I yet am unable to use them.
Questions 2 & 4 are almost the same problem. When you are passing a value by reference as opposed to copying it, you are using a kind of pointer.

Think of a pointer as being like a URL. When you tell your friend to check out some video on Youtube you don't copy Youtube to a million DVD's and have them sort them all, you give him an address that he goes to in order to access the data. The same goes with pointers, when you use a pointer you aren't making a copy of the data and telling the program to use the copy, you are telling it to go to a certain location in memory to access the data. This has a special meaning in regards to passing data to functions. When you pass a function a piece of data by reference instead of by value then instead using a copy of that data to work with the function uses the data at that specific address. This means and changes to that data are reflected back to the origional source which may otherwise by out of scope for that function. If you had used a copy then the changes in that data would not be reflected back.

Was that any help? Pointers are a complicated thing to explain, they just sort of "click" one day and you suddenly understand them.
Last edited on
Well yeah that explains it much better! I will have to work on understanding pointers better though.
Thanks a lot for the replies and the help!
I have a new doubt.
What is the use of having the conditional operator (?) when we have if statements, that have the same function?
Conditional operator is:
A.) Shorthand. Programmers are lazy. Lazy is a good thing.
B.) Overloadable
Conditional operator is:
A.) Shorthand. Programmers are lazy. Lazy is a good thing.
B.) Overloadable


A.) Ha, funny.
B.) What does overloadable mean?
Overloadable basically means you can change what something does. For example you can make an if then check with the "?" operator check a specific set of conditions between two custom classes that you wrote, where as normally an if then would return an error or undefined behavior.
Last edited on
Another doubt:
I tried using return 1, [tt]return 2[tt] etc in the end of my main function, but the final result was the same. so does returning different values in the main function have any effect? same thing if I change the type of main to say, char or something else?
You can't overload the ?: operator (although old versions of GCC used to allow this).
The main difference is that ?: evaluates to something and if/else does not, so they have different use cases.
?: is particularly useful to avoid code duplication when you call a function and only one of the parameters depends on a condition, the others remaining the same.

but the final result was the same

What final result?

does returning different values in the main function have any effect?

It's an error code that can be checked by the program that invoked yours. A return of 0 indicates success, other values are usually used to indicate failure of some kind.
The return value is also used in some expressions in the terminal, such as oneprogram && anotherprogram. In that case, anotherprogram will only execute when oneprogram terminated successfully (i.e. with a return value of 0).

same thing if I change the type of main to say, char or something else?

You can't do that, it's always int.
What final result?

I was referring to the result given by my program. It was a LCM calculator, and when I ran it with main returning 1 or 2 (just to check what happens!), the program compiled and ran perfectly.
The return value is also used in some expressions in the terminal, such as oneprogram && anotherprogram. In that case, anotherprogram will only execute when oneprogram terminated successfully (i.e. with a return value of 0).

What is a terminal?
And I didn't fully how we can use return # to denote an error.I thought that the error can either be a syntax error, which will prevent the code from compiling altogether, or runtime.
Topic archived. No new replies allowed.