- One of the places you'll see pointers used
the most and something that you truly can't avoid
in the C language,
is where a function requires or returns a pointer value.
Happens a lot.
This movie helps you deal with pointer functions,
even create your own.
You'll see how pointers are used in functions,
how to pass a pointer to a function,
and how to return a pointer from a function.
You've all ready seen pointers used in functions
in this course.
You probably just didn't recognize
that's what was happening.
The scan F function uses pointer values,
specifically, memory locations of the variables it reads.
The ampersand in line eight reads the address
of variable eight and sends that address
to the scan F function.
You know, scan F's turn value isn't used in the function.
Instead, the function simply uses a pointer
to assign the value to variable X directly.
Clever, huh?
In exercise scan F2, which I have on the screen here,
the ampersand is not required for the name variable,
why, because name is an array.
Array's are pretty much pointers in disguised.
The ampersand isn't needed.
You can concoct your own functions that use pointers.
You need to specify a pointer as an argument
or as the return value.
Open exercise file P function one.
The function minus 10 is (proto typed) at line three.
It accepts a pointer variable as its argument.
Pointer variable is a memory location.
So, it accepts the address of a variable as the argument.
The address a variable value is past the
function at line 10.
The ampersand operator is what fetches the address.
In the function, the asterisk operator is used
to change the value of the variable past.
Nothing needs to be returned,
because the variables value is directly accessed in memory.
Built and run the code.
The variables value is effected by the function,
even though, only its address was past the function,
and the function didn't return anything.
Open exercise P function two.
In this code, a pointer function is declared.
Longer returns a character pointer
or the address of the start of a string in memory.
The functions starting at line 18,
compares two strings by length.
The longer string is returned,
or I should say its address is returned.
The string itself never really makes it to the function,
only its address.
The address returned by the longer function
is stored in the result pointer.
It's then output at line 13.
Build and run this code.
The longer string is displayed.
You can also manipulate strings in functions.
Again, you're not passing this string itself,
merely its starting point in memory.
Open exercise file P function three.
This code is another example of string manipulation
by using pointers,
where the function doesn't really need
to return any value.
The shouting function at line 19,
takes a string and munches through it,
one character at a time.
Each character is run through the two upper function,
which converts lower case letters to upper case.
Build and run the code.
Type something like
I'm not shouting, and enter, and it worked.
All without returning anything from the function.
Open exercise P function four.
In this code, a string is returned from
the encrypted function.
A string is accepted by the function,
manipulated, then a new string is returned,
which keeps the original string intact,
but will it work?
Build and run.
Now, you may see a compiler warning,
and it's a serious warning,
which explains why the output is unpredictable.
Well, unless these are really secret instructions.
The problem here has nothing to do with pointers.
It's an old situation I explained in an
earlier movie on functions.
That is all variables used inside a function are local.
When the function is done, its variables are tossed aside.
This holds true for the encrypted function
with the output array.
To fix the problem, you need to use
the static keyword.
Insert the word static
before the variable declaration on line 17.
The program now retains the content of the array,
the string, when the function is done.
It stays static, which means unchanging.
Save. Build and run.
Only the address of the string is returned
by the encrypted function.
The rest of the characters exist in memory,
and are excessed by the print F function.
It works only when you remember to make the string
return static.
This rule doesn't apply to single values.
Functions are designed to return individual values,
which include pointers,
but for strings or other arrays,
or any other variable use inside a function.
It must be declared as static,
or the value doesn't keep when the function is done.