Pointers

What is a pointer

  1. A pointer is a variable that contains the memory address of a certain value
  2. When declaring a pointer, the star character is used:
    Example:
    int *valp;
    

  3. In the above example, the varaible valp holds the memory address of a type int number
  4. When dealing with pointers, if you want to consider the value that the pointer points to, the variable should be preceded by a *
  5. In other words, valp contains the memory address, and *valp is the value stored at that memory address
  6. Pointers can be useful in creating functions that have outputs, not returned values. See the example:
#include <stdio.h>

// This program has a function that takes an argument and
// has an output, using pointers.  This program is actually
// useless.

void mathit(int inputa, int inputb, int *out); 

int main() {

	int num1, num2, num3;

	num1 = 3;

	num2 = 5;

	mathit(num1,num2,&num3);

	printf("The answer is %d\n\n", num3);
	
	return(0);
}

void mathit(int inputa, int inputb, int *out) {

	*out = inputa + inputb;

}
Source File | DOS Executable (run from DOS prompt)