Tutorial - Pointer, Input, Operators

Main Page
Helpful Links
Downloads
Tutorials
Grades
Syllabus
Contact Me

This lesson covers input, and operators.  Before discussing input however, it has a quick overview of pointers, for the sake of understanding the input function.


Pointers and Addresses

   Computer memory is laid out sequentially, like in a line (there may be exceptions to this).  Every piece of memory, every byte, has an address.  This is simply a number.  Sometimes, it is necessarily to directly manipulate these addresses.
   Perhaps the most common case is with a function.  When a function is given a parameter, it is really given a copy of that variable.  Any changes it makes die with the function.  But what about when we want a function to change a variable?
   The correct way is instead of passing the actual variable, pass the address of the variable.  If the function is written to accept this, it will be able to use the address to change the value of the variable.

Pointer Syntax

   Pointers are declared using the * symbol, like this:

 int * x;
 char * name;

   On the first line, we are declaring an integer pointer named x.  On the second line, we are declaring a char pointer called name.

   The address of a variable is taken using the & symbol:

int xcoord;
x = &xoord;

   The first line declares an integer variable, while the second line uses & to assign that variable's address to x.


Input

   Input can be collected in C using the scanf function.  It works almost exactly the same as the printf function.  It takes a string, with format specifiers, followed by the addresses of the variables you wish to read in to.  This is so the function can actually modify the contents of the variables.
   Here's an example snippet:

int x;
int y;
int z;

printf("Please enter the coordinates of the point>");

scanf("%i %i %i", &x, &y, &z);  // pass the addresses of the variables.

printf("\nYou have entered %i, %i, and %i.\n", x, y, z);


   One must also be watchful of potential errors, such as this one:

int x = 25436;  // declare x and initialize it with a default value

printf("Please enter a value for x>");

scanf("%i", x);  // this is WRONG, we're passing x, not the address of x

printf("x=%i",x);

   As you can see, we've screwed up by passing the value of x instead of the address.  Since we've initialized it, we know that the value of x is 25,436.  The scanf function thinks it's getting the address of x.  So what it does is reads in the value the user enters, and attempts to store it at memory address number 25,436.  Depending on where that is, several things can occur.
   If that memory address lies withing your program's memory space, it could overwrite some other important variable.  If it lies outside, your program will attempt to overwrite some other program or the Operating System's memory.   On most modern Operating System's, any attempts to write outside your own memory space result in your  program being terminated.  On older or less protected ones, you could crash the whole system doing this stuff.  The point is, be careful.
   In theory, the compiler should warn you if you do this.  It won't stop you, but it will at least warn you when you pass the wrong type to a function.

Scanf's Format Specifiers

Format Specifier
Description
%d,%i
Specifies an int
%c
Specify a char
%u
Specify an unsigned int
%f
Specify a float
%lf
Specify a double

Take special note of the last two specifiers, %f and %lf.  Unlike with printf, scanf uses different specifiers for floats and doubles.  Ignoring this will cause problems.  For more info, check this page out.  Special thanks go to Tiffany J. Aubuchon and the Resource Center for pointing this out to me.




Arithmetic Operators

   The C language includes several built in operators for performing basic arithmetic operations like addition, subtraction, multiplication, and division.  More advanced mathematics can be performed using functions from the math library.


Operator
Explaination
+
Returns the sum of two numbers.
-
Returns the difference between two numbers.
*
Returns the product of two numbers.
/
When two integers are used, returns the integer division result.
When at least on floating point number is used, returns a floating point result.
%
Returns the remainder of integer division.  Known as the modulus operator.

   It's important to remember that the return type of an operation is determined by the operands, not the type of the variable the result is assigned to.  It's also important to be wary of combining different types in an operation, like an unsigned int and a regular int.  Make sure you know exactly what you're doing if you choose to do this.