Thread: [Dev-C++] Passig by Reference Question
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <and...@su...> - 2003-04-03 17:24:50
|
Dear Sirs (and Ladies, if you are here),
Please, consider the three simple codes as follows:
##############################
Program 1:
1 -#include <stdio.h>
2 -#include <stdlib.h>
3 -
4 -int sum(int, int);
5 -
6 -int main()
7 -{
8 - int a, b, c;
9 -
10- printf("Two Numbers Sum\n");
11- printf("Enter First Number-->");
12- scanf("%d", &a);
13- printf("Enter Second Number-->");
14- scanf("%d", &b);
15- c = sum(a,b);
16- printf("Sum of %d with %d is %d\n", a, b, c);
17- system("PAUSE");
18- return 0;
19-}
20-
21-int sum(int op1, int op2)
22-{
23- int res;
24-
25- res = op1 + op2;
26- return(res);
27-}
##############################
Program 2:
1 -#include <stdio.h>
2 -#include <stdlib.h>
3 -
4 -void sum(int, int, int *);
5 -
6 -int main()
7 -{
8 - int a, b, c;
9 -
10- printf("Two Numbers Sum\n");
11- printf("Enter First Number-->");
12- scanf("%d", &a);
13- printf("Enter Second Number-->");
14- scanf("%d", &b);
15- sum(a, b, &c);
16- printf("The Sum of %d with %d is %d\n", a, b, c);
17- system("PAUSE");
18- return 0;
19-}
20-
21-void sum(int op1, int op2, int *res)
22-{
23- *res = op1 + op2;
24-
25- return;
26-}
##############################
Program 3:
1 -#include <stdio.h>
2 -#include <stdlib.h>
3 -
4 -void sum(int, int, int *);
5 -
6 -int main()
7 -{
8 - int a, b, *c;
9 -
10- c=(int *) malloc(sizeof(int));
11- printf("Two Numbers Sum\n");
12- printf("Enter First Number-->");
13- scanf("%d", &a);
14- printf("Enter Second Number-->");
15- scanf("%d", &b);
16- sum(a, b, c);
17- printf("The Sum of %d with %d is %d\n", a, b, *c);
18- system("PAUSE");
19- return 0;
20-}
21-
22-void sum(int op1, int op2, int *res)
23-{
24- *res = op1 + op2;
25- return;
26-}
################
Fight Between Programmers Scenario:
A-I prefer to use program 2 instead of program 3 to demonstrate the way how
to use passing
by reference over the original passing by value program 1. I prefer it
because I find this
kind of manipulation in the books and itsn´t necessary to change the
nature of the variables
(c integer variable in this case- look for line 8 in P2 and line 8 in P3);
B-If we are already inside a function, and this function needs to pass a
pointer variable to
other funcion, ok, is only use a passing way like line 16 of Program 3
(integer pointer c
variable in this case).
################
My Question:
Q1-Are both Programs, 2 and 3, correct? Please comment.
Q2-Any opposition about my opinion in "A" Scenario?
Q3-Any opposition about my opinion in "B" Scenario?
Q4-Any additional comments?
Regards
Andre
|
|
From: Ioannis V. <iv...@em...> - 2003-04-03 18:14:51
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Andrι Macαrio Barros
> Sent: Thursday, April 03, 2003 8:25 PM
> To: Dev-Cpp-Users
> Subject: [Dev-C++] Passig by Reference Question
>
>
> Dear Sirs (and Ladies, if you are here),
>
> Please, consider the three simple codes as follows:
>
> ##############################
> Program 1:
> 1 -#include <stdio.h>
> 2 -#include <stdlib.h>
> 3 -
> 4 -int sum(int, int);
> 5 -
> 6 -int main()
> 7 -{
> 8 - int a, b, c;
> 9 -
> 10- printf("Two Numbers Sum\n");
> 11- printf("Enter First Number-->");
> 12- scanf("%d", &a);
> 13- printf("Enter Second Number-->");
> 14- scanf("%d", &b);
> 15- c = sum(a,b);
> 16- printf("Sum of %d with %d is %d\n", a, b, c);
> 17- system("PAUSE");
> 18- return 0;
> 19-}
> 20-
> 21-int sum(int op1, int op2)
> 22-{
> 23- int res;
> 24-
> 25- res = op1 + op2;
> 26- return(res);
or return op1+op2; directly (but in reality sum() is not needed anyway).
> 27-}
> ##############################
> Program 2:
> 1 -#include <stdio.h>
> 2 -#include <stdlib.h>
> 3 -
> 4 -void sum(int, int, int *);
> 5 -
> 6 -int main()
> 7 -{
> 8 - int a, b, c;
> 9 -
> 10- printf("Two Numbers Sum\n");
> 11- printf("Enter First Number-->");
> 12- scanf("%d", &a);
> 13- printf("Enter Second Number-->");
> 14- scanf("%d", &b);
> 15- sum(a, b, &c);
> 16- printf("The Sum of %d with %d is %d\n", a, b, c);
> 17- system("PAUSE");
> 18- return 0;
> 19-}
> 20-
> 21-void sum(int op1, int op2, int *res)
> 22-{
> 23- *res = op1 + op2;
> 24-
> 25- return;
return statement is not needed here.
> 26-}
> ##############################
> Program 3:
> 1 -#include <stdio.h>
> 2 -#include <stdlib.h>
> 3 -
> 4 -void sum(int, int, int *);
> 5 -
> 6 -int main()
> 7 -{
> 8 - int a, b, *c;
> 9 -
> 10- c=(int *) malloc(sizeof(int));
Casting after malloc(), calloc() and realloc() IS NOT needed in C. K&R2 uses
casting because they tried the code in that time Storustrup's C++ compiler.
K&R2 also doesn't place a return statement before the end of main() which
was not proper C90. I assume that at least the first was fixed in later
editions.
So the above should be:
c=malloc(sizeof(int));
or (better):
c=malloc(sizeof(*c));
> 11- printf("Two Numbers Sum\n");
> 12- printf("Enter First Number-->");
> 13- scanf("%d", &a);
> 14- printf("Enter Second Number-->");
> 15- scanf("%d", &b);
> 16- sum(a, b, c);
> 17- printf("The Sum of %d with %d is %d\n", a, b, *c);
> 18- system("PAUSE");
> 19- return 0;
> 20-}
> 21-
> 22-void sum(int op1, int op2, int *res)
> 23-{
> 24- *res = op1 + op2;
> 25- return;
return; not needed.
> 26-}
> ################
> Fight Between Programmers Scenario:
> A-I prefer to use program 2 instead of program 3 to
> demonstrate the way how
> to use passing
> by reference over the original passing by value program 1.
> I prefer it
> because I find this
> kind of manipulation in the books and itsn´t necessary to change the
> nature of the variables
> (c integer variable in this case- look for line 8 in P2 and
> line 8 in P3);
> B-If we are already inside a function, and this function
> needs to pass a
> pointer variable to
> other funcion, ok, is only use a passing way like line 16
> of Program 3
> (integer pointer c
> variable in this case).
> ################
> My Question:
> Q1-Are both Programs, 2 and 3, correct? Please comment.
> Q2-Any opposition about my opinion in "A" Scenario?
> Q3-Any opposition about my opinion in "B" Scenario?
> Q4-Any additional comments?
See my comments above.
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
Anti-racism movement: http://www.positive-youthfoundation.org
Iraq Peace Pledge: http://www.peacepledge.org
|