[Dev-C++] RES: Dev-cpp-users digest, Vol 1 #1386 - 7 msgs
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <and...@su...> - 2003-04-04 13:22:05
|
I spoke too much. And the main answer I was looking for was given.
-->Program 2 and Program 3 are correct programs to be used as examples of
passing by reference?
-->I say this, because I prefer Program 2 that doesn´t change the nature
of variables, ok?
-->I repost the codes, ok?
note-1: of course, is not needed the function sum in any of theese programs,
and the c variable, and the res variable! It´s a matter of explanation for
the students. I prefer to use a very simple program to explain some
operation,
beginning from a program that they already know how to write, like sum, to
present a concept not so easy to understand like passing by reference.
note-2: the cast, I explain too: some of them use the oldies Turbo C
and Turbo C++, and to avoid there the compilation error I´d agree to put the
cast.
note-3: the return? I remember that probably in HP-UX, I´m not sure, when I
used
a command-line cc program (without additional command line options) some of
the
compilation results indicated warnings in void functions that didn´t put
a final "return" command.
Regards
Andre
>--__--__--
>
>Message: 1
>From: "Ioannis Vranos" <iv...@em...>
>To: "'Dev-Cpp-Users'" <dev...@li...>
>Subject: RE: [Dev-C++] Passig by Reference Question
>Date: Thu, 3 Apr 2003 21:14:39 +0300
>
>> -----Original Message-----
>> From: dev...@li...
>> [mailto:dev...@li...] On Behalf
>> Of Andr$B&I(B Mac$B&A(Brio 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$B!-(Bt 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
>
>
|