Looking for a Tutor Near You?

Post Learning Requirement »
x

Choose Country Code

x

Direction

x

Ask a Question

x

x
x
x
Hire a Tutor

C Language

Loading...

Published in: C / C++
1,140 Views

Simplified Concepts And Implementation of C.

Suman / Gurgaon

6 years of teaching experience

Qualification: B.Tech

Teaches: Computer, IT, C / C++, Java And J2EE, Python Programming, MCA Subjects, Networking, Software Testing, Unix/Linux

Contact this Tutor
  1. Structures & Unions Heterogenous collection of items. Structures are derived types as per C language. struct struct tag { int a; char b; Struct _ tag is a new type using which can be declrared further. variables (objects) struct struct _ tag objectl, object 2, struct struct tag { int char b; No memory allocated so compilation error int i struct struct tag { int a; char b; object3; 'p'}; struct struct struct struct struct struct output: printf( printf( Input: scanf ( " scanf ( " &sl.b = operator: sl.a sl.b tag tag tag sl.a); sl.b); &sl.a); &sl.b); &(sl.b) Binary operator. Requires a Structure OR union object on the left side and a filed name from the structure or union on the right side. struct struct tag *sp=&sl; struct struct _ tag sp is a pointer to struct struct _ tag. - > operator
  2. sp b sp sp->a) sp->b) &sp->a) &sp->b); sl s2 10 x' printf( printf( Input : scanf ( " scanf ( "%c\n" , &sp->b printf( printf( &(sp->b) sl.a); sl.b); struct struct tag { int a ; char b ; print the fields of SI, s2 and s3. a'}, x'}, s3; sl sl - //Error //Error //Error VALID sizeof of structure: struct X { int a ; char b; struct X { int a ; char b ; int c ; struct X { int a; char b ; char bl; int c ; struct X { int a ; char b ; short bl; int c ; struct X { int a ;
  3. char b; char bl; short b2; int c; struct X { int a; char b; short b2; int c; char bl; struct X { int a; char b; struct X { short a; char b; struct X { short a; char b; char bl; struct X { char bl; short a; char b; bl--l padding 5 b bl b 2 1 --1 2 1 4 so uniformly size is set at 6 Array of structures: struct X x [4] • b field of the 4 th object in the array: Read the values for all the fields of x and display back to the screen
  4. Arrays in structures: struct student { int id; char name [20] • int marks[3] • struct student sl,s2; second subject marks of student one sl.marks[l] scanf &sl.marks[i] scanf ( "%19s " , sl . name) ; f gets(sl. name, sizeof(sl. name), Structures within structures: X { int a; char b;} stdin); struct Y { int a; struct struct X x; char b; ---- int struct X int char char Y { int a; struct struct X x; char b; { 10, {20, 20 'P' 'q' struct y.x.a---2@ struct y.x.a---2@ struct 'p'}/ 'q'} Y { int a; struct X x; char b; { 10, {20}, 'q' Y { int a; struct X x; char b; { 10, 20, 'q' Y { int a;
  5. struct X x; char b; { 10, {20, y.x.a---2@ 'q' Incomplete structures: x' 'Y'} struct X struct X x; // Incomplete struct. // ERROR struct X * xp=NULL; // VALID Size of Pointer XP is not dependent on sizeof(struct X) sizeof( XP) will sizeof( struct X struct X { int a; struct X x; sizeof( struct X struct X { int a; struct Y { int a; be pointer size. . ) and sizeof( *XP) are unknown... char b; } ; // struct X COMPLETED NOT AN ERROR ) and sizeof( *XP) ARE KNOWN. . char b;} struct Y x; char b; compilation error Self containing structures-- struct Y { int a; struct Y *sp; char b; pointer Y. sp self referential structures struct X struct X struct { int a; { int a; { int a; No further objects struct { int a; char b;} x; char b;} ; char b;} xl; of type xl can char b;} xl,x2,x3, not permitted in C allowed in C // tag & object // tag // object be created. // valid objects single- ton obj ects struct X { int a; char b;} struct X *xp; Pointer to a single ton object;
  6. struct &xl &xl->a &(xl->a) ERROR xl.b { int a; char b;} Ptr to object xl xl x'}, *xp; // object (&xl) ->a (&xl) ->b XP = &xl; xp->a xp->b -30 x' 30 x' differing types: struct X { int a}x; struct Y { int a}y; int i; x and i are of differing types. struct X is a different type from struct Y x and i; x i=x.a; y are of differing types. // ERROR // ERROR VALID. Optimizing on Structure size: struct X { char a; int b; char c; Bit fields: struct X { int int b: 20; struct X { int a: 1; int b: 2;
  7. 1 1 sign 1 sign Use unsigned bit bit bit fields 1 1 data sizeof(x) sizeof(y) 1 -2 -1 -1 struct X { int a: 20; int b: 15; union X { int a char b: struct Y { int a char b: print &x, &x.a, &x b print &y, &y.a, print x. a with %x print x. b with %hhx Space for the largest field is allocated. Alignment : With respect to all the fields union Query { int id; char name [20] q. id bytes q . name // No point using name when id is filled
  8. q. name:" hello" strcpy(q. name, ' q. id No Linkage for 1) filel.c struct X{ //ERROR 'hello"); No point using id when name is filled Tags... OR typedef. . file2 . c int a; char b; filel.c struct X{ int a; char b; filel.c struct X{ int a; char b; void f (void) printf("%p\n" //ERROR struct X x; main() { } file2 . c extern struct X; // Incomplete struct, // No linkage //ERROR struct X x; main() { } file2 . c struct X { int a; char b;} ; exten struct X x; int main(void) print f ("main: %p\n" VLAs in structures? int a=3; struct X { int p[a]; char b} x; gcc does support VLAs in structures..
  9. int int i -i, struct X xp->a-- xp->b-- j=i; { int a; char b} equal 10 equal 'a' x2, *xp=&xl;