In my old C++ programming classes I would often trip up on theses basic concepts when things got more and more complicated:

1. Arrays
2. Functions
3. Inheritance
4. Call by Reference
5. Call by Value
6. Pointers

I kind of know how they work but don't really know what they are good for in real life applications. Could somebody explain his or her:

(A) Definition of the concept
(B) Understanding of what the concept does/how it works
(B) Quick example of the concepts use in real life applications (i.e. what is it good for?)


I'll put my understandings and explanation up later tonight, tomorrow the latest

Each of these are rather broad topics. Can you cut it down to specific questions so we don't have to write page after page of tutorials?

Each of these are rather broad topics. Can you cut it down to specific questions so we don't have to write page after page of tutorials?

Okay, lets deal with functions first, then inheritance

lets start with the format of a function:

return-value-type function name( parameter-list)
{
declaractions and statements
}

^^^what does all that mean, and what does it mean whem you have nothing in the parameter-list compared to when you do have something(s) in the parameter-list?

that one always gets me

return-value-type Any value, or none at all, that you want to return from the function: an error code, a calculated result, etc.

parameter-list Any value(s) -- or possibly references in C++, or none at all, that you want to pass to the function: a pointer to an array to calculate the sum, for example.

declaractions and statements The "meat"; what it takes to do whatever it is the function does.

what does it mean whem you have nothing in the parameter-list compared to when you do have something(s) in the parameter-list?

It means whatever you want it to mean. This is a little like asking how a painter turns paints and canvas into a portrait.

parameter-list Any value(s) -- or possibly references in C++, or none at all, that you want to pass to the function

okay you touched on something there! what do you mean by "pass to the function"

explain with an example please

#include <stdio.h>

int multiply(int x, int y)
{
   return x * y;
}

int main()
{
   int result = multiply(2,3);
   printf("result = %d\n", result);
   return 0;
}

/* my output
result = 6
*/
#include <stdio.h>

int multiply(int x, int y)
{
   return x * y;
}

int main()
{
   int i, j;
   for (i = 1; i < 10; ++i)
   {
      for (j = 1; j < 10; ++j)
      {
         printf("%2d ", multiply(i, j));
      }
      putchar('\n');
   }
   return 0;
}

/* my output
 1  2  3  4  5  6  7  8  9 
 2  4  6  8 10 12 14 16 18 
 3  6  9 12 15 18 21 24 27 
 4  8 12 16 20 24 28 32 36 
 5 10 15 20 25 30 35 40 45 
 6 12 18 24 30 36 42 48 54 
 7 14 21 28 35 42 49 56 63 
 8 16 24 32 40 48 56 64 72 
 9 18 27 36 45 54 63 72 81 
*/
#include <stdio.h>

int multiply(int x, int y)
{
   return x * y;
}

int main()
{
   int result = multiply(2,3);
   printf("result = %d\n", result);
   return 0;
}

/* my output
result = 6
*/

hmmm...thanx for the break down...ok so did you pass parameters x and y in function multiply() to function multiply() found in the function main()? am i right or wrong?

and what is this "%d", what does it do?

please explain whats going on in the printf() function

#include <stdio.h>

int multiply(int x, int y)
{
   return x * y;
}

int main()
{
   int i, j;
   for (i = 1; i < 10; ++i)
   {
      for (j = 1; j < 10; ++j)
      {
         printf("%2d ", multiply(i, j));
      }
      putchar('\n');
   }
   return 0;
}

/* my output
 1  2  3  4  5  6  7  8  9 
 2  4  6  8 10 12 14 16 18 
 3  6  9 12 15 18 21 24 27 
 4  8 12 16 20 24 28 32 36 
 5 10 15 20 25 30 35 40 45 
 6 12 18 24 30 36 42 48 54 
 7 14 21 28 35 42 49 56 63 
 8 16 24 32 40 48 56 64 72 
 9 18 27 36 45 54 63 72 81 
*/

Hmmmmm....in the function multiply() how did you go from parameters x and y to i and j, that looks like a "call-by-reference" but i don't understand why you did that and how it works, please explain dave

and what does the function putchar() mean

hmmm...thanx for the break down...ok so did you pass parameters x and y in function multiply() to function multiply() found in the function main()? am i right or wrong?

In the first example, the values 2 and 3 from the call to multiply in main() become the values of the parameters x and y in the function multiply().

and what is this "%d", what does it do?

please explain whats going on in the printf() function

It is just printing an integer, which was the result returned by the function.

Hmmmmm....in the function multiply() how did you go from parameters x and y to i and j

In the second example, the values of i and j in main() are passed to the function multiply() as the parameters x and y.

that looks like a "call-by-reference" but i don't understand why you did that and how it works, please explain dave

Nope. Pass by value. A copy of the value of i, for instance, is passed to the function as the parameter x.

and what does the function putchar() mean

It puts a char to the stdout.

In the first example, the values 2 and 3 from the call to multiply in main() become the values of the parameters x and y in the function multiply().

In the second example, the values of i and j in main() are passed to the function multiply() as the parameters x and y.

i noticed how you said "become the value"


let me make sure i got that right

does the funtion

multiply(int x, int y)

CALL THE VALUES 2 and 3 (i.e. call-by-value")

of the function

multiply(2,3);

to perform

return x * y;


i'm not sure, is that what you meant by "become"?

then what is call-by-reference???

i noticed how you said "become the value"
i'm not sure, is that what you meant by "become"?

This is the standard's definition of "parameter" or "formal parameter":

object declared as part of a function declaration or definition that acquires a value on entry to the function, or an identifier from the comma-separated list bounded by the parentheses immediately following the macro name in a function-like macro definition

And where the function, the "expression in the comma-separated list bounded by the parentheses in a function call expression" is called the "arguments".

The value of an argument is passed to a function as a parameter.

Hello,

Hmmmmm....in the function multiply() how did you go from parameters x and y to i and j, that looks like a "call-by-reference" but i don't understand why you did that and how it works

» One aspect of C functions may be unfamiliar to programmers who are used to some other languages, particularly Fortran, is Call by Value. In C, all function arguments are passed "by value". This means that the called function is given the values of its arguments in temporary variables rather than the originals. This leads to some different properties that are seen with "call by reference" languages. The main distinction is that in C the called function cannot directly alter a variable in the calling function; it can only alter its private, temporary copy.

Call by value is an asset, however, a liability. It usually leads to more compact programs with fewer extraneous variables, because paramaters can be treated as conveniently initialized local variables in the called routine. For example, here is a version of power that makes use of this property:

int power(int base, int n) {
	int p;

	for (p = 1; n > 0; --n)
		p = p * base;
	return p;
}

int main() {
	int x = 2, y = 3;

	printf("%d\n", power(2, 3));

	printf("%d\n", power(x, y));

	return 0;
}

The paramater n is used as a temporary variable, and is counted down until it becomes zero. Whatever is done to n inside power has no effect on the argument that power was originally called with.

and what does the function putchar() mean

» The function, putchar(), writes a character to the current position in the standard output (stdout) and increases the file pointer to point to next character.

what is call-by-reference???

» An argument passing convention where the address of an argument variable is passed to a function or procedure, as opposed to where the value of the argument expression is passed.


- Stack Overflow

#include <stdio.h>

int multiply(int x, int y)
{
   return x * y;
}

int main()
{
   int i, j;
   for (i = 1; i < 10; ++i)
   {
      for (j = 1; j < 10; ++j)
      {
         printf("%2d ", multiply(i, j));
      }
      putchar('\n');
   }
   return 0;
}

/* my output
 1  2  3  4  5  6  7  8  9 
 2  4  6  8 10 12 14 16 18 
 3  6  9 12 15 18 21 24 27 
 4  8 12 16 20 24 28 32 36 
 5 10 15 20 25 30 35 40 45 
 6 12 18 24 30 36 42 48 54 
 7 14 21 28 35 42 49 56 63 
 8 16 24 32 40 48 56 64 72 
 9 18 27 36 45 54 63 72 81 
*/

^^^i know you said that is "pass-by-value", but it looks like the same thing is going on in the pass-by-referrence example you linked me to, look

//An alias (an alternate name) for an object.
//References are frequently used for pass-by-reference: 


 void swap(int& i, int& j)
 {
   int tmp = i;
   i = j;
   j = tmp;
 }
 
 int main()
 {
   int x, y;
   ...
   swap(x,y);
   ...
 } 
//Here i and j are aliases for main's x and y respectively.
//In other words, i is x — not a pointer to x, nor a copy of x, but x itself.
//Anything you do to i gets done to x, and vice versa.


why is that?

I'm reading your responses right now Stack!

Let's try this.

[B]#include[/B] <iostream>
[B]using[/B] std::[B]cout[/B];

[B]void[/B] [B]by_value[/B]([B]int[/B] copy)
{
   copy += 42;
   [B]cout[/B] << "by_value: " << copy << '\n';
}

[B]void[/B] [B]by_reference[/B]([B]int[/B] &reference)
{
   reference += 42;
   [B]cout[/B] << "by_reference: " << reference << '\n';
}

[B]int[/B] [B]main[/B]()
{
   [B]int[/B] i = 0;
   [B]cout[/B] << "main: " << i << "\n";
   [B]by_value[/B](i);
   [B]cout[/B] << "main: " << i << " - Note: no change!\n";
   [B]by_reference[/B](i);
   [B]cout[/B] << "main: " << i << " - Note: changed!\n";
   [B]return[/B] 0;
}

/* my output
main: 0
by_value: 42
main: 0 - Note: no change!
by_reference: 42
main: 42 - Note: changed!
*/

Let's try this.

[B]#include[/B] <iostream>
[B]using[/B] std::[B]cout[/B];

[B]void[/B] [B]by_value[/B]([B]int[/B] copy)
{
   copy += 42;
   [B]cout[/B] << "by_value: " << copy << '\n';
}

[B]void[/B] [B]by_reference[/B]([B]int[/B] &reference)
{
   reference += 42;
   [B]cout[/B] << "by_reference: " << reference << '\n';
}

[B]int[/B] [B]main[/B]()
{
   [B]int[/B] i = 0;
   [B]cout[/B] << "main: " << i << "\n";
   [B]by_value[/B](i);
   [B]cout[/B] << "main: " << i << " - Note: no change!\n";
   [B]by_reference[/B](i);
   [B]cout[/B] << "main: " << i << " - Note: changed!\n";
   [B]return[/B] 0;
}

/* my output
main: 0
by_value: 42
main: 0 - Note: no change!
by_reference: 42
main: 42 - Note: changed!
*/

when im done with this game of chess (do you play?) im definitely looking at this a lot closer. Looks like a real good/smart example

thanks a lot

i got it!!!!


damn that feels good (i was starting to think i wasn't cut out for programming)


check this out:

when calling by value a copy is made of the callers value
which is then passed to the called function
changes made by the called function to the copied value doesn't have an affect on the value in the callers function

a disadvantage of this is if the value copied is HUGE it could take a while to process and slow down performance of program

when calling by reference the object of the called function is given a new name by the calling function and therefore BECOMES the new object. Any changes to the value of the called function reflects an exact change in the value of the callers function

a disadvantage of this is a security issue. Call by reference allows outside functions/whatever to manipulate the data in the original function.


CAN SOMEBODY PLEASE HELP ME WITH "POINTERS" and "INHERITANCE"...that can get very confusing also!!!

i got it!!!!

I believe you do.

CAN SOMEBODY PLEASE HELP ME WITH "POINTERS" and "INHERITANCE"...that can get very confusing also!!!

How about pointers first? What do you already know, or think you know?

Let's start with a klunky definition.

A pointer type may be derived from a function type, an object type, or an incomplete type, called the referenced type. A pointer type describes an object whose value provides a reference to an entity of the referenced type. A pointer type derived from the referenced type T is sometimes called ‘‘pointer to T’’. The construction of a pointer type from a referenced type is called ‘‘pointer type derivation’’.

Also, A TUTORIAL ON POINTERS AND ARRAYS IN C may be a good starting point.

I believe you do.

*cartwheel cartwheel*

*backflip backflip*

:mrgreen:


I'm a VERY rusty beginner programmer, so give me a second to read the tutorials and my old C++ text book and i'll tell you, because right now all i know is a pointer is like an object or variable that points to the location/address of another object or variable. The problem is I don't know, how to really set it up. I also don't know what it's good for in real life applications (i.e. like a real like example of when it's would be needed), although I do remember a good chess example in my C++ book.

okay i understand pointers somewhat now

here's the basics,

*ptr is "deferencing" which means it points to the value of the pointer

ptr = &X means ptr is pointing to the lvalue or address of X

if x=7

and *ptr = 7

the prt = x

right?


^^^its all of the top of my head, seeing i've got the basic down

Can you please help me with "Pointers and Dynamic Memory Allocation", that is a little confusing now?

>*ptr is "deferencing" which means it points to the value of the pointer
Dereferencing, or indirection. It means that given an address, you're given back the contents of that address.

>ptr = &X means ptr is pointing to the lvalue or address of X
Don't confuse the concrete with the abstract. In the abstract, it means that ptr is pointing to X. In the concrete, it means that ptr is assigned the address of X.

>if x=7
Yes.

>and *ptr = 7
Not necessarily. Just because *ptr evaluates to 7 doesn't mean it points to x. ;)

>the prt = x
No, not in any case unless the address of x were equal to its value. Here's what you want:

if x == 7 and ptr == &x then *ptr == 7.

That's the classic definition of a pointer.

>Can you please help me with "Pointers and Dynamic Memory Allocation"
What part are you having trouble with?

^^^

damn it feels good seeing you again :)

thanx!

what do you mean by "evaluate to"?

guess what, Dude im getting a Dell :cheesy:

^^^

guess what, Dude ...

LOL, since when did you became a man Narue :mrgreen:

>LOL, since when did you became a man Narue
I've always been a man. First, people assume I'm a guy because most programmers are guys and no good programmer could possibly be a girl. :rolleyes: Then, after being corrected, people assume I'm a guy pretending to be a girl to get attention. It's a common occurance, and I'm used to it.

>LOL, since when did you became a man Narue
no good programmer could possibly be a girl. :rolleyes:

Yup, I have seen only you and Kathy Sierra( author of Head First Java) :D

>LOL, since when did you became a man Narue
I've always been a man.

Ok now im confused :cheesy:

could you help me with Pointers and Dynamic Memory Allocation?

how does it work, i understand what Dynamic Memory Allocation is but don't understand the code for it.

My understanding of D M A: You could call it variable storage of date. It grows and shrinks depending on the need of the application at run time.

this is where i get lost:

int *iptr;
    iptr = (int *)malloc(10 * sizeof(int));
    if (iptr == NULL)

    { .. ERROR ROUTINE GOES HERE .. }

I'm not sure I understand your question. You declare a pointer, iptr, that you will try to point to data that will be dynamically allocated. You then call malloc to request some amount of memory. Then you check to see whether you did indeed receive the memory you requested, and make sure you don't use memory you didn't get if you didn't get any.

int main(void)
{
    int nrows = 10;
    int row, col;
    rptr = malloc(nrows * COLS * sizeof(int));
    for (row = 0; row < nrows; row++)
    {
        for (col = 0; col < COLS; col++)
        {
            rptr[row][col] = 17;
        }
    }

    return 0;
}

is int main(void) mean nothing is returned? if so, what does "return 0;" mean? What happens when you return something as opposed to returning nothing?

You've got it turned around -- main is returning an int; the void means it receives no parameters.


These questions are better posted in the forum.

Okay I got you about void meaning main receives no parameters, whatever that means...hahahaha

but seriously, i think you can answer my question by explaining to me in pseudo code whats going on in this line of code:

iptr = (int *)malloc(10 * sizeof(int));

or this

int main(void)
{
    int nrows = 10;
    int row, col;
    rptr = malloc(nrows * COLS * sizeof(int));
    for (row = 0; row < nrows; row++)
    {
        for (col = 0; col < COLS; col++)
        {
            rptr[row][col] = 17;
        }
    }

    return 0;
}

I think a psuedo code explanation will really help here unless you have a better example to help me get this concept of pointers and Dymanic Memory Allocation down.

i think you can answer my question by explaining to me in pseudo code whats going on in this line of code:

iptr = (int *)malloc(10 * sizeof(int));

You request from malloc space for 10 objects each of which are sizeof(int) bytes in size -- you ask for space for 10 ints.

You are using a C++ compiler to compile C code, so you have a cast which is unnecessary in C.

The result of the call to malloc, a pointer to the memory you requested if successful -- or a NULL pointer if unsuccessful, is assigned to iptr, which has the type pointer to int.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.