C's POINTERS
Please keep this BLOG RUNNING. Wait for the ads to load, then Click Skip add at upper right.
Title:
C's POINTERS
PART
A - POINTERS IN C LANGUAGE
v What is a
Pointer?
·
Pointers are
widely used in programming; they are used to refer to memory location of
another variable without using variable identifier itself.
·
A pointer is a variable that is used to point to a memory address whose
content you want to use in your program.
·
The contents of a pointer is a memory address of another location of
memory, which is usually the memory address of another variable, element of a
structure
·
The Figure below
illustrates the idea of pointers. Yptr
is pointing to memory address 100.
v Pointer
Declaration
·
To declare
pointer variable we need to use *
operator (indirection/dereferencing operator) before the variable identifier
and after data type.
·
Pointer can only
point to variable of the same data type.
SYNTAX:
Datatype
*pointerVariablName;
Ex: int *ptrX;
Example Program: Character Pointer
#include<stdio.h>
main()
{
char a='b';
char *ptr;
printf("%c",a);
ptr=&a; /* & is use to get the address of a variable /*
printf("%p n",ptr); /* %p is used to reference pointers */
*ptr='d';
printf("%cn",a);
}
Notes:
& - Ampersand is an Address Operator used to get the address of a variable.
* - Asterisk tells the computer that you are declaring a pointer.
%p - Conversion specifier for pointers.
Output:
b
FFDF
d
v How Pointer
Works?
int x = 1, y = 2;
int
*ip;
ip
= &x; //& means
getting the address of x
y
= *ip;
x =
ip;
*ip =
3;
What will be the value of x and y?
Illustration:
v Pointer
Arithmetic
·
Pointers can be added (increment) and subtracted (decrement). However pointer
arithmetic is quite meaningless unless performed on arrays.
·
Addition and
subtraction are mainly for moving forward and backward in an array.
Note:
you have to be very careful NOT to exceed the array elements when you use arithmetic;
otherwise you will get horrible errors such as “access violation”. This error is
caused because your code is trying to access a memory location which is registered
to another program.
Example: Array
and pointer arithmetic
1: #include <stdio.h>
2: main(){
3: int
ArrayA[3]={1,2,3};
4: int *ptr;
5: ptr=ArrayA;
6: printf("Address:
%p is array value:%d \n",ptr,*ptr);
7: ptr++;
8: printf("Address:
%p is array value:%d \n",ptr,*ptr);
9: }
Output:
Address: FFDC is array value: 1
Address: FFDE is array value: 2
Analysis:
·
In line 3 we are
declaring ‘ArrayA’ integer array
variable initialized to numbers ‘1,2,3’,
·
In line 4, the
pointer variable ptr is declared.
·
In line 5, the
address of variable ‘ArrayA’ is assigned to variable ptr. NOTE that & notation should not be used
with arrays because array's identifier is pointer to the first element of the
array.
·
In line 7 ptr is
incremented by 1.
PART
B - POINTER-ORIENTED PROGRAM
v Using
Pointer Arithmetic and pointer notation to access array elements.
1: /* Demonstrates using
pointer arithmetic to access */
2: /* array elements with
pointer notation. */
3:
4: #include <stdio.h>
5: #define MAX 10
6:
7: /* Declare and initialize
an integer array. */
8:
9: int i_array[MAX] = {
0,1,2,3,4,5,6,7,8,9 };
10:
11: /* Declare a pointer to int and an int variable. */
12:
13: int *i_ptr, count;
14:
15: /* Declare and initialize a float array. */
16:
17: float f_array[MAX] = { .0, .1, .2, .3, .4, .5, .6, .7, .8, .9
};
18:
19: /* Declare a pointer to float. */
20:
21: float *f_ptr;
22:
23: main()
24: {
25: /* Initialize the
pointers. */
26:
27: i_ptr = i_array;
28: f_ptr = f_array;
29:
30: /* Print the array elements.
*/
31:
32: for (count = 0;
count < MAX; count++)
33:
printf("%d\t%f\n", *i_ptr++, *f_ptr++);
34:
35: return 0;
36: }
Output:
0 0.000000
1 0.100000
2 0.200000
3 0.300000
4 0.400000
5 0.500000
6 0.600000
7 0.700000
8 0.800000
9 0.900000
Analysis:
·
Line 5 set 10 in line 5; it is used throughout the program.
·
In line 9, MAX is used to set the number of elements in an array
of ints named i_array. The elements in this array are initialized at the same
time that the array is declared.
·
Line 13 declares two additional int variables. The first is a
pointer named i_ptr. The other variable is a simple type int variable named
count.
·
In line 17, a second array is defined and initialized. This array
is of type float, contains MAX values, and is initialized with float values.
·
Line 21 declares a pointer to a float named f_ptr.
·
Line 27 and 28 assigns the beginning address of the two arrays to
the pointers of their respective types. Remember, an array name without a
subscript is the same as the address of the array's beginning.
·
A for statement in lines 32 and 33 uses the int variable count to
count from 0 to the value of MAX.
·
For each count, line 33 dereferences the two pointers and prints
their values in a printf() function call. The increment operator then
increments each of the pointers so that each points to the next element in the
array before continuing with the next iteration of the for loop.
Workshop:
1)
Is there a difference between a
pointer to a string and a pointer to an array of characters?
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
2)
Why are pointers so important in C?
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
3)
How does the compiler know the
difference between * for multiplication, for dereferencing, and for declaring a
pointer?
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
4)
What happens if I use the
address-of operator on a pointer?
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
5)
Are variables always stored in the
same location?
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
PART
C - PROGRAM DEVELOPMENT
Pointer
Demonstration Program
1.
The program will
use arrays of a type character, integer, and float with 5 elements. The program
will then accept user inputs to fill the arrays thru a looping statement (while for set01, for for set02, and Do..while for Set03).
2.
After filling the
content of the array, the program will then print the memory address and data types of the corresponding inputs.
The program should use pointers arithmetic operators (Set01 will use
Increment/pointer++, Set02 will use Decrement/pointer--, and Set03 must use
both).
Sample Output:
Enter Five Characters:
a
t
s
e
x
Enter Five Integers:
1
4
5
7
3
Enter Five Floats:
1.3
4.0
5.3
1.1
1.0
Value Memory Address Data
Type
a FFDC Char
t EDRC Char
s SDEC Char
e FEDC Char
x FRWE Char
4 ERGB Integer
5 DGFV Integer
7 CDWF Integer
3 SVCD Integer
1.3 CDFR Float
4.0 VDES Float
5.3 ZSDV Float
1.1 ZCVD Float
1.0 CSDR Float
Please keep this BLOG RUNNING. Wait for the ads to load, then Click Skip add at upper right.
Title: C's POINTERS
No comments:
Post a Comment