(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
In C language a pointer is just a normal data type that can hold a address.
+
 
 +
=== ''Pointer is not at all a difficult concept. Pointer usage has created much difficulty for code maintenance and in debugging and hence the word spread "Pointers are difficult". But for a programmer writing codes in the order of 100s of lines, pointers shouldn't be difficult.'' ===
 +
 
 +
 
 +
 
 +
 
 +
In C language a pointer is just a normal data type that can hold an address
  
 
There is just two operators a programmer should know about pointer usage
 
There is just two operators a programmer should know about pointer usage
  &var => returns the address of var
+
  '''&var''' => returns the address of var
  *var => returns the content of value of var  
+
  '''*var''' => returns the content of value of var  
(it takes the value of var as address and returns the value of that address)
+
(it takes the value of var as address and returns the value of that address)
To make the confusion clear
+
 
  var => returns the value of var
+
Just to ensure no confusion is there
 +
  '''var''' => returns the value of var
  
 
A pointer can hold any address (any int value can be considered an address), but the programmer should ensure that the address is within the memory range allocated to the process, or otherwise a segmentation fault can occur. Two normal ways of doing this is  
 
A pointer can hold any address (any int value can be considered an address), but the programmer should ensure that the address is within the memory range allocated to the process, or otherwise a segmentation fault can occur. Two normal ways of doing this is  
  
#p = &var1;  
+
#'''p = &var1;''' All program variables are assigned addresses within the program space. Hence, we can take the address of any program variable and assign it to a pointer
All program variables are assigned addresses within the program space. Hence, we can take the address of any program variable and assign it to a pointer
+
#'''p = malloc(...);''' malloc is a function used to create memory dynamically (memory is created in heap while normal variables are stored in stack). This dynamic memory also falls within the address space of the program and hence is safe to assign to a pointer variable
#p = malloc(...);
 
malloc is a function used to create memory dynamically (memory is created in heap while normal variables are stored in stack). This dynamic memory also falls within the address space of the program and is hence safe to assign to a pointer variable
 
 
 
 
 
<div class="fb-like"  data-layout="standard" data-action="like" data-show-faces="false" data-share="true"></div>
 
  
  
<div class="fb-share-button"  data-type="button_count"></div>
+
{{Template:FBD}}
  
  
<div class="fb-comments"  data-href="http://armi.in/wiki/NP,_NP_Complete,_NP_Hard" data-numposts="5" data-colorscheme="light"  ></div>
+
[[Category: Code]]
[[Category: Algorithms & Data Structures]]
 
  
[[Category: CS Notes]]
+
[[Category: Programming in C Notes]]

Latest revision as of 10:03, 14 July 2014

Pointer is not at all a difficult concept. Pointer usage has created much difficulty for code maintenance and in debugging and hence the word spread "Pointers are difficult". But for a programmer writing codes in the order of 100s of lines, pointers shouldn't be difficult.

In C language a pointer is just a normal data type that can hold an address

There is just two operators a programmer should know about pointer usage

&var => returns the address of var
*var => returns the content of value of var 
(it takes the value of var as address and returns the value of that address)

Just to ensure no confusion is there

var => returns the value of var

A pointer can hold any address (any int value can be considered an address), but the programmer should ensure that the address is within the memory range allocated to the process, or otherwise a segmentation fault can occur. Two normal ways of doing this is

  1. p = &var1; All program variables are assigned addresses within the program space. Hence, we can take the address of any program variable and assign it to a pointer
  2. p = malloc(...); malloc is a function used to create memory dynamically (memory is created in heap while normal variables are stored in stack). This dynamic memory also falls within the address space of the program and hence is safe to assign to a pointer variable




blog comments powered by Disqus

In C language a pointer is just a normal data type that can hold a address.

There is just two operators a programmer should know about pointer usage

&var => returns the address of var
*var => returns the content of value of var 

(it takes the value of var as address and returns the value of that address) To make the confusion clear

var => returns the value of var

A pointer can hold any address (any int value can be considered an address), but the programmer should ensure that the address is within the memory range allocated to the process, or otherwise a segmentation fault can occur. Two normal ways of doing this is

  1. p = &var1;

All program variables are assigned addresses within the program space. Hence, we can take the address of any program variable and assign it to a pointer

  1. p = malloc(...);

malloc is a function used to create memory dynamically (memory is created in heap while normal variables are stored in stack). This dynamic memory also falls within the address space of the program and is hence safe to assign to a pointer variable