Like this presentation? Why not share!

Pointer

by Learn By Watch on Apr 27, 2010

• 2,700 views

Views

Total Views
2,700
Views on SlideShare
2,512
Embed Views
188

Likes
4
320
0

7 Embeds188

 http://www.ustudy.in 137 http://en.learnbywatch.com 43 http://ustudy.in 3 http://www.slideshare.net 1 http://www.en.learnbywatch.com 1 https://www.facebook.com 1 http://www.facebook.com 1
More...

PointerPresentation Transcript

• C Programming Language By: Yogendra Pal yogendra@learnbywatch.com Dedicated to My mother and Father
• t y Keep your notebook with you. Write important point and questions that comes in your mind Solve Mind band exercise. C Rewind when not clear Ask Questions by call or SMS or by mail Keep Watching Keep Learning THIS IS POINTERS 2
• Pointer • Pointer variable – Contains memory address as their values. – Normal variable contains a specific value. – Pointers contain address of a variable. name ptr name a • address 1090 9 address 1004 3
• Pointer… • Pointer definitions – * used with pointer variables int *ptr; – Defines a pointer to an int (pointer of type int *). – Can define pointers to any data type. – Initialize pointers to 0, NULL, or an address • 0 or NULL – points to nothing (NULL preferred) • & (address operator) – Return address of operand 4
• Pointer… • Initialize a pointer variable with the address of a variable. int *ptr , x = 10; ptr = &x; ptr x • 1090 10 1000 Data type of pointer variable & variable must be same. 5
• Pointer… • * (indirection/dereferencing) operator – Returns the value of the corresponding memory address. • *ptr and ptr are different variable and hence has different value. – ptr is the address of x (1000). – *ptr is the value at 1000 memory address. * and & are inverse 6
• *ptr ptr x • 1090 9 10 1000 &ptr ptr *ptr = 9; y y = *ptr; 9 1098 7
• Pointer Operators… Operators Associativity Type () [] left to right highest + - ++ -- ! (type) right to left unary * & * / % left to right multiplicative + - left to right additive < <= > >= left to right relational == != left to right equality && left to right logical and || left to right logical or ?: right to left conditional = += -= *= /= %= right to left assignment , left to right Comma 8
• Pointer and Function • Call by value – Pass the value of the variable to the function. void main() void print(int x) { { 10 int x = 10; x += 10; print(x); printf(“%d”,x); printf(“n%d”,x); } 20 10 } 20 10 9
• Pointer and Function • Do not pass a variable. print(x); • Pass address of variable using & operator. print(&x); • Allows you to change the value directly at memory location. • Arrays are not passed with & because the array name is already a pointer. 10
• Pointer and Function • An address can handle by a pointer only. • Use * operator for variable inside of function. print(int *x); • *x is used as an alias for the variable x. 11
• Pointer and Function • Call by reference – Pass the address of the variable to the function. void main() void print(int *y) { { 20 10 int x = 10; *y += 10; print(&x); printf(“%d”,x); printf(“n%d”,x); } &x } 20 20 12
• Pointer Arithmetic • ++ or -- : Increment or Decrement • +, +=,-, -= : Add or subtract an integer from pointer. • Operations meaningless unless performed on an array. int a[5] a a+1 a+2 a+3 a+4 1000 1004 1008 1012 1016 13
• Pointer Arithmetic • Subtracting pointers – Returns number of elements. int x[10], *a, *b; a = &x[4]; b = &x[2]; printf(“%d”,a-b); • Pointer comparison ( <, == , > ) – Check which pointer points to the higher numbered array element. 14
• Pointer and Array • Array name is a pointer to the first element of the array. int a[10]; a 0 1 2 3 4 5 6 7 8 9 • a = &a[0]; • a[0] = *a; 15
• Pointer and Array 0 1 2 3 4 5 6 7 8 9 a a+1a+2a+3a+4a+5a+6a+7 a+8 a+9 • Using + we can access the next element or the next memory address. • Use * operator to access the value. – *(a+2) = a[2] – a[i] = *(a+i) 16
• Pass Array to a Function • Pass name and size of the array to the function. • Problem: write a function (printa()) that print all elements of an array. • Write a function (scana()) that scan all elements of an array. • Put above functions in a file named “astdio.h” & create main function in other file. 17
• Character Pointer • A character pointer points to a character variable. char *ptr; ptr = “hello”; h e l l o 0 18
• Arrays of Pointers • Arrays can contain pointers • For example: an array of strings char *name[3] = { “rajan”, “sonu”, “hari”}; • name array has a fixed size, but strings can be of any size. name[0] • ‘r’ ‘a’ ‘j’ ‘a’ ‘n’ ‘0’ name[1] • ‘s’ ‘o’ ‘n’ ‘u’ ‘0’ name[2] • ‘h’ ‘a’ ‘r’ ‘i’ ‘0’ 19
• Pointer to Pointers • A pointer that point to another pointer. • Use to read: – Character array. – Two dimensional array. 20
• Pointer to Pointers • Declaration char **reader; • This can be used to read a character array. char *name[3] = {“sonu”, “raju”, “hari”}; reader = name; 21
• Two Dimensional Array • name[5][6]; • name is the address of first element. • We can say it array of pointers. • name[0] points to first row. • name[1] points to second row. • name[0] is equivalent to name. • name[1] is equivalent to (name+1). • ith element can be access by *(name + i). 22
• Two Dimensional Array • To access the jth element of ith row. • name [i][j] • *(name[i] + j) • *(*(name + i) + j) 23
• Mind Bend • Write a program which performs the following tasks: – Initialize an integer array of 10 elements in main(). – Pass the entire array to a function modify(). – In modify() multiply each elements of array by 3. – Return the control to main() and print the new array elements in main(). 24
• To get complete benefit of this tutorial solve all the quiz on www.learnbywatch.com For any problem in this tutorial mail me at yogendra@learnbywatch.com with the subject “C” For Other information mail at info@learnbywatch.com Keep Watching Keep Learning NEXT IS STRUCTURES 25