
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:
Scott McKellar
Southwestern Bell Telephone
St. Louis, MO