Pointers - apbtechstudent

Download Report

Transcript Pointers - apbtechstudent

Pointers in C
What is a variable?
Each variable must be defined before you
can use it inside your program.
Did you ask yourself why we have to
declare variables?


First reason is
Second reason is
To allocate memory
Instruct compiler how
to treat this memory
location ML? As int or
float...
What is a variable?
int main()
{
int x = 16;
float y = 2.3;
char z = 70;
.
.
.
}
Three MLs have been
allocated. The first ML
holds int value. The
second ML holds
floating-point number.
The last ML holds one
byte integer.
Sometimes we say: the content of ……… is ……….
Run this code 
int main()
{
float num = 1.0;
printf(“num = %d\n”, num);
}
What is a variable?
A variable is a named memory location.
Variables provide direct access to its
memory location.
Can you understand what will happen
when you write:
x = 44;
y = x;
Memory Addresses
Each ML has an address  each variable
has an address.
How to get the address of a variable?
&variable_name
Run this code 
int main()
{
float num = 1.0;
printf(“Address of num is %u\n”, &num);
}
Pointer Variables
Ask yourself:



What is an integer variable?
What is a floating-point variable?
What is a character variable?
Now, what is a pointer variable?
is a variable which holds an address of a
ML.
Pointer Variable
Assume ptr is a pointer variable and x is an integer variable
x 10
ptr &x
Now ptr can access the value of x.
x = 10
ptr = &x
HOW!!!!
Write: *variable .
For example:
printf(“%d\n”, *ptr);
Can you tell me why we put %d????
Declaring a Pointer Variable
In previous example, ptr points to the
variable x. We say that ptr is an integer
pointer.
Similarly, we have character pointer,
floating pointer, long pointer, … .
Declaring a Pointer Variable
To declare j as an integer pointer:
int *j;
This declaration tells the compiler that j will be
used to store the address of an integer value.
‘*’ stands for “value at address”.
Thus, int *j would mean , the value at the
address contained in j is an int.
To declare ptr as a character pointer:
char *ptr;
Declaration versus Redirection:
When an asterisk is used for declaration, it is
associated with a type.
Ex: int* pa;
int* pb;
On the other hand, we also use the asterisk for
redirection.
When used for redirection, the asterisk is an
operator that redirects the operation from the
pointer variable to a data variable.
Ex: sum=*pa+*pb
pointers to pointers
Pointer is a variable that contains the
address of another variable.
This variable in turn can be might be
another pointer.
Now, we have pointer that contains
another pointer’s address.
Ex:
int a;
int* p;
int** q;
q
a=58;
p=&a;
q=&p;
p
234560
397870
287650
234560
printf(“%d”,a);
printf(“%d”,*p);
printf(“%d”,**q);
a
58
287650
Address arithmetic
Addition of a number to a pointer.
Ex: int i=4,*j,*k;
j=&i;
j=j+1;
j=j+9;
k=j+3;
Subtraction of a number from a pointer.
Ex: int i=4,*j,*k;
j=&i;
j=j-2;
j=j-5;
k=j-6;
Subtraction of one pointer from another.
#include<stdio.h>
void main()
{
int arr[]={10,20,30,45,67,56,74};
int *i,*j;
i=&arr[1];
j=&arr[5];
printf(“%d%d”,j-i,*j-*i);
}
Comparison of two pointer variables.
pointer variables can be compared provided both variables
point to objects of the same data type.
#include<stdio.h>
void main()
{
int arr[]={10,20,30};
int *j,*k;
j=&arr[4];
k=(arr+4);
If(j==k)
printf(“The two pointers point to the same location”);
else
printf(“The two pointers do not point to the same
location”);
}
Note:
The following operations cannot be
performed on pointers.
Addition of two pointers.
Multiplication of a pointer with a constant.
Division of a pointer with a constant.
Pointers and Arrays
When an array is declared, the compiler
allocates a base address and sufficient
amount of storage to contain all the
elements of the array in contiguous
memory locations.
The base address is the location of the
first element of the array.
Ex: int x[5]={1,2,3,4,5};
Elements
X[0]
Value
Address
1
1000
X[1]
2
X[2]
3
1002
1004
X[3]
X[4]
4
5
1006
1008
Base Address
Address of x[3]=base address+(3*scale factor of int)
= 1000+(3*2)=1006
An element in a two-dimensional array can be
represented by the pointer expression as follows:
*(*(a+i)+j) or *(*(p+i)+j)
p
p+i
*(p+i)
*(p+i)+j
*(*(p+i)+j)
pointer to first row.
pointer to ith row.
pointer to first element in the ith
row.
pointer to jth element in the ith
row.
value stored in the cell (i,j).
Ex: int a[3][4]={{15,27,11,35},
{22,19,31,17},
{ 31,23,14,36}
};
The elements of a will be stored as:
Row 0
15
27
11
Row 1
35
22
19
31
Row 2
17
31
23
14
36
Compatibility
Pointer Size compatibility.
Dereference Type Compatibility.
Pointer size compatibility:
The size of all pointers is the same.
Every pointer variable holds the address of
one memory location in the computer.
On the other hand, the size of the variable
that the pointer references can be
different.
It takes the attributes of the type being
referenced.
Deference type compatibility:
The deference type is the type of the variable
that the pointer is referencing.
A pointer is a char is only compatible with a
pointer to a char.
We cannot assign a pointer to a char to a pointer
to an int.
Ex: char c;
int a;
char* pc;
int* pa;
pc=&c;
// good and valid.
pa=&a;
// good and valid.
pa=&c;
// Error: different types.
pc=&a;
// Error: different types.
Pointer to void
A pointer to void is a generic type that is not
associated with a reference type.
It is not the address of a character, an integer, a
real or any other type.
It is compatible for assignment purposes only
with all other pointer types.
A pointer of any reference type can be assigned
to a pointer to void type.
A pointer to void type can be assigned to a pointer of any reference
type.
We can declare a variable of pointer to void type as follows:
void* pvoid;
A null pointer is a pointer of any type that is assigned the constant
NULL.
A variable of pointer to void is a pointer with no reference type that
can store only the address of any variable.
Ex: void* pvoid;
// pointer to void type.
int* pint = NULL;
// NULL pointer of type int.
char* pchar = NULL; // NULL pointer of type
char.
A void pointer cannot be dereferenced
Casting pointers
The problem of type incompatibility can be
solved if we use casting.
We can make an explicit assignment between
incompatible pointer types by using a cast.
Ex:
pc=(char*)&a;
Ex: // local declaration
void* pvoid;
char* pchar;
int* pint;
// statements
pvoid = pchar;
pint = pvoid;
pint = (int*) pchar;
Pointers And Character Strings
C supports an alternate method to create
strings using pointer variables of type char.
Ex: char *str=“good”;
This creates a string for the literal and then
stores its address in the pointer variable
str.
The pointer str now points to the first
character of the string “good”.
We can also use the run time assignment
for giving values to a string pointer.
Ex:
char *string1;
string1=“good”;
We can print the content of the string
string1 using either printf or puts functions
as follows:
printf(“%s”,string1);
puts(string1);