NewBie in C needs help - C simple questions(not for me) help help help help! 
Author Message
 NewBie in C needs help - C simple questions(not for me) help help help help!

HI C expects,

Will you prpfressional help to clear those confusing as below? Please
reply as soon as you can.

1)What is call by reference and call by value in Pointer? Function? What
is the difference between call by reference and cally by value in those
two condition?

2)HOw to a password program ?

3) What is prototype?

Thank you

Benny KOay



Tue, 27 Jan 1998 03:00:00 GMT  
 NewBie in C needs help - C simple questions(not for me) help help help help!

Quote:

>1)What is call by reference and call by value in Pointer? Function? What
>is the difference between call by reference and cally by value in those
>two condition?

The best way to understand this is to understand how C constructs the
function call at the machine level.  Within the machine, there is a
stack.  A function's parameters (and automatic variables) are
allocated on the stack, along with the return address (so the computer
can get back to where it left off before the call).  When the call is
complete, the stack pointer is restored to the point it was before the
call, so all the parameters and automatic variables, in essence,
disappear.

The result of this is that to pass a parameter to a function, I can
either put the value of the parameter or it's address (reference) on
the stack.  Notice that since the stack disappears on exit, anything
the function does to the values on the stack has no effect after the
call.  However, if what was put on the stack was an address, the
function can use that address to modify the thing that the pointer
pointed to.

This is why the following fragment:

     i = 7;
     fun(i);
     printf("%d\n",i);
       .
       .

     void fun( int i )
     {
       i++;
       return
      }

has the somewhat counterintuitive result of printing 7 instead of 8.
The function fun() messes with the copy of i that's on the stack,
never touching the i that the caller passed to it.

However, if I said something like:
     i = 7;
     fun(&i);
     printf("%d\n",i);
       .
       .

     void fun( int *i )
     {
       *i++;
       return
      }

i would be changed to 8.  Outside the function, i is an integer.
Inside the function, i is a pointer to an integer.  The
    fun(&i);
causes the compiler to put the ADDRESS of i on the stack, instead of
the value of i, and the function can then use that address to modify
the thing the address points to.

Quote:
>2)HOw to a password program ?

Depends on operating system and what, specifically, you need.

Quote:
>3) What is prototype?

A prototype is an example to the compiler of how a function should
look.  In the first example, at the top of the main, I should have
something like:

    void fun( int );

which is a PROTOTYPE for fun.  This allows the compiler to check, at
the point fun(i), that i is, in fact, the type of variable that the
function expects.  This is a new-ish feature,  and not found in the
original K&R.

Hope this helps,
--McD



Thu, 29 Jan 1998 03:00:00 GMT  
 NewBie in C needs help - C simple questions(not for me) help help help help!

Quote:

>1)What is call by reference and call by value in Pointer? Function? What
>is the difference between call by reference and cally by value in those
>two condition?

There is no call by reference but only a call by value.
However you can pass a pointer to an object which allows
you to change the object it points to.

Quote:
>2)HOw to a password program ?

??

Quote:
>3) What is prototype?

A prototype is more or less the function header followed
by an ';'. It ensures that functions are called with the
aproprate arguments.

h.f.s.

--
Hans Friedrich Steffani
Institut fuer Elektrische Maschinen und Antriebe
TU Chemnitz-Zwickau



Fri, 30 Jan 1998 03:00:00 GMT  
 NewBie in C needs help - C simple questions(not for me) help help help help!


writes:

Quote:
> HI C expects,

> Will you prpfressional help to clear those confusing as below? Please
> reply as soon as you can.

> 1)What is call by reference and call by value in Pointer? Function? What
> is the difference between call by reference and cally by value in those
> two condition?

In C, parameters are always passed by value.  In other words, the
calling program makes a copy of the variable it is passing, and then
gives this copy to the function being called.  The function can change
the contents of its copy, but that change does not affect the contents
of the original variable.

In some other languages, such as COBOL, variables are usually passed by
reference.  In other words, the calling program gives the address of the
passed variable to the function or subprogram being called.  Because the
subprogram has the address of the original, it can find the original and
change its contents.

In C, passing a pointer is the equivalent of passing by reference.  The
pointer itself is passed by value, i.e. the called function gets a copy
of the pointer.  But because the pointer is the address of something, the
called program can find -- and change -- the thing to which the pointer
points.

Just to add to the confusion, C++ allows you to pass by value or by
reference.  Passing by reference is really a disguised way to pass a
pointer, but it allows a simpler form of coding in some cases.  Of
course, you can still pass a pointer, just as you can in C.

Quote:
> 2)HOw to a password program ?

Huh?

Quote:
> 3) What is prototype?

A prototype tells the compiler two things about a function.  

First, it tells the type of the return value.  Unless you tell it
otherwise, the compiler will assume that a function returns an int.

Second, it tells the compiler the number and types of the function's
parameters.  If you provide a prototype, the compiler will refuse to
let you pass the wrong types of parameters, or the wrong number of
parameters.  

(There are some exceptions.  For example, if the prototype specifies
a parameter of type int, you can still pass it a char.  The compiler
will convert the char to an int for purposes of the function call.)

Here is the prototype of the standard function strchr():

        char *strchr( const char *string, int c );

This prototype says that strchr() accepts a pointer to char and an
int, and returns a pointer to char.  The const keyword is a promise
that strchr() will not change the contents of the character string to
which the pointer points.

Another form is possible:

        char *strchr( const char *, int );

That is, the prototype does not have to provide names for the parameters.
If it does provide names, those names have no meaning outside of the
prototype itself, but they can be useful hints as to what the parameters
are used for.

The following is only a declaration, not a prototype:

        char *strchr();

A declaration gives the type of the return value but says nothing about
the parameters.  It does NOT say that there are no parameters.  If a
function takes no parameters you can say so:

        int rand( void );

This function takes no parameters and returns an int.

You don't have to provide prototypes but you should, either in headers or
in your C source files.  Given prototypes, the compiler can help you
avoid a lot of silly mistakes which might otherwise be hard to catch.

Again, in C++ the rules are somewhat different, but the purpose of a
prototype is the same.

Quote:
> Thank you

> Benny KOay

Scott McKellar
Southwestern Bell Telephone
St. Louis, MO



Fri, 30 Jan 1998 03:00:00 GMT  
 NewBie in C needs help - C simple questions(not for me) help help help help!

Quote:

>In C, passing a pointer is the equivalent of passing by reference.  The
>pointer itself is passed by value, i.e. the called function gets a copy
>of the pointer.  But because the pointer is the address of something, the
>called program can find -- and change -- the thing to which the pointer
>points.

One subtle difference, is that since the function 'owns' the copy of
the pointer, it can change it (not only the thing the pointer points
to, but the copy itself).  A lot of string manipulation routines take
advantage of this, since it's the copy that's being changed.

In languages that permit passing by reference, very often changing the
address is not allowed.  To some extent that makes truly passing by
reference somewhat 'safer', but in many cases, a lot less convenient.

--McD



Tue, 03 Feb 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. newbe/cs student, need help w/ code

2. HELP HELP HELP HELP HELP HELP!!!

3. HELP HELP HELP HELP HELP

4. Newbie: separate big .cs file into small .cs files

5. Compiler error CS 1595 - Help

6. Help!!! Novice CS Advice

7. Newbie needs urgent help, simple question

8. Help C newbie need help please

9. Help C newbie need help please

10. HELP , HELP , HELP - I need a compiler

11. HELP -- NEWBIE NEEDS HELP

12. HELP ++++++++++++++ HELP +++++++++++ HELP ++++++++++++++ HELP

 

 
Powered by phpBB® Forum Software