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

t15BStructuresFunctionsAndArrays Pps

This document provides information about using structures with functions in C programming. It discusses passing entire structures or individual structure members to functions, and how it is better to pass a pointer to the structure. Examples are provided of defining a date structure and passing a pointer to a day_of_year function. The document also covers typedef, arrays of structures, unions, bit fields, enumeration constants, and storage management functions like malloc(), free(), calloc(), and cfree().

Uploaded by

Anjali Naidu
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

t15BStructuresFunctionsAndArrays Pps

This document provides information about using structures with functions in C programming. It discusses passing entire structures or individual structure members to functions, and how it is better to pass a pointer to the structure. Examples are provided of defining a date structure and passing a pointer to a day_of_year function. The document also covers typedef, arrays of structures, unions, bit fields, enumeration constants, and storage management functions like malloc(), free(), calloc(), and cfree().

Uploaded by

Anjali Naidu
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 14

Department of Computer and Information Science,

School of Science, IUPUI

CSCI 230

Structures
Functions and Arrays
Dale Roberts, Lecturer
Computer Science, IUPUI
E-mail: [email protected]

Dale Roberts

Using Structures With Functions


Passing structures to functions
Pass entire structure or pass individual members
Both pass call by value
It is not a good idea to pass a structure to or return from function.
The better way is passing a pointer to the structure to the functions
and returning a pointer from function.

To pass structures call-by-reference


Pass its address
Pass reference to it

To pass arrays call-by-value


Create a structure with the array as a member
Pass the structure

Dale Roberts

Using Structures With Functions (cont.)


Example:
day_of_year(struct date *pd)
{
int i, day, leap;
day = pd -> day;
leap = pd->year%4 ==0 && pd->year %100 ==0 || pd->year%400 ==0;
for (i=1; i < pd -> month; i++)
day += day_tab[leap][i];
return (day);
}
The declaration struct date *pd;
says that pd is a pointer to a structure of the type date
If p is a pointer to a structure, then p-> member_of_structure refers to the
particular members, like pd -> year
p-> member_of_structure is equivalent to (*p).member_of_structure
Notice: . has higher precedence than *; *pd.year is wrong, since pd.year
is not a pointer.
Both -> and . associate from left to right. So p -> q -> member
are (p->q)->member.
Example: emp.birthday.month are (emp.birthday).month

Dale Roberts

Using Structures With Functions (cont.)


-> and . both are at the highest precedence (together with () for
function and [] for array subscripts)
Example:

++p->x;
(++p)->x;
*p->y;
*p->y++;
(*p->y)++;
*p++->y;

struct {
int *x;
int *y;
} *p;
is equivalent to ++(p->x) /* increment x, not p */
/* increment p before access x */
/* fetch whatever y points to */
/* increments y after accessing whatever y point to */
/* increments whatever y point to, just like *p->y++ */
/* increments p after accessing whatever y point to */

Dale Roberts

typedef
typedef
Creates synonyms (aliases) for previously defined data types
Use typedef to create shorter type names
Example:
typedef struct card *CardPtr;
Defines a new type name CardPtr as a synonym for type struct card *
typedef does not create a new data type while it o nly creates an alias
Example:

struct card {
const char *face;
const char *suit;
};
typedef struct card Card;
void fillDeck( Card * const, const char *[], const char *[] );
int main()
{
Card deck[ 52 ];
const char *face[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six",
Seven", "Eight", Nine", "Ten", "Jack", "Queen", "King"};
const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"};
.. ..
fillDeck( deck, face, suit );
.. ..
}
void fillDeck(Card * const wDeck, const char * wFace[], const char * wSuit[])
{
.. ..
}

Dale Roberts

Array of Structures
Example: (before)
char name[PERSON][NAMESIZE];
int tscore[PERSON]
int math[PERSON]
int english[PERSON]

struct person_data{
char name[NAMESIZE];
int tscore;
int math;
int english;
} person[PERSON];

(now)

Initialization of structure array

struct person_data{
.. .. .. ..
} person[]={
{Jane,180,89,91},
{John,190,90,100},
.. .. .. ..
}; /* similar to 2D array */

Example: using separated arrays


average (int tscore, int math,
eng, int n)
{
int i, total=0,mathtotal =
engtotal=0;
for (i=0; i<n, i++) {
total += *tscore++;
mathtotal += *math++;
engtotal += *eng++;
}

the inner brace is not necessary

Jane,180,89,91,
John,190,90,100,
.. .. .. ..

Example: using pointer to structure


int
0,

average (struct person_data


*person, int n)
{
int i, total=0,mathtotal = 0,
engtotal=0;
for (i=0; i<n, i++) {
total += person->tscore;
mathtotal += person->math;
engtotal += person->eng;
person++;
}

Dale Roberts

Unions
union

Memory that contains a variety of objects over time


Only contains one data member at a time
Members of a union share space
Conserves storage
Only the last data member defined can be accessed

union declarations
Same as struct

union Number {
int x;
float y;
};
union Number value;

Valid union operations

Assignment to union of same type: =


Taking address: &
Accessing union members: .
Accessing members using pointers: ->
Dale Roberts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

/* Fig. 10.5: fig10_05.c


An example of a union */
#include <stdio.h>
union number {
int x;
double y;
};

Define union

int main()
{
union number value;

Initialize variables

value.x = 100;
printf( "%s\n%s\n%s%d\n%s%f\n\n",
"Put a value in the integer member",
"and print both members.",
"int:
", value.x,
"double:\n", value.y );
value.y = 100.0;
printf( "%s\n%s\n%s%d\n%s%f\n",
"Put a value in the floating member",
"and print both members.",
"int:
", value.x,
"double:\n", value.y );
return 0;
}

Set variables
Print
Program Output
Put a value in the integer member
and print both members.
int:
100
double:
9255959211743313600000000000000000000000000
0000000000000000000.00000
Put a value in the floating member
and print both members.
int:
0
double:
100.000000

Dale Roberts

Bit Fields
Bit field
Member of a structure whose size (in bits) has been specified
Enable better memory utilization
Must be declared as int or unsigned
Cannot access individual bits

Declaring bit fields


Follow unsigned or int member with a colon (:) and an integer constant representing
the width of the field
Example:
struct BitCard {
unsigned face : 4;
unsigned suit : 2;
unsigned color : 1;
};

Unnamed bit field

struct Example {
unsigned a : 13;
unsigned
: 3;
unsigned b : 4;
}

Field used as padding in the structure


Nothing may be stored in the bits
Unnamed bit field with zero width aligns next bit field to a new storage unit boundary

Dale Roberts

Enumeration Constants
Enumeration
Set of integer constants represented by identifiers
Enumeration constants are like symbolic constants whose values are
automatically set
Values start at 0 and are incremented by 1
Values can be set explicitly with =
Need unique constant names
Example:
enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL,
AUG, SEP, OCT, NOV, DEC};

Creates a new type enum Months in which the identifiers are set to the
integers 1 to 12

Enumeration variables can only assume their enumeration constant


values (not the integer representations)

Dale Roberts

/* Fig. 10.18: fig10_18.c

2
3

Using an enumeration type */


#include <stdio.h>

4
5

enum months { JAN = 1, FEB, MAR, APR, MAY, JUN,

JUL, AUG, SEP, OCT, NOV, DEC };

7
8

int main()

10

enum months month;

11

const char *monthName[] = { "", "January", "February",

12

"March", "April", "May",

13

"June", "July", "August",

14

"September", "October",

15

"November", "December" };

16
17
18

for ( month = JAN; month <= DEC; month++ )


printf( "%2d%11s\n", month, monthName[ month ] );

19
20

return 0;

21 }

Dale Roberts

1
2
3
4
5
6
7
8
9
10
11
12

January
February
March
April
May
June
July
August
September
October
November
December

Storage Management
C supports 4 functions, malloc(),
calloc(),free(), and cfree() for storage
management
malloc(n):
allocate a node while its content is still garbage
n is an integer, indicating the size of memory in byte which you would like
to allocate
malloc() return a character pointer to that memory
So, you have to use cast operator (type), to change the type of the
pointer.
Example:
int *ip;
ip = (int*) malloc(sizeof(int));
struct treeNode *tp;
tp = (struct tnode *) malloc(sizeof(struct tnode));

Dale Roberts

Storage Management (cont.)


free(p):
free() will release the memory allocated by malloc().
p is the pointer containing the address returning from malloc().
Example:

int *ip;
ip = (int*) malloc(sizeof(int));
... .. ..
free(ip);
/* Question: can you free(ip) after ip++ ? */
Example:

struct treeNode *tp;


tp=(struct treeNode *)malloc(sizeof(struct treeNode ));
... .. ..
free(tp);

When there is no further memory, malloc() will return NULL pointer. It is a


good idea to check the returning value of malloc().
if ((ip=(int *)malloc(sizeof(int))) == NULL){
printf(\nMemory is FULL\n);
exit(1);
}

When you free the memory, you must be sure that you pass the original
address returning from malloc() to function free(). Otherwise, system
exception may be happened

Dale Roberts

Storage Management (cont.)


calloc(n,size):
calloc() allow you to allocate an n elements array of same data type.
Because n can be an integer variable, you can use calloc() to allocate a
dynamic size array.
n is the element number of array that you want to allocate.
size is the number of byte of each element.
Unlike malloc(), calloc() guarantees that memory contents are all zero
Example: allocate an array of 10 elements
int *ip;
ip = (int*) calloc(10, sizeof(int));
*(ip+1) refer to the 2nd element, the same as ip[1]
*(ip+i) refer to the i+1th element, the same as ip[i]
Like malloc(), calloc() will return NULL, if no further memory is available.

cfree(p):
cfree() releases the memory allocated by calloc().
Example:

cfree(ip);

Dale Roberts

You might also like