advanced pointers c tutorial

Bodhak advanced pointers c tutorial in this chapter we will discuss in details about Memory Models, Working with various types of pointers, near pointer, far pointer, huge pointer, void pointer, constant pointer, pointer constant,       dangling pointer, pointer to pointer.

As we are in discussions of memory and its address and the sizes, in the earlier implementation we had considered that each variable occupies the specific size of memory in the form of a bit with respect to data types.

 

advanced pointers c tutorial

But nowhere we had proved practically. We can check the memory size occupied by each data type by using sizeof() operator.

Near pointer

A near pointer is one where we use to point only the offset – it can only address the current segment.

The pointer which can points only 64KB data segment or segment number 8 is known as near pointer.

Near pointer is 16 bits wide and holds only offset address. This can point up to 64kb or 0xFFFF offset in a given segment.

far pointer

A far pointer has both components, so can address any of the addressable 32-bit space. However, you can see that the same pointer can be expressed in different ways, depending on where you draw segment boundaries.

The pointer which can point or access whole the residence memory of RAM i.e. which can access all 16 segments is known as far pointer

huge pointer

A huge pointer is a far pointer that has had as much as possible expressed in the segment.

The pointer which can point or access whole the residence memory of RAM i.e. which can access all the 16 segments is known as a huge pointer.

void pointer

A void pointer is a pointer which has no specified data type. The keyword ‘void’ precedes the pointer variable because the data type is not specific and known as a generic pointer. We use the void pointer to point to any type.

These type of pointers are the one which cannot change the address they are pointing to. This means that suppose there is a pointer which points to a variable (or stores the address of that variable). Now if we try to point the pointer to some other variable (or try to make the pointer store address of some other variable), then pointers constant are incapable of this.

Pointer constant

These type of pointers are the one which cannot change the address they are pointing to. This means that suppose there is a pointer which points to a variable (or stores the address of that variable). Now if we try to point the pointer to some other variable (or try to make the pointer store address of some other variable), then pointers constant are incapable of this.

Syntax : ‘int *const ptr’

Pointer constant example:

include<stdio.h>

int main(void)

{

int a[] = {10,11};

int* const ptr = a;

*ptr = 11;

printf(“\n value at ptr is : [%d]\n”,*ptr);

printf(“\n Address pointed by ptr : [%p]\n”,(unsigned int*)ptr);

ptr++;

printf(“\n Address pointed by ptr : [%p]\n”,(unsigned int*)ptr);

return 0;

}

Constant pointer:

These type of pointers are the one which cannot change the value they are pointing to. This means they cannot change the value of the variable whose address they are holding.

Syntax : ‘const int *ptr’

dangling pointer:

while a pointer is still pointing such memory If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory or location.

pointer to pointer:

As the definition of pointer says that it’s a special variable that can store the address of the other variable. Then the other variable can very well be a pointer. This means that it’s perfectly legal for a pointer to be pointing to another pointer.

NULL pointer:

The literal meaning of NULL pointer is a pointer which is pointing to nothing. The null pointer points the base address of the segment.

Examples of NULL pointer:

  1. 1. int*ptr=(char *)0;
  2. 2. float *ptr=(float *)0;
  3. 3. char *ptr=(char *)0;
  4. 4. double *ptr=(double *)0;
  5. 5. char *ptr=’\0’;
  6. 6. int*ptr=NULL;

Wild Pointer:

Not initialized pointer.

Example:
#include<stdio.h>

int main()

{
int *ptr;
printf(“%u\n”,ptr);

printf(“%d”,*ptr);
return 0;

}