Mar 13

C uses pointers in 3 main ways :

  1. To create dynamic Data Structures.
  2. To handle variable parameters passed to functions. (Call by Reference)
  3. Accessing array or string elements.

A normal variable is a location in memory that can hold a value. Pointer is a variable that points to another variable. It holds the memory address of another variable.

Example

int main()

{

int *j , i;

j = &i ;   /* & -> address of */

return 0;

}

As pointer holds an address rather than a value ,it has 2 parts :

  1. The pointer itself holds the address. That address points to a value. (&i)
  2. There is a pointer and value pointed to. (*j)

When we declare a variable in our program, the compiler immediately assigns a specific block of memory to hold the value of that variable.

A pointer variable points to another variable by holding its address.

written by Shweta

Mar 12

What is a Pointer variable?

A pointer variable is a special variable which holds the address (memory location) of another variable.

e.g

Code snippet :

int main(){

int  x = 10 ;        // normal variable

int  *ptr = &x;   // here ‘ptr‘ is pointer variable  and initialised with ‘address of x

printf(”value of x is %d”, *ptr);

return 0;

}

output :

value of x is 10

Explantion

  • &‘  when used with variable name (here ‘x‘) represents the ‘address of that variable‘. Thus  ‘ptr‘ stores the address of x .
  • *ptr‘  represents the value stored at the location hold by ‘ptr‘. This is known as ‘dereferencing a pointer variable’.

Now, What is Function Pointer ?

A Function pointer is a pointer variable that holds the Address of a Function.

Note : In low level code generation every function call is replaced by the address of the function.

Function name itself represents the address of a Function.

Function Pointer Syntax :

1) Function Pointers are declared as
<return_type>  (* <function_pointer_variable>)(<argument types>)

e.g

note : return type and argument types or number may change as per the requirement

int (*function_ptr) (int,int) =NULL;

  • function_ptr is pointer to a function which takes two int arguments and returns an int.
  • funcion_ptr can take the address of any function which takes two int arguments and returns an int.
  • Assigning address of a function with a different prototype is “incompatible pointer assignment”.

2) Initialisation of Function Pointer

<function_pointer_variable> = <function_name>

or

<function_pointer_variable> =  &<function_name>

e.g

Let the function pointer variable be function_ptr and the function name be ‘sum

then, as per initialisation rule :

function_ptr = sum;

or

function_ptr = &sum;

3) Dereferencing of Function Pointer

<function_pointer_variable>(<arguments>)

or

(*<function_pointer_variable>)(<arguments>)

e.g

Let the function pointer variable be function_ptr

then, as per dereferencing rule :

note : arguments are of int types as per above declaration

function_ptr(12,23);

or

(*function_ptr)(12,23);


Example code given below for function pointer usage relates everything with above given pointer example.

e.g

Code snippet :

//function definition

int sum(int a, int b)
{
return a + b;
}

int main(){

//declaration as well as initialisation of function pointer

int (*function_ptr) (int,int) = sum ;

printf(”Sum of  10 and 20 is %d”, function_ptr(10,20));

return 0;

}

output :

Sum of  10 and 20 is 30

Explantion

  • First we define the function ‘sum‘ and then we declare the ‘function pointer’  named ‘function_ptr‘ and initialises it with the ‘address of sum function‘ .
  • Then we ‘deference the function pointer’ by invoking ‘function_ptr(10,20)‘ . It indirectly invokes ‘sum(10,20)‘.

Note : Signature (return type as well as argument types and number) of function pointer should match with the signature of the function whose address it is going to hold.

written by Anup

Mar 05

There are four segments in C Program. Data, Code, Stack and Heap Segments. All this sections define the scope & storage class of the data and flow of control between the instructions.

memory_layout

Code segment or text segment: Code segment contains the code executable or code  binary.

Data segment: Data segment is sub divided into two parts

  • Initialized data segment
  • Uninitialized data segment

Initialized data segment: All the global, static and constant data are stored in the data segment.

Uninitialized data segment: All the uninitialized data are stored in BSS.

Heap: When program allocate memory at runtime using calloc and malloc function, then memory gets allocated in heap. when some more memory need to be allocated using calloc and malloc function, heap grows upward as shown in above diagram.

Stack: Stack is used to store your local variables and is used for passing arguments to the functions along with the return address of the instruction which is to be executed after the function call is over. When a new stack frame needs to be added (as a result of a newly called function), the stack grows downward.

written by Anup