SlideShare une entreprise Scribd logo
1  sur  13
Télécharger pour lire hors ligne
main( ) 
{ 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
a[0] a[1] a[2] a[3] a[4] 
7 9 16 -2 8 
102 104 106 108 110 
int i ; 
printf ( ”%u %u %u”, &a[ 0 ], &a[ 1 ], &a[ 2 ] ) ; 
printf ( ”%u %u %u”, a, a + 1, a + 2 ) ; 
printf ( ”%d %d %d”, *a, *( a +1 ), *( a + 2 ) ) ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, *( a + i ) ) ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, a[ i ] ) ; 
} 
102 104 106 
102 104 106 
7 9 16 
Another Form... 
Subscript 
Notation 
Pointer 
Notation
More Forms... 
main( ) 
{ 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
int i ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, a[ i ] ) ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, *( a + i ) ) ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, *( i + a ) ) ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, i[ a ] ) ; 
} 
a[ i ] i[ a ] 
*( a + i ) *( i + a )
Flexible Arrays 
int a[ n ] ; 
scanf ( ”%d”, n ) ; 
Size of an array must always be mentioned 
as a positive, non-zero, integer constant
Pointer Arithmetic 
a 
b 
1008 2009 6002 
1012 
main( ) 
{ 
float a = 3.14 
, *b ; 
char ch = ’z’ 
, *dh ; 
int i = 25 
, *j ; 
b = a ; dh = ch ; j = i ; 
printf ( ”%u %u %u”, b, dh, j ) ; 
b++ ; dh++ ; j++ ; 
printf ( ”%u %u %u”, b, dh, j ) ; 
b += 3 ; dh += 8 ; j -= 3 ; 
printf ( ”%u %u %u”, b, dh, j ) ; 
} 
3.14 
ch i 
z 25 
1008 2009 6002 
1008 
dh j 
2009 6002 
1024 
2010 6004 
2018 5998
Legal Pointer Arithmetic 
Pointer + number 
Pointer - number 
Pointer - Pointer 
Pointer 
Pointer 
Number
Access Using Pointers 
main( ) 
{ 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
int *p ; int i ; 
p = a ; /* same as a[ 0 ] */ 
printf ( ”%d”, *p ) ; p++ ; 7 
printf ( ”%d”, p ) ; 104 
printf ( ”%d”, *p ) ; 
9 
for ( i = 0 ; i = 4 ; i++ ) 
printf ( ”%d”, *p ) ; 
p++ ; 
{ 
} 
} 
a[0] a[1] a[2] a[3] a[4] 
7 9 16 -2 8 
102 104 106 108 110 
p p 
102 
104 
p++ 
* ++ p++ ; 
p 
Ist 
* ++p ; 
IInd Ist 
IInd 
++ *p ; 
p = a ;
main( ) 
{ 
[ ] For Notation... 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
int *p ; int i ; 
p = a ; 
for ( i = 0 ; i = 4 ; i ++ ) 
printf ( ”%d %d %d %d”, 
} 
a[0] a[1] a[2] a[3] a[4] 
7 9 16 -2 8 
102 104 106 108 110 
*( p + i ), *( i + p ), 
p[ i ], i[ p ] ) ; 
int a[5] ; int 5[a] ; 
printf ( ”%d”, a[2] ) ; printf ( ”%d”, 2[a] ) ;  
int a[5] ; int 5[a] ; 
printf ( ”%d”, a[2] ) ; printf ( ”%d”, 2[a] ) ;
Dancing Dolls Revisited 
main( ) 
{ 
char far *s = 0xB8000000 ; int i ; 
for ( i = 0 ; i = 3999 ; i += 2 ) 
{ 
if ( *( s + i ) = ’A’  *( s + i ) = ’Z’ ) 
} 
} 
ss[[ ii ]] 
* ( i + s ) 
i[ s ] 
a[i] 
- One is pointer 
or array 
- Other is int
main( ) 
{ 
Changing Array Address 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
int *p ; int i ; 
p = a ; 
for ( i = 0 ; i = 4 ; i ++ ) 
printf ( ”%d”, *p ) ; 
p++ ; 
{ 
} 
} 
for ( i = 0 ; i = 4 ; i ++ ) 
printf ( ”%d”, *a ) ; 
a++ ; 
{ 
} Error 
a[0] a[1] a[2] a[3] a[4] 
7 9 16 -2 8 
102 104 106 108 110 
a-- ; 
a = a + 2 ; 
a = a - 2 ; 
a += 2 ; 
a -= 2 ; 
a-- ; 
a = a + 2 ; 
a = a - 2 ; 
a += 2 ; 
a -= 2 ;
Passing Array Elements 
main( ) 
{ 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
int i ; 
display ( a[ 0 ], a[ 1 ], a[ 2 ], a[ 3 ], a[ 4 ] ) ; 
for ( i = 0 ; i = 4 ; i++ ) 
display1 ( a[ i ] ) ; 
} 
display ( int i, int j, int k, int l, int m 
) 
{ 
printf ( ”%d %d %d %d %d”, i, j, k, l, m ) ; 
} 
display1 ( int n 
) 
{ 
printf ( ”%d”, n ) ; 
} 
Which 
is good? 
Which 
is good?
Passing Entire Array 
main( ) 
{ 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
display2 ( a ) ; 
display3 ( a ) ; 
, sizeof ( a ) / 2 - 1 
} display2 ( i n t * p ) 
int i ; 
for ( i = 0 ; i = 4 ; i ++ ) 
printf ( ”%d”, ) ; 
} 
*( p + i ) 
{ 
display3 ( i n t * p , int n 
) 
{ 
i = n 
int i ; 
for ( i = 0 ; i = 4 ; i ++ ) 
} printf ( ”%d”, * ( p + i ) ) ; 
a[0] a[1] a[2] a[3] a[4] 
7 9 16 -2 8 
102 104 106 108 110 
qqssoorrtt (( aa,, 55,, ,, ,, )) ;;
Remember... 
Any time an entire array is to be passed 
to function, it is necessary to pass 
(1) Base address of array 
(2) No. of elements present in the array
main( ) Two Dimensional Array 
{ int a[ ][ ] = { 
{ 2, 6, 1, 8, 4 } 
{ 1, 2, 5, 6, 8 } 
{ 7, 9, 8, 7, 21 } 
{ 4, 5, 6, 8, 10 } 
} ; 
optional 
int i, j ; 
printf ( ”%d”, a[ ][ ] ) ; 
2 4 
printf ( ”%d %d”, sizeof ( a ), a ) ; 
for ( i = 0 ; i = 3 ; i++ ) 
{ 
for ( j = 0 ; j = 4 ; j++ ) 
printf ( ”%d”, a [ i ][ j ] ) ; 
} printf ( ”n” ) ; 
} 
5 
optional 
int b[ ][1][2][3] 
compulsory 
21 
40 4080 
optional 
, 
, 
, 
compulsory 
compulsory 
EExxcceeppttiioonn

Contenu connexe

Tendances (20)

BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
programs
programsprograms
programs
 
Array
ArrayArray
Array
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
Vcs9
Vcs9Vcs9
Vcs9
 
Vcs5
Vcs5Vcs5
Vcs5
 
LET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERSLET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERS
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Ds
DsDs
Ds
 
Progr2
Progr2Progr2
Progr2
 
Pnno
PnnoPnno
Pnno
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
cosc 281 hw2
cosc 281 hw2cosc 281 hw2
cosc 281 hw2
 
Cpl
CplCpl
Cpl
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
 
10 2 배열 응용
10 2 배열 응용10 2 배열 응용
10 2 배열 응용
 

Similaire à Vcs17 (20)

Vcs16
Vcs16Vcs16
Vcs16
 
C programs
C programsC programs
C programs
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacion
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207
 
pointers 1
pointers 1pointers 1
pointers 1
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
#2
#2#2
#2
 
Array Programs.pdf
Array Programs.pdfArray Programs.pdf
Array Programs.pdf
 
Lab Question
Lab QuestionLab Question
Lab Question
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
C Programs.pptx
C Programs.pptxC Programs.pptx
C Programs.pptx
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
Arrays
ArraysArrays
Arrays
 

Plus de Malikireddy Bramhananda Reddy

DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYDATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyMalikireddy Bramhananda Reddy
 
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit orderMalikireddy Bramhananda Reddy
 

Plus de Malikireddy Bramhananda Reddy (20)

M v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notesM v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notes
 
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDYAVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
 
B-TREE PREPARED BY M V BRAHMANANDA REDDY
B-TREE PREPARED BY M V BRAHMANANDA REDDYB-TREE PREPARED BY M V BRAHMANANDA REDDY
B-TREE PREPARED BY M V BRAHMANANDA REDDY
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYDATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
 
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDYC LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
 
DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1
 
Data representation UNIT-1
Data representation UNIT-1Data representation UNIT-1
Data representation UNIT-1
 
C SLIDES PREPARED BY M V B REDDY
C SLIDES PREPARED BY  M V B REDDYC SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY M V B REDDY
 
C AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDYC AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDY
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
C LANGUAGE NOTES
C LANGUAGE NOTESC LANGUAGE NOTES
C LANGUAGE NOTES
 
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
 
Vcs29
Vcs29Vcs29
Vcs29
 
Vcs28
Vcs28Vcs28
Vcs28
 
Vcs26
Vcs26Vcs26
Vcs26
 
Vcs24
Vcs24Vcs24
Vcs24
 
Vcs23
Vcs23Vcs23
Vcs23
 
Vcs22
Vcs22Vcs22
Vcs22
 

Vcs17

  • 1. main( ) { int a[ ] = { 7, 9, 16, -2, 8 } ; a[0] a[1] a[2] a[3] a[4] 7 9 16 -2 8 102 104 106 108 110 int i ; printf ( ”%u %u %u”, &a[ 0 ], &a[ 1 ], &a[ 2 ] ) ; printf ( ”%u %u %u”, a, a + 1, a + 2 ) ; printf ( ”%d %d %d”, *a, *( a +1 ), *( a + 2 ) ) ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, *( a + i ) ) ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, a[ i ] ) ; } 102 104 106 102 104 106 7 9 16 Another Form... Subscript Notation Pointer Notation
  • 2. More Forms... main( ) { int a[ ] = { 7, 9, 16, -2, 8 } ; int i ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, a[ i ] ) ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, *( a + i ) ) ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, *( i + a ) ) ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, i[ a ] ) ; } a[ i ] i[ a ] *( a + i ) *( i + a )
  • 3. Flexible Arrays int a[ n ] ; scanf ( ”%d”, n ) ; Size of an array must always be mentioned as a positive, non-zero, integer constant
  • 4. Pointer Arithmetic a b 1008 2009 6002 1012 main( ) { float a = 3.14 , *b ; char ch = ’z’ , *dh ; int i = 25 , *j ; b = a ; dh = ch ; j = i ; printf ( ”%u %u %u”, b, dh, j ) ; b++ ; dh++ ; j++ ; printf ( ”%u %u %u”, b, dh, j ) ; b += 3 ; dh += 8 ; j -= 3 ; printf ( ”%u %u %u”, b, dh, j ) ; } 3.14 ch i z 25 1008 2009 6002 1008 dh j 2009 6002 1024 2010 6004 2018 5998
  • 5. Legal Pointer Arithmetic Pointer + number Pointer - number Pointer - Pointer Pointer Pointer Number
  • 6. Access Using Pointers main( ) { int a[ ] = { 7, 9, 16, -2, 8 } ; int *p ; int i ; p = a ; /* same as a[ 0 ] */ printf ( ”%d”, *p ) ; p++ ; 7 printf ( ”%d”, p ) ; 104 printf ( ”%d”, *p ) ; 9 for ( i = 0 ; i = 4 ; i++ ) printf ( ”%d”, *p ) ; p++ ; { } } a[0] a[1] a[2] a[3] a[4] 7 9 16 -2 8 102 104 106 108 110 p p 102 104 p++ * ++ p++ ; p Ist * ++p ; IInd Ist IInd ++ *p ; p = a ;
  • 7. main( ) { [ ] For Notation... int a[ ] = { 7, 9, 16, -2, 8 } ; int *p ; int i ; p = a ; for ( i = 0 ; i = 4 ; i ++ ) printf ( ”%d %d %d %d”, } a[0] a[1] a[2] a[3] a[4] 7 9 16 -2 8 102 104 106 108 110 *( p + i ), *( i + p ), p[ i ], i[ p ] ) ; int a[5] ; int 5[a] ; printf ( ”%d”, a[2] ) ; printf ( ”%d”, 2[a] ) ; int a[5] ; int 5[a] ; printf ( ”%d”, a[2] ) ; printf ( ”%d”, 2[a] ) ;
  • 8. Dancing Dolls Revisited main( ) { char far *s = 0xB8000000 ; int i ; for ( i = 0 ; i = 3999 ; i += 2 ) { if ( *( s + i ) = ’A’ *( s + i ) = ’Z’ ) } } ss[[ ii ]] * ( i + s ) i[ s ] a[i] - One is pointer or array - Other is int
  • 9. main( ) { Changing Array Address int a[ ] = { 7, 9, 16, -2, 8 } ; int *p ; int i ; p = a ; for ( i = 0 ; i = 4 ; i ++ ) printf ( ”%d”, *p ) ; p++ ; { } } for ( i = 0 ; i = 4 ; i ++ ) printf ( ”%d”, *a ) ; a++ ; { } Error a[0] a[1] a[2] a[3] a[4] 7 9 16 -2 8 102 104 106 108 110 a-- ; a = a + 2 ; a = a - 2 ; a += 2 ; a -= 2 ; a-- ; a = a + 2 ; a = a - 2 ; a += 2 ; a -= 2 ;
  • 10. Passing Array Elements main( ) { int a[ ] = { 7, 9, 16, -2, 8 } ; int i ; display ( a[ 0 ], a[ 1 ], a[ 2 ], a[ 3 ], a[ 4 ] ) ; for ( i = 0 ; i = 4 ; i++ ) display1 ( a[ i ] ) ; } display ( int i, int j, int k, int l, int m ) { printf ( ”%d %d %d %d %d”, i, j, k, l, m ) ; } display1 ( int n ) { printf ( ”%d”, n ) ; } Which is good? Which is good?
  • 11. Passing Entire Array main( ) { int a[ ] = { 7, 9, 16, -2, 8 } ; display2 ( a ) ; display3 ( a ) ; , sizeof ( a ) / 2 - 1 } display2 ( i n t * p ) int i ; for ( i = 0 ; i = 4 ; i ++ ) printf ( ”%d”, ) ; } *( p + i ) { display3 ( i n t * p , int n ) { i = n int i ; for ( i = 0 ; i = 4 ; i ++ ) } printf ( ”%d”, * ( p + i ) ) ; a[0] a[1] a[2] a[3] a[4] 7 9 16 -2 8 102 104 106 108 110 qqssoorrtt (( aa,, 55,, ,, ,, )) ;;
  • 12. Remember... Any time an entire array is to be passed to function, it is necessary to pass (1) Base address of array (2) No. of elements present in the array
  • 13. main( ) Two Dimensional Array { int a[ ][ ] = { { 2, 6, 1, 8, 4 } { 1, 2, 5, 6, 8 } { 7, 9, 8, 7, 21 } { 4, 5, 6, 8, 10 } } ; optional int i, j ; printf ( ”%d”, a[ ][ ] ) ; 2 4 printf ( ”%d %d”, sizeof ( a ), a ) ; for ( i = 0 ; i = 3 ; i++ ) { for ( j = 0 ; j = 4 ; j++ ) printf ( ”%d”, a [ i ][ j ] ) ; } printf ( ”n” ) ; } 5 optional int b[ ][1][2][3] compulsory 21 40 4080 optional , , , compulsory compulsory EExxcceeppttiioonn