SlideShare a Scribd company logo
Arrays 
main( ) 
{ 
int m1, m2, m3, i, per [ 1 0 ] ; 
for ( i = 0 ; i <= 9 ; i++ ) 
{ 
printf ( ”Enter marks” ) ; 
scanf ( ”%d %d %d”, &m1, &m2, &m3 ) ; 
[ i ] 
per = ( m1 + m2 + m3 ) / 3 ; 
printf ( ”%d”, per [ i ] ) ; 
} } 
No Flexibility
# include ”alloc.h” 
main( ) 
{ 
int m1, m2, m3, i, per ; 
int *p ; 
printf ( ”Enter no. of students” ) ; 
scanf ( ”%d”, &n ) ; 
for ( i = 0 ; i < n ; i++ ) 
malloc ( n * 2 ) ; 
{ 
scanf ( ”%d%d%d”, &m1, &m2, 
&m3 ) ; 
per = ( m1 + m2 + m3 ) / 3 ; 
* ( p + i ) = per ; 
} 
for ( i = 0 ; i < n ; i++ ) 
printf ( ”%d”, * ( p + i ) ) ; 
} 
MMeemmoorryy 
n* 2 
p 
Dynamic Allocation 
p = malloc ( n * 2 ) ; 
( int * ) malloc ( n * 2 ) ;
Memory Allocation 
Array 
Static 
Compile Time 
malloc( ) 
Dynamic 
Execution Time
Better Still... 
int *p[ ] ; 
char ch = ’Y’ ; 
while ( ch = = ’Y’ ) 
{ 
scanf ( ”%d %d %d”, &m1, &m2, &m3 ) ; 
per = ( m1 + m2 + m3 ) / 3 ; 
pp malloc [= i ]( =in (( t i*2 n ) t m* ; 
)a mlloacll o( c2 () 2; ) ; 
ch = getche( ) ; 
} 
p p p 
55 63 28 45 
Memory Leaks 
? 
p = malloc ( 2 ) ; 
**p( p= + p ie )r =; per ; 
printf ( ”Another student y/n” ) ;
Memory Leak? 
main( ) 
{ 
int *j ; int *f( ) ; 
j 
400 
j = 
f( ) ; 
printf ( ”%d”, *j ) ; 
a 
25 
} Memory Leaks 
f( int ) 
* f( ) 
{ 
int a = 25 ; 
return ( a ) ; 
} 
400 
& 
Dangling 
Pointer
55 400 63 28 45 60 
400 
node 
Linked List 
750 
750 
900 
200 900 
120 
NNUULLLL 
120 
55 63 28 45 60 
NNUULLLL 
data 
link 
Best...
main( ) 
{ 
struct node 
{ 
p q r s 
55 63 28 45 N 
int data ; 
struct node *link ; 
} ; 
struct node *p, *q, *r, *s ; 
malloc p = mallosciz ( (e soifz e( osft r(u scttr uncotd neo )de ) ; 
) ) ; 
q = ( struct node * ) malloc ( sizeof ( struct node ) ) ; 
r = 
. . . 
s = 
. . . 
p  data = 55 ;q  data = 63 ; r  data = 28 ; 
s  data = 45 ; 
p  link = q ; q  link = r ; r  link = s ; 
s  link = NULL ; 
CCoonntt...... 
s 
Linked List 
( struct node * ) malloc ( sizeof ( struct node ) ) ;
....CCoonntt p q r s 
# include ”alloc.h” 
main( ) 
{ ... 
... 
... 
... 
s 
55 63 28 45 N 
t t t 
printf ( ”%d %d %d %d”, p  data, q  data, 
r  data, s  data ) ; 
printf ( ”%d %d %d”, p  data, p  link  data, 
p  link  link  data ) ; 
t = p ; 
while ( t ! = N U L L ) 
{ 
printf ( ”%d”, t  data ) ; t = t  link ; 
} 
}
# include ”alloc.h” 
struct node 
{ int per ; struct node *link ; 
} ; 
main( ) 
{ 
struct node *p ; 
char ch = ’Y’ ; int pp ; p = NULL ; 
while ( ch == ’Y’ ) 
{ 
scanf ( ”%d %d %d”, m1, m2, m3 ) ; 
pp = ( m1 + m2 + m3 ) / 3 ; 
add ( ) ; 
printf ( ”Another student y/n” ) ; 
} ch = getche( ) ; 
} 
pp 
Most General
# include ”alloc.h” 
struct node 
{ int per ; struct node *link ; 
} ; 
main( ) 
{ 
struct node *p ; 
char ch = ’Y’ ; int pp ; p = NULL ; 
while ( ch == ’Y’ ) 
{ 
scanf ( ”%d %d %d”, m1, m2, m3 ) ; 
pp = ( m1 + m2 + m3 ) / 3 ; 
add ( ) ; 
printf ( ”Another student y/n” ) ; 
} ch = getche( ) ; 
} 
pp 
Most General

More Related Content

DOCX
1 (1)
PPT
C questions
PPTX
Function basics
PDF
SATySFiのこれからの課題たち
PPTX
Computer lab (programs)
DOCX
New microsoft office word document
1 (1)
C questions
Function basics
SATySFiのこれからの課題たち
Computer lab (programs)
New microsoft office word document

What's hot (15)

PPTX
Double linked list
PDF
Implementing string
DOCX
cosc 281 hw2
PPTX
PDF
Insertion sort
PDF
Bcsl 033 data and file structures lab s2-1
PDF
DOC
Final ds record
PDF
Bcsl 033 data and file structures lab s3-3
PPTX
Double linked list
PPTX
Single linked list
DOCX
Pratik Bakane C++
Double linked list
Implementing string
cosc 281 hw2
Insertion sort
Bcsl 033 data and file structures lab s2-1
Final ds record
Bcsl 033 data and file structures lab s3-3
Double linked list
Single linked list
Pratik Bakane C++
Ad

Viewers also liked (17)

PPTX
STACKS AND QUEUES CONCEPTS
PPT
B-TREE PREPARED BY M V BRAHMANANDA REDDY
PPTX
PPT
List build linked in - jigsaw - salesforce - free tools
PDF
Presentation Ukay Metal Ind.Pvt.Ltd.-Ashok 7-2015
PDF
Presentation ukay 2014 - ashok 05.2014
PDF
Presentation ukay ashok 2-2015
PDF
C SLIDES PREPARED BY M V B REDDY
PDF
Presentation ukay 2014 - ashok 17.02.2014
PPT
RIM(Reaction Injection Moulding)
STACKS AND QUEUES CONCEPTS
B-TREE PREPARED BY M V BRAHMANANDA REDDY
List build linked in - jigsaw - salesforce - free tools
Presentation Ukay Metal Ind.Pvt.Ltd.-Ashok 7-2015
Presentation ukay 2014 - ashok 05.2014
Presentation ukay ashok 2-2015
C SLIDES PREPARED BY M V B REDDY
Presentation ukay 2014 - ashok 17.02.2014
RIM(Reaction Injection Moulding)
Ad

Similar to Vcs29 (20)

DOC
C basics
PDF
9.C Programming
PDF
Program of sorting using shell sort #include stdio.h #de.pdf
DOCX
C lab manaual
PPTX
Algoritmos e Estruturas de Dados - dynamic memory allocation
DOC
Basic c programs updated on 31.8.2020
DOCX
PDF
Data Structure using C
PPTX
C Programming Language Part 8
DOC
Sorting programs
PPTX
L25-L26-Parameter passing techniques.pptx
DOCX
SaraPIC
DOCX
DataStructures notes
DOCX
Data Structures Using C Practical File
PPTX
Intoduction to dynamic memory allocation
PDF
programs
DOCX
(Meta 5) ejemplo vectores dev c++
DOCX
(Meta 5) ejemplo vectores 2 dev c++
DOCX
cpract.docx
C basics
9.C Programming
Program of sorting using shell sort #include stdio.h #de.pdf
C lab manaual
Algoritmos e Estruturas de Dados - dynamic memory allocation
Basic c programs updated on 31.8.2020
Data Structure using C
C Programming Language Part 8
Sorting programs
L25-L26-Parameter passing techniques.pptx
SaraPIC
DataStructures notes
Data Structures Using C Practical File
Intoduction to dynamic memory allocation
programs
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores 2 dev c++
cpract.docx

More from Malikireddy Bramhananda Reddy (20)

PDF
M v bramhananda reddy dsa complete notes
PPT
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
PPT
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
PPT
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DOCX
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
PPTX
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
PPT
DATASTRUCTURES UNIT-1
PPT
Data representation UNIT-1
DOC
C AND DATASTRUCTURES PREPARED BY M V B REDDY
DOCX
DOC
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
M v bramhananda reddy dsa complete notes
AVL TREE 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
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES UNIT-1
Data representation UNIT-1
C AND DATASTRUCTURES PREPARED BY M V B REDDY
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order

Vcs29

  • 1. Arrays main( ) { int m1, m2, m3, i, per [ 1 0 ] ; for ( i = 0 ; i <= 9 ; i++ ) { printf ( ”Enter marks” ) ; scanf ( ”%d %d %d”, &m1, &m2, &m3 ) ; [ i ] per = ( m1 + m2 + m3 ) / 3 ; printf ( ”%d”, per [ i ] ) ; } } No Flexibility
  • 2. # include ”alloc.h” main( ) { int m1, m2, m3, i, per ; int *p ; printf ( ”Enter no. of students” ) ; scanf ( ”%d”, &n ) ; for ( i = 0 ; i < n ; i++ ) malloc ( n * 2 ) ; { scanf ( ”%d%d%d”, &m1, &m2, &m3 ) ; per = ( m1 + m2 + m3 ) / 3 ; * ( p + i ) = per ; } for ( i = 0 ; i < n ; i++ ) printf ( ”%d”, * ( p + i ) ) ; } MMeemmoorryy n* 2 p Dynamic Allocation p = malloc ( n * 2 ) ; ( int * ) malloc ( n * 2 ) ;
  • 3. Memory Allocation Array Static Compile Time malloc( ) Dynamic Execution Time
  • 4. Better Still... int *p[ ] ; char ch = ’Y’ ; while ( ch = = ’Y’ ) { scanf ( ”%d %d %d”, &m1, &m2, &m3 ) ; per = ( m1 + m2 + m3 ) / 3 ; pp malloc [= i ]( =in (( t i*2 n ) t m* ; )a mlloacll o( c2 () 2; ) ; ch = getche( ) ; } p p p 55 63 28 45 Memory Leaks ? p = malloc ( 2 ) ; **p( p= + p ie )r =; per ; printf ( ”Another student y/n” ) ;
  • 5. Memory Leak? main( ) { int *j ; int *f( ) ; j 400 j = f( ) ; printf ( ”%d”, *j ) ; a 25 } Memory Leaks f( int ) * f( ) { int a = 25 ; return ( a ) ; } 400 & Dangling Pointer
  • 6. 55 400 63 28 45 60 400 node Linked List 750 750 900 200 900 120 NNUULLLL 120 55 63 28 45 60 NNUULLLL data link Best...
  • 7. main( ) { struct node { p q r s 55 63 28 45 N int data ; struct node *link ; } ; struct node *p, *q, *r, *s ; malloc p = mallosciz ( (e soifz e( osft r(u scttr uncotd neo )de ) ; ) ) ; q = ( struct node * ) malloc ( sizeof ( struct node ) ) ; r = . . . s = . . . p data = 55 ;q data = 63 ; r data = 28 ; s data = 45 ; p link = q ; q link = r ; r link = s ; s link = NULL ; CCoonntt...... s Linked List ( struct node * ) malloc ( sizeof ( struct node ) ) ;
  • 8. ....CCoonntt p q r s # include ”alloc.h” main( ) { ... ... ... ... s 55 63 28 45 N t t t printf ( ”%d %d %d %d”, p data, q data, r data, s data ) ; printf ( ”%d %d %d”, p data, p link data, p link link data ) ; t = p ; while ( t ! = N U L L ) { printf ( ”%d”, t data ) ; t = t link ; } }
  • 9. # include ”alloc.h” struct node { int per ; struct node *link ; } ; main( ) { struct node *p ; char ch = ’Y’ ; int pp ; p = NULL ; while ( ch == ’Y’ ) { scanf ( ”%d %d %d”, m1, m2, m3 ) ; pp = ( m1 + m2 + m3 ) / 3 ; add ( ) ; printf ( ”Another student y/n” ) ; } ch = getche( ) ; } pp Most General
  • 10. # include ”alloc.h” struct node { int per ; struct node *link ; } ; main( ) { struct node *p ; char ch = ’Y’ ; int pp ; p = NULL ; while ( ch == ’Y’ ) { scanf ( ”%d %d %d”, m1, m2, m3 ) ; pp = ( m1 + m2 + m3 ) / 3 ; add ( ) ; printf ( ”Another student y/n” ) ; } ch = getche( ) ; } pp Most General