In this part you will learn:
1. What are Pointers
2. How to use them in Programs
3. C syntax
4. Showing output
What are Pointers?
Our computer stores variables in the memory so they can be accessed by the compiler for processsing. The compiler sets aside a memory location with a unique address to store that variable. Compiler basically creates an address space for a specific variable declared in the program and pointers are used to store the address locations of the variables or pointer can be referred as a variable that stores a memory address.
A pointer variable can be declared just by putting an asterisk (*) before the name of the variable but after the type of variable
e.g if we want to make an integer type pointer variable then simply put asterisk before the name of the variable like here we are making an integer type pointer named num1 so we write it as
Secondly there is an important thing that you will see in the program below that is ampersand ‘&’ , whenever we want that the change from the function changes the value of originally passed variable , we used ampersand sign with the variable when being passed.
The address of the variable is passed when we write ampersand with variable and ampersand is opposite to *.
printf(“Address of num1 : %d”,&num1); // here something like 00AF344 will show up which is the //memory address
now if we want to see the relationship between ampersand and pointers it can be demonstrated as
printf(“Value of num1 : %d”,&*num1); // output will be value of num1 i.e 4
Pointer dereferences the variable.
Using Pointer and Functions
Open Dev C++ then File > new > source file and start writing the code below.
- double distance(int *y1,int *y2,int *x1,int *x2)
- double x,y;
- x = *x2-*x1;
- y = *y2-*y1;
Execute > compile
then Execute > run
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.