C – Pointers

Pointer

  • It’s a pointer variable which can hold the address of another pointer variable.
  • That two variables should be same data type.
  • It de-refers twice to point to the data held by the designated pointer variable.
  • //Eg:    int x = 5, *p=&x, **q=&p;     //Therefore ‘x’ can be accessed by **q.
  • Advantage
    • Use pointer to increase the speed of program execution
    • Reduce program complexity
    • To use pointer to access the inside function’s variable value.
    • To use pointer to transfer the data from outside or inside the functions.
  • Access the address of the variable syntax :  &variable_name
  • Declaring pointers syntax: datatype *var;  Example : int *a;
  • Initializing pointer syntax :  pointer_name =&variable_name;  Example : int *a; int x; a =&x;
  • Pointer expressions : +  for addition, -  for subtraction, ++  for incremanet, --  for decrement.

What is a NULL pointer?

  • A pointer pointing to nothing is called so.
  • NULL pointer is always initialized.
  • NULL pointer assignment :  char *q=0;  (or) char *q=(char*)0;  (or) char *p=NULL;
  • Example
    • What is (void*)0 ? //Representation of NULL pointer
  • NULLMacro is defined in locale.h , stddef.h , stdio.h , stdlib.h , string.h , time.h , and wchar.h

What is a dangling pointer?

  • A pointer initially holding valid address, but later the held address is released or freed.
  • Then such a pointer is called as dangling pointer.

Near pointer

  • Near pointer occupies 2 bytes in DOS

What is Far Pointer?

  • far pointer occupies 4 bytes in DOS

huge pointer

  • huge pointer occupies 4 bytes in DOS

What is the advantage of declaring void pointers?

  • When we do not know what type of the memory address the pointer variable is going to hold, then we declare a void pointer for such.

What is a pointer to a function?

  • A pointer holding the reference of the function is called pointer to a function.

What is pointer to pointer in C?

  • one pointer refers to the address of another pointer

What is Function Pointer?

  • syntax : int(*fun)(int, int, int);
  • A function pointer is a variable that stores the address of a function that can later be called through that function pointer.
  • This is useful because functions encapsulate behavior.

What is a pointer on pointer?

  • It’s a pointer variable which can hold the address of another pointer variable.
  • It de-refers twice to point to the data held by the designated pointer variable.
  • Eg: int x = 5, *p=&x, **q=&p;
    • Therefore x  can be accessed by **q .

Pointer and Array

  • int (*ptr)[10]; //  ptr is a pointer to an array of 10 integers.

Reference Video

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

if you have any queries call us +918300026060 or WhatsApp Dismiss

0

Your Cart

%d bloggers like this: