Pointers (Part 2), Structures (Part 2), Unions
Pointers (Part 2), Structures (Part 2), Unions
Pointers (Part 2), Structures (Part 2), Unions
Programming Languages 1
Lecture 6
Pointers [ Part 2 ], Structures [ Part 2 ], Unions
Course & Lectures are based on their counterparts in the following courses:
o Harvard University's CS50; An introduction to the intellectual enterprises of
computer science and the art of programming, Harvard School of
Engineering and Applied Sciences.
o UNSW's CS1: Higher Computing, by Richard Buckland – The University of
New South Wales.
o MIT's 6.087 Practical Programming in C (2010), 6.096 Introduction to C++
(2011), and 6.S096 Introduction to C and C++ (2013), MIT (Massachusetts
1
Institute of Technology) OpenCourseWare.
2
• Pointers [ Part 2 ]
• Passing an Array to a Function
• Functions returning Multiple Results
• Structures [ Part 2 ]
O • Defining members (variables, arrays,
u & pointers)
t • Accessing the Members (variables,
Outline l
i
arrays, & pointers)
n • Structures of Structures
e • Structures with Functions
• Unions
• Declaring a Union
• Defining & Accessing its Members
Pointers
[ Part 2 ]
Passing an
Array to a
Function
Passing a 1D Array to a Function: a
Copy
Example 1 Address b
0
0
1
&a[0]
2
// See the complete code in fig. 6.13 p.262-263 2 4
int a[ SIZE ] = { 0, 1, 2, 3, 4 }; 3
/* Pass an array to be modified in a function
6
4 8
Array by reference */
modifyArray( a, SIZE ); // Only the name of the array is passed
for ( i = 0; i < SIZE; i++ ) { Pass array name
printf( "%3d", a[ i ] ); is
} /* end for */ “Call by reference”
#include <stdio.h>
void IncreamentBy5( int * );
int main()
{
int c[ ] = { 23, 55, 22, 3, 40, 18 };
IncreamentBy5( c );
for ( int i = 0; i < 6; i++)
printf("%d\t", c[ i ] );
return 0;
}
Structure Name
Can be omitted ..
typedef struct person {
char name[50];
int cit_no;
float salary;
} Person;
So, Person can now be used to declare variables of the type struct
18
person. ...
Person p1, p[50], *ptr;
Accessing the Members of a Structure
typedef struct {
char *face;
char *suit;
} Card;
int main()
{
..
Card aCard;
..
aCard.face = "Three";
aCard.suit = "Hearts";
printf( "%s", aCard.face ); /* displays Three */
Accessing the members
Accessing the Members of a Pointer to a
Structure
..
Card * cardPtr; /* Pointer */
..
(*cardPtr).face = "Three";
(*cardPtr).suit = "Hearts";
Or
cardPtr->face = "Three";
cardPtr->suit = "Hearts";
..
Card deck[52];
..
deck[2].face = "Three";
deck[2].suit = "Hearts";
One element of an array of structs,
.. which is a struct ..
for (int i = 0; I < 52;i++ )
printf( "%s%s", deck[ i ].face, deck[ i ].suit );
An Example
int main( ) {
status record[ 10 ]; int i;
for ( i = 0; i < 10; i++ ) {
printf("Enter student name & his (her) percentage");
record[ i ].id = i;
scanf ("%s %f ", record[ i ].name, &record[ i ].percentage);
}
for ( i = 0; i < 10; i++ ) {
printf("The Record of STUDENT: %d \n", i + 1);
printf("Id is: %d \n", record[ i ].id );
printf("Name is: %s \n", record[ i ].name );
printf("Percentage is: %f\n", record[ i ].percentage);
}
return 0;
}
Structures of Structures
int main( ) {
n1.real = 3.5;
n1.c1.imag_value = 5;
n1.c1.real_value = 0.65;
..
n2.real = 6.4;
n2.c1.imag_value = 9;
n2.c1.real_value = 4.95;
..
return 0; 23
}
Structures of Structures
struct employee {
char firstName[ 20 ];
char lastName[ 20 ];
int age;
char gender;
Can’t contain an instance of itself
double hourlySalary;
struct employee person; /* ERROR */
struct employee *ePtr; /* Pointer */
Note that:
int main( )
{
union Data data; // declaring data
printf( "Memory size of data: %d\n", sizeof( data ) );
}
Memory size of data: 20
Unions .. An Example
C 1
10 2
union Data { 20.5
p 3
int i; r 4
float f; o
char str[ 20 ]; 5
g
}; \0 6
int main( ) { .
.
union Data data; .
data.i = 10; .
data.f = 20.5; 20
data.str = "C Prog";
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
}
data.i : 1917853763
data.f : 4122360580327794860452759994368.00000
data.str : C Prog
Unions .. An Example
union Data {
int i;
float f;
char str[ 20 ]; data.i : 10
}; data.f : 20.500000
int main( ) { data.str : C Programming
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 20.5;
printf( "data.f : %f\n", data.f);
data.str = "C Prog";
printf( "data.str : %s\n", data.str);
}
Thanks! .. Questions?
37