3. –Contains memory address as their values.
–Normal variable contains a specific value.
–Pointers contain address of a variable.
–We can have a pointer to any variable type.
4. 5/31/2014
• The unary or monadic operator & gives the
``address of a variable'‘
• The indirection or dereference
operator * gives the ``contents of an
object pointed to by a pointer''.
Indirection Operator *
5. 5/31/2014
1.We must associate a pointer to a particular Data type
2.Data type of pointer variable & variable must be same
3.Help us to know How many bytes of data is stored in?
int i=1,*ip; // pointer Declaration
ip=&i;//Pointer Assignment
*ip=3;//Pointer Assignment
7. When a pointer is declared it does not point
anywhere. You must set it to point somewhere
before you use it
5/31/2014
8. Pointer Arithmetic
• Operations are meaningless unless performed on an
array.
• An arithmetic operator increases and decreases its
contents by the size of the data type it points to it
• When we increment a pointer we increase pointer by
one ``block'' memory
9. • Call by value
– Pass the value of the variable to the
function.
x
void main()
{
int = 10;
print(x);
printf(“n%d”,x);
}
x)void print(inta
a += 10;
printf(“%d”,a);
}
10
120
20
10
10. • Do not pass a
variable.
• Pass address of variable using &
operator.
• Allows you to change the value directly at memory
location.
• Arrays are not passed with & because the array
name
is already a pointer.
print(x);
print(&x);
11. • Call by reference
– Pass the address of the variable to the
function.
x
void main()
{
int = 10;
prin(&x);
printf(“n%d”,x);
}
void prin(int *y)
{
*y += 10;
printf(“%d”,x);
}
&x
120
20
20
12. 5/31/2014
• pa = a; instead of pa = &a[0]
• a[i] can be written as *(pa + i).
13. 5/31/2014
• When an array is passed to a function what is
actually passed is its initial elements location in
memory
• strlen(s) strlen(&s[0])
• This is why we declare the function:
• int strlen(char s[]);
• An equivalent declaration is : int strlen(char *s);
14. • We can have arrays of pointers since pointers
are variables.
• Arrays can contain pointers
• For example: an array of strings
• Name array has a fixed size, but strings can be
of any size.
char *name[3] = { “rajan”, “sonu”, “hari”};
‘r’ ‘a’ ‘j’ ‘a’ ‘n’ ‘’
‘s’ ‘o’ ‘n’ ‘u’ ‘0’
‘h’ ‘a’ ‘r’ ‘i’ ‘0’
•
•
•
name[0]
name[1]
name[2]
14