0% found this document useful (0 votes)
50 views4 pages

Structures and Other Data Forms

Uploaded by

midhungbabu88
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views4 pages

Structures and Other Data Forms

Uploaded by

midhungbabu88
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Overview

Structures and „ Structures „ Files and Structures


Other Data Forms „ Declare
Initialize
„ Unions
„
„ Enumerated Types
„ Arrays
Programming for Engineers „ Typedef
„ Nested
„ Pointers to „ Function Pointers
„ Malloc and
Structures

1 2

Declaring & Defining Structures Initializing Structure


„ May be collection of dissimilar data type „ At defining
„ Example struct bookTag textbook={ “C Primer Plus”,
#define MAXTITL 40
#define MAXAUTH 40 “Stephen Prata”, 49.99};
„ From file
struct bookTag{
char title[MAXTITL];
fread(&textbook, sizeof(struct bookTag), 1,
char author[MAXAUTL]; inFile);
float value; sizeof(textbook) „ By assignment and string copy
}; is 84 textbook.value = 35.99;
struct bookTag textbook; strcpy(textbook.author,”Jim Grover”);
3 4

Arrays of Structure Nested Structures


Name Structure
„ Declaration „
#define LEN 20
struct bookTag library[100]; struct nameTag {
char first[LEN];
char last[Len];
„ Note this is array of arrays };
„ To access elements „ Gradebook Structure
#define MAXSTUDENTS 30
strcpy(library[5].title,”Steal This Book”); #define MAXEXAMS 2
#define MAXPROJECTS 15
strcpy(library[5].author,”Abbie hoffman”); #define SCORE unsigned char;
library[5].value=0.00; struct gradebookTag[MAXSTUDENTS] {
struct nameTag student;
library[5].author[6]=‘H’; SCORE exams[MAXEXAMS];
SCORE projects[MAXPROJECTS];
};
5 6

Copyright James E Grover 2008 1


Structures and Pointers Address of Arrays
„ Creating pointers „ Method 1
struct nameTag * me; strncpy(&gradebook[0].student.first[0],
„ Assigning "Mickey",LEN);
struct nameTag friends[100]; „ Method 2
me = &friend[0];
strncpy(gradebook[0].student.last,
„ Access entities "Mouse",LEN);
strcpy(me.first,”Jimmy”);

7 8

Access by Array Notation Structure Pointers and Functions


#define NUMSTD 6
#define MAXEXAMS 3
„ Structure in Argument List
#define MAXPROJECTS 13 int nameCount(const struct nameTag * list,
const char * firsty ) {
//Assign Random Grades for Exams and Projects int j,cnt;
srand(time(0)); int size = sizeof(list)/size(name);
for(s=0;s<NUMSTD;s++) for(j=0,cnt=0;j<size;j++){
{ if(strstr(list[j]->name,firsty))
for(e=0;e<MAXEXAMS;e++) cnt++;
gradebook[s].exams[e]=51+rand()%50; }
for(e=0;e<MAXPROJECTS;e++) return cnt;
gradebook[s].projects[e]=76+rand()%25; }
} 9 10

Calling Function w/ Pointer to


Structure Using Pointer In Function
//Display and calculate averages for exams and projects
SCORE averagePrj(struct gradesTag * ptr)
for(s=0;s<NUMSTD;s++)
{
{
int j,sum;
printf("\n%s, %s\t%d %d",
for(j=0,sum=0;j<MAXPROJECTS;j++)
gradebook[s].student.last,
sum+=ptr->projects[j];
gradebook[s].student.first,
return sum/MAXPROJECTS;
averageEx(&gradebook[s]),
}
averagePrj(&gradebook[s]));
}

11 12

Copyright James E Grover 2008 2


Unions Union
„ Overlay of Data Types „ Assigning Data to union
„ Union is size of largest member #define ABS(x) ((x<0)?-(x):(x))

„ Example //populate random number union


// 8-byte union //note: rand() returns number between 0 and 32,767
union tagRandom{ for(k=ABS(rand());k>0;k--)
char cRan[8]; //byte size integer rNum.sRan[0]=rand()+rand();
short sRan[4]; //two-byte size integer for(k=ABS(rand());k>0;k--)
int iRan[2]; //four-byte size integer rNum.sRan[1]=rand()+rand();
long long llRan; //eight-byte size integer for(k=ABS(rand());k>0;k--)
float fRan[2]; //four-byte floating-point rNum.sRan[2]=rand()+rand();
double dRan; //eight-byte floating-point for(k=ABS(rand());k>0;k--)
}rNum; rNum.sRan[3]=rand()+rand();

13 14

Display Results Enumerated Types


clrscr(); „ Default enumeration starts at zero
printf("\n PASS: %d",j);
„ enum resistor {black, brown, red, orange, yellow, green,
printf("\n%c %c %c %c %c %c %c %c",rNum.cRan[0],rNum.cRan[1], blue, violet, grey, white};
rNum.cRan[2],rNum.cRan[3],rNum.cRan[4],rNum.cRan[5],rNum.cRan[6],
„ enum resistor color;
rNum.cRan[7]);
printf("\n%d %d",rNum.iRan[0],rNum.iRan[1]); „ May overload enum type
printf("\n%e %e",rNum.fRan[0],rNum.fRan[1]); „ enum text_modes {LASTMODE = -1,BW40 = 0,
printf("\n%e",rNum.dRan); C40,BW80,C80,MONO=7,C4350=64,C40X14= 8,
printf("\n%lld",rNum.llRan); C40X21,C40X28,C40X43,C40X50,C40X60,
printf("\n\nPress \"Enter\" to continue ... "); C80X14,C80X21,C80X28,C80X43,C80X50,
C80X60,BW40X14,BW40X21,BW40X28,
BW40X43,BW40X50,BW40X60,BW80X14,
BW80X21,BW80X28,BW80X43,BW80X50,
BW80X60,MONO14,MONO21,MONO28,
MONO43,MONO50,MONO60,_ORIGMODE = 65};
15 16

Typedef Typedef
„ Unlike #define, typedef is limited to „ Examples
giving symbolic name to types only and „ #define BYTE unsigned char
not to values „ typedef char * STRING;
„ Typedef interpretation is performed by „ typedef struct {double x,double y} rect;
the compiler, not the preprocessor „ rect r1 = {3.0,-6.0}; rect r2; r2=r1;
„ Whithin its limits, typedef is more
flexible than #define

17 18

Copyright James E Grover 2008 3


Functions and Pointers Prototype of qsort( )
„ Function protoype „ Prototyped in stdlib.h
„ void ToUpper(char *); „ void qsort(void *base, size_t nelem, size_t
A function that returns void (nothing) and has size, int (*cmp)(const void *e1, const void
one argument, a pointer to char. *e2));
„ void (*pf)(char *); A „ Return nothing ( magic done my pointers)
A pointer to a function that returns void (nothing) „ Arg 1 is pointer to array to be sorted
and has one argument, a pointer to char. „ Arg 2 is number of elements
„ void * pf(char *); „ Arg 3 sizeof each element in bytes
A function that returns a pointer to void (pure „ Arg 4 is function that returns int with
pointer) and has one argument, a pointer to char. „ Arg a is pointer to element
„ Arg b is pointer to adjacent element
19 20

Calling qsort( ) Compare Function for qsort( )


int main(int argc, char* argv[]) { int comp(const int * a,const int * b) {
int numbers[10]={1892,45,200,-98,4087,5,-2345, if (*a==*b)
1087,88,-100000}; return 0;
int i; else if (*a < *b)
return -1;
/* Sort the array */ else
qsort(numbers,10,sizeof(int),comp) ; return 1;
for (i=0;i<9;i++) }
printf("Number = %d\n",numbers[ i ]) ;
return 0;
}
21 22

Programming Exercises

„ Chapter 14
„ Exercise – 4 (Class List Structure)
„ Exercise – 8 (Colossus Airline)
„ Exercise – 11 (Function Pointers)

24

Copyright James E Grover 2008 4

You might also like