0% found this document useful (0 votes)
22 views

Operations On Structures: The Other Way Is To Use - E.g.: Structure Variables of Struct Type Can Be Declared As Follows

The document discusses structures in C programming. It describes how to declare and initialize structures, use typedef to define structure types, pass structures as arguments to functions, and allocate structures dynamically using malloc. An example is provided that defines a struct to store personal data, initializes it by passing arguments to a constructor function, prints the struct, and allocates a string dynamically for storage.

Uploaded by

Justin Garcia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Operations On Structures: The Other Way Is To Use - E.g.: Structure Variables of Struct Type Can Be Declared As Follows

The document discusses structures in C programming. It describes how to declare and initialize structures, use typedef to define structure types, pass structures as arguments to functions, and allocate structures dynamically using malloc. An example is provided that defines a struct to store personal data, initializes it by passing arguments to a constructor function, prints the struct, and allocates a string dynamically for storage.

Uploaded by

Justin Garcia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C Programming

Structures
Declaration and Initialization

Operations on Structures
Using Typedef
The other way is to use typedef.
E.g.: structure variables of struct type myType can be declared as
follows.
typedef struct {
i n t number ;
c h a r name [MAXLEN+ 1 ] ;
int extra ;
} myType ; // myType can be u s e d a s a c o n v e n t i o n a l t y p e

myType s1 , s 2 ; // p r e f i x s t r u c t n o t r e q u i r e d now

R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 2/3


C Programming
Structures
Declaration and Initialization

Structure as Arguments
Example
t y p e d e f s t r u c t { // D e f i n i n g a s t r u c t u r e a s a t y p e
c h a r name [MAXLEN+ 1 ] ;
char sex ;
i n t a ge ;
i n t code ;
} p a r t ; // Comma r e q u i r e d

p a r t c o n s t r u c t ( c h a r ∗name , c h a r s e x , i n t age , i n t c o d e ) {
part p ;

// C r e a t e s t r u c t u r e w i t h i n p u t p a r a m e t e r s
s t r c p y ( p . name , name ) ;
p . age = age ;
p . code = code ;
p . sex = sex ;
r e t u r n p ; // R e t u r n t h e s t r u c t u r e
}

R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 3/3


C Programming
Structures
Declaration and Initialization

Structure as Arguments

Example
i n t r e a d L i n e ( c h a r s t r [ ] ) { // R e a d i n g a s t r i n g f o r name
i n t ch , i = 0 ;
w h i l e ( ( ch = g e t c h a r ( ) ) != ’ \n ’ )
i f ( i < MAXLEN)
s t r [ i ++] = ch ;
s t r [ i ] = ’ \0 ’ ;
return i ;
}
void p r i n t S t r u c t ( part p) {
p r i n t f ( ”%s ” , p . name ) ;
p r i n t f ( ”%c ” , p . sex ) ;
p r i n t f ( ”%d ” , p . a ge ) ;
p r i n t f ( ”%d \n” , p . c o d e ) ;
}

R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 3/3


C Programming
Structures
Declaration and Initialization

Structure as Arguments
Example
i n t main ( ) {
c h a r name [MAXLEN+1] , s ;
int a , c ;
part record ;

p r i n t f ( ” E n t e r name : ” ) ;
r e a d L i n e ( name ) ;
p r i n t f (” Enter gender : ” ) ;
s c a n f ( ”%c ” , &s ) ;
p r i n t f ( ” E n t e r a ge : ” ) ;
s c a n f ( ”%d” , &a ) ;
p r i n t f ( ” Enter code : ” ) ;
s c a n f ( ”%d” , &c ) ;
r e c o r d = c o n s t r u c t ( name , s , a , c ) ;
printStruct ( record );
}

R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 3/3


C Programming
Dynamic Allocation
Introduction

Dynamic Storage Allocation

Importance of Dynamic Allocation


Normally, C data structures are of fixed size
Once a program has been compiled, the length of an array is fixed.
Though the size of a variable length array is determined at runtime,
but remains fixed during the execution.
In many cases it is not possible to anticipate the size in advance.
Ideally data structures should grow and shrink according to program
requirements during execution.
C supports dynamic allocation.

R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 1/5


C Programming
Dynamic Allocation
Introduction

Dynamic Storage Allocation

Memory Allocation Functions


Functions are available in stdlib.h.
Three main functions defined therein are:
malloc: allocates a block of unitialized memory.
calloc: allocates a block of memory and clears it.
realloc: resize previously allocated block of memory.
Since he data types to be stored by user not know in advance,
allocation functions return void or generic pointers to allocated blocks.

R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 1/5


C Programming
Dynamic Allocation
Strings

Allocation for Strings


Using Malloc to Allocate for a String
#i n c l u d e <t i m e . h>
#i n c l u d e < s t d i o . h>
#i n c l u d e < s t d l i b . h>
char ∗ a l l o c S t r i n g ( i n t n ) {
char ∗ buf ;
buf = malloc (n + 1 ) ;
return buf ;
}
i n t main ( ) {
int i , len ;
char ∗ s ;

/∗ ∗∗ R e s t o f t h e program ∗∗ ∗/
}

R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 2/5


C Programming
Dynamic Allocation
Strings

Allocation for Strings


Using Malloc to Allocate for a String
p r i n t f ( ” Enter l e n g t h of s t r i n g needed : ” ) ;
s c a n f ( ”%d” , &l e n ) ;

s = allocString ( len );

s r a n d ( ( u n s i g n e d i n t ) t i m e (NULL ) ) ;

f o r ( i = 0 ; i < l e n ; i ++)
s [ i ] = r a n d ( ) % 26 + ’ a ’ ;
s [ l e n ] = ’ \0 ’ ;
p r i n t f ( ”%s \n” , s ) ;
free (s );

R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 2/5

You might also like