General Discussions

Pointers and Conventions (C)

This note covers various types of pointers to be aware of and a few notations used with pointers.

Types of Pointers

Pointer: Pointer is a variable which holds the address of another variable

NULL Pointer: A null pointer has a value reserved for indicating that the pointer does not refer to a valid object. It is pointer initialized to NULL value

Void Pointer: are pointers pointing to data of no specific data type. The compiler will have no idea on what type of object the pointer is pointing to. It has to be type casted to the required type

Dangling or Wild Pointer: are pointers that do not point to a valid object of the appropriate type. A normal pointer becomes dangling pointer when it is free’d.

Constant Pointer and Pointer to Constant:

Consider an example:
char  data = ‘D’;
char  * p = &data;

const char * p  — declares a pointer to a constant character. We cannot use this pointer to change the value being pointed to.

char * const p — declares a constant pointer to a character. The location stored in the pointer cannot change.

const char * const p  — declares a pointer to a character where both the pointer value and the value being pointed at will not change.

Other Pointer to be aware of: Near pointer, Far Pointer, Huge Pointer


Pointer Notations

int (*ptr) ( )

  • Function Pointer
  • Return type is integer
  • No parameters passed to the function
  • *ptr holds the address of the function (as good as name of the function)

 

int * ptr ( )

  • ptr is a function
  • No parameters passed to the function
  • Return type of the function is int *
  • Name of the function is ptr

 

*a[10]

  • array of pointers
  • Each location pointing to specified data type

 

(*a)[10]

  • a is a pointer to a group of contiguous 1-dimensional array elements

 

(*a)( )

  • a is a function pointer
  • Return type of the function is void
  • No parameters passed to the function
  • *a holds the address of the function
Advertisements

One thought on “Pointers and Conventions (C)

Let me Know What you Think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s