t15BStructuresFunctionsAndArrays Pps
t15BStructuresFunctionsAndArrays Pps
CSCI 230
Structures
Functions and Arrays
Dale Roberts, Lecturer
Computer Science, IUPUI
E-mail: [email protected]
Dale Roberts
Dale Roberts
Dale Roberts
++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)
struct person_data{
.. .. .. ..
} person[]={
{Jane,180,89,91},
{John,190,90,100},
.. .. .. ..
}; /* similar to 2D array */
Jane,180,89,91,
John,190,90,100,
.. .. .. ..
Dale Roberts
Unions
union
union declarations
Same as struct
union Number {
int x;
float y;
};
union Number value;
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
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
struct Example {
unsigned a : 13;
unsigned
: 3;
unsigned b : 4;
}
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
Dale Roberts
2
3
4
5
7
8
int main()
10
11
12
13
14
"September", "October",
15
"November", "December" };
16
17
18
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
int *ip;
ip = (int*) malloc(sizeof(int));
... .. ..
free(ip);
/* Question: can you free(ip) after ip++ ? */
Example:
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
cfree(p):
cfree() releases the memory allocated by calloc().
Example:
cfree(ip);
Dale Roberts