Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
pointers (1).ppt
1. Pointer Basics
Variables are allocated at addresses in computer memory
(address depends on computer/operating system)
Name of the variable is a reference to that memory address
A pointer variable contains a representation of an address of
another variable (P is a pointer variable in the following):
101
V P
Name
Abstract
Representation
Concrete
Representation
Address
4 bytes for
int value 101
4 bytes for
mem address v
v (some value) p (some value)
int V = 101;
int *P = &V;
2. Pointer Variable Definition
Basic syntax: Type *Name
Examples:
int *P; /* P is var that can point to an int var */
float *Q; /* Q is a float pointer */
char *R; /* R is a char pointer */
Complex example:
int *AP[5]; /* AP is an array of 5 pointers to ints */
– more on how to read complex declarations later
3. Address (&) Operator
The address (&) operator can be used in front of any
variable object in C -- the result of the operation is
the location in memory of the variable
Syntax: &VariableReference
Examples:
int V;
int *P;
int A[5];
&V - memory location of integer variable V
&(A[2]) - memory location of array element 2 in array A
&P - memory location of pointer variable P
4. Pointer Variable Initialization/Assignment
NULL - pointer lit constant to non-existent address
– used to indicate pointer points to nothing
Can initialize/assign pointer vars to NULL or use the
address (&) op to get address of a variable
– variable in the address operator must be of the right
type for the pointer (an integer pointer points only at
integer variables)
Examples:
int V;
int *P = &V;
int A[5];
P = &(A[2]);
5. Indirection (*) Operator
A pointer variable contains a memory address
To refer to the contents of the variable that the
pointer points to, we use indirection operator
Syntax: *PointerVariable
Example:
int V = 101;
int *P = &V;
/* Then *P would refer to the contents of the variable V
(in this case, the integer 101) */
printf(“%d”,*P); /* Prints 101 */
6. Working with Pointers
In order to work with pointers, do the
following:
• Declare a pointer variable
• Initialize it.
• Access the original value through a pointer
or perform any other operation on it.
7. Pointer Declaration
• In C, every variable must be declared for its type.
• Since pointer variables contain addresses that belong to a separate data
type, they must be declared as pointers before we use them.
• The general format of format declaration is
– datatype *pt_name;
• This tells the compiler three things about the variable pt_name
1. The asterisk (*) tells that the variable pt_name is a pointer variable.
2. pt_name needs a memory location.
3. pt_name points to variable of type data type. Example: int *ptr; This
declaration tells that the variable points to an integer data type.
8. INITIALIZING POINTERS:
• Once a pointer variable has been declared, it
can be made to point a variable using an
assignment statement such as
– int i=10,*ptr; ptr=&i;
– or int *ptr=&i;
• Which causes ptr to point to i. That is, ptr
now contains the address of i.
• Note: A pointer declared of one data type
cannot point to variable of another data
type.
9. ACCESSING A VARIABLE THROUGH ITS
POINTER
• Once a pointer has been assigned the address of a
variable, we can access the value of the variable
using this pointer.
• This is done by using another unary operator
*(asterisk), known as the indirection operator.
13. Pointer Arithmetic
Valid operations on pointers are:
Assignment of pointers
Adding or subtracting a pointer and integer
Subtracting two pointers
Comparing two pointers
Incrementing or decrementing the pointers
Assigning the value 0 to the pointer.
Invalid operations on pointers are:
Addition, Multiplication, Division of two pointers
Multiplication, Division of pointer with a number
19. Invalid operations on pointers
• There are various operations which can not be performed on
pointers. Since, pointer stores address hence we must ignore the
operations which may lead to an illegal address, for example,
addition, and multiplication. A list of such operations is given
below.
• Address + Address = illegal
• Address * Address = illegal
• Address % Address = illegal
• Address / Address = illegal
• Address & Address = illegal
• Address ^ Address = illegal
• Address | Address = illegal
• ~Address = illegal
20. Pointer to Pointer(Double pointer)
• In C, we can also define a pointer to store the
address of another pointer. Such pointer is known
as a double pointer (pointer to pointer).
• The first pointer is used to store the address of a
variable whereas the second pointer is used to store
the address of the first pointer.
22. Array of Pointers
• The way there can be an array of ints or an array of
floats, similarly there can be an array of pointers.
• Since a pointer variable always contains an address,
an array of pointers would be nothing but a collection
of addresses.
• The general format of declaring array of pointers is
as follows
• datatype *arr_name[sz];
• Example: int *arr[5];
• This indicates arr is the array which consists of
pointers point to integer data type.
26. How Are Pointers Related to Arrays
In C, the name of an array, is actually a pointer to the first
element of the array.
This basically means that we can work with arrays through pointers!
How? Since myNumbers is a pointer to the first element in myNumbers, you can use
the * operator to access it:
27. • To access the rest of the elements in myNumbers, you can increment
the pointer/array (+1, +2, etc):
• Or loop through it.
28. • It is also possible to change the value of array elements with pointers:
31. Null Pointer
• A Null Pointer is a pointer that does not point to any memory
location. It stores the base address of the segment. The null
pointer basically stores the Null value while void is the type
of the pointer.
• A null pointer is a special reserved value which is defined in
a stddef header file. Here, Null means that the pointer is
referring to the 0th memory location.
• If we do not have any address which is to be assigned to the
pointer, then it is known as a null pointer.
• When a NULL value is assigned to the pointer, then it is
considered as a Null pointer.
32. Applications of Null Pointer
• It is used to initialize o pointer variable when the pointer
does not point to a valid memory address.
• It is used to perform error handling with pointers before
dereferencing the pointers.
• It is passed as a function argument and to return from a
function when we do not want to pass the actual
memory address.
33.
34. Generic Pointer
• The void pointer in C is a pointer which is not
associated with any data types. It points to some
data location in the storage means points to the
address of variables.
• It is also called general purpose pointer. In C,
malloc() and calloc() functions return void * or
generic pointers.
It has some limitations −
• 1) Pointer arithmetic is not possible with void
pointer due to its concrete size.
• 2) It can’t be used as dereferenced.
36. Passing Pointer to Function
Call by Reference
• In call by reference, the address of the variable is passed
into the function call as the actual parameter.
• The value of the actual parameters can be modified by
changing the formal parameters since the address of the
actual parameters is passed.
• In call by reference, the memory allocation is similar for
both formal parameters and actual parameters. All the
operations in the function are performed on the value stored
at the address of the actual parameters, and the modified
value gets stored at the same address.
41. Accessing Structure using Pointer
• Pointer pointing to a structure variable is called a structure
pointer, and structures and pointers in C together can be
used to access and change the values of members of the
structure they are pointing.
• Declaring a structure pointer is similar to the declaration of
a structure variable. To declare a structure
pointer struct keyword is used followed by the structure
name and pointer name with an asterisk * symbol.
• Members of a structure can be accessed from pointers
using two ways that are.
– Using dot and asterisk operator on a pointer.
– Using arrow operator (->) on a pointer.
43. Self referential structures
• Same structure name is referred inside the
structure or a structure refer itself is known as
self-referential structure.
• A self-referential structure is one which contains a
pointer to its own type.
• Ex.
struct student
{
int rno;
char name[50];
struct student *p;
}s1;
46. Memory allocation for variable is two types.
– Static memory allocation.
– Dynamic memory allocation.
Static memory allocation (SMA) :- The process of
allocating memory to variables at compile time is
called SMA (or) compile time allocation.
• Ex:
– int a[10]= {10,20,30};
– float b[5]={0.5,1.1};
47. Advantages of Static memory allocation
– Simplicity of usage.
– Efficient execution time.
– Need not worry about memory allocation/re-
allocation/freeing of memory
– Variables remain permanently allocated.
Disadvantages of Static memory allocation
– Main disadvantage is wastage of memory.
– Memory can't be freed when it is no longer
needed.
48. Dynamic memory allocation (DMA) :- the process of
allocating memory to variables at the runtime is called DMA
(or) runtime allocation.
The concept of dynamic memory allocation in c
language enables the C programmer to allocate memory at
runtime. Dynamic memory allocation in c language is
possible by 4 functions of stdlib.h header file.
– malloc()
– calloc()
– realloc()
– free()
malloc() allocates single block of requested memory.
calloc() allocates multiple block of requested memory.
realloc() reallocates the memory occupied by malloc()
or calloc() functions.
free() frees the dynamically allocated memory.
49. Dynamic memory allocation (DMA) :- the process of
allocating memory to variables at the runtime is called DMA
(or) runtime allocation.
The concept of dynamic memory allocation in c
language enables the C programmer to allocate memory at
runtime. Dynamic memory allocation in c language is
possible by 4 functions of stdlib.h header file.
– malloc()
– calloc()
– realloc()
– free()
malloc() allocates single block of requested memory.
calloc() allocates multiple block of requested memory.
realloc() reallocates the memory occupied by malloc()
or calloc() functions.
free() frees the dynamically allocated memory.
50. static memory allocation & dynamic memory
allocation
static memory allocation dynamic memory allocation
memory is allocated at compile
time.
memory is allocated at run time.
memory can't be increased while
executing program.
memory can be increased while
executing program.
used in array. used in linked list.
51. • The malloc() function reserves a block of memory of the
specified number of bytes. And, it returns a pointer of void
which can be casted into pointers of any form.
Syntax of malloc()
– ptr = (castType*) malloc(size);
Example
– ptr = (float*) malloc(100 * sizeof(float));
• The above statement allocates 400 bytes of memory. It's
because the size of float is 4 bytes. And, the pointer ptr
holds the address of the first byte in the allocated memory.
• The expression results in a NULL pointer if the memory
cannot be allocated.
52. The name "calloc" stands for contiguous allocation.
The malloc() function allocates memory and leaves the
memory uninitialized, whereas the calloc() function allocates
memory and initializes all bits to zero.
Syntax of calloc()
– ptr = (castType*)calloc(n, size);
Example:
– ptr = (float*) calloc(25, sizeof(float));
• The above statement allocates contiguous space in memory
for 25 elements of type float.
53. free()
• Dynamically allocated memory created with either calloc()
or malloc() doesn't get freed on their own. You must
explicitly use free() to release the space.
Syntax of free()
– free(ptr);
• This statement frees the space allocated in the memory
pointed by ptr.
54. realloc()
• If the dynamically allocated memory is
insufficient or more than required, you can change
the size of previously allocated memory using the
realloc() function.
Syntax of realloc()
– ptr = realloc(ptr, x);
• Here, ptr is reallocated with a new size x.