(LECTURE 2) Advanced C Programming
(LECTURE 2) Advanced C Programming
Advanced C programming
15/07/2016
Agenda
Pointers
Arrays
User-Defined Types
Memory Allocations
Linked List
Pointers
Limitations of Reference by value
Write a C function that does the following
Argument: the radius of a circle r
Output: circumference c and area a
#include <stdio.h> Reference by value
#define PI 3.14 does not allow the
void circle(float r, float a, float c);
manipulation of
int main(void) variables external
{ to the function!!!
float r, a=0, c=0;
printf("Enter the radius of the circle:");
scanf("%f",&r);
circle(r, a, c); // arguments passed by value
printf("Area:%.1f Circumference:%.1f\n", a, c);
return(0);
}
3
The concept of Pointers
Mail boxes at the post office
1 45
2 678
4 ‘a’
5 112
6 5.83
Why do we need pointers
P var
Changing the Address of a Pointer
#include<stdio.h>
int main(void)
{
int main(void)
{
char *p1, *p2, *p3; // three pointers of type char
char c; // a char variable
p1=&c; // p1 now points to c
*p1='a'; // value of c changed to 'a'
p2=p1; // p2 is assigned the arithmetic value of p1
p3=p2; // p3 is assigned the arithmetic value of p2
p1
p2 c
a
p3
Re-visiting the circle function
#include<stdio.h>
#define PI 3.14
void circle(float r, float *p_a, float *p_c);
int main(void)
{
float r, a=0, c=0;
printf("Enter the radius of the circle:");
scanf("%f",&r);
circle(r, &a, &c); // arguments passed by value
printf("Area:%.1f Circumference:%.1f\n", a, c);
return(0);
}
Output: fa, fb … fz
Algorithm:
Scan the file letter by letter
Increase counter corresponding to a letter by one, every time this letter is read
Divide by the total number of characters to find the frequency
Arrays
A collection of two or more adjacent memory cells, called
array elements associated with a single symbolic name
Example:
int scores[30], x, pins[10];
double temp[10], limit;
char initial[20];
int scores[SIZE], i;
Algorithm:
Scan the file letter by letter
Increase counter corresponding to a letter by one, every time this letter is
read
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
User-Defined Types
Dealing with Multiple-type Objects
Built-in C data types are limited to: integers, characters,
floating point numbers
Problem: Programmers have to deal with real-word objects
Each object has multiple attributes that must be stored and
manipulated
User-Defined Types
typedef struct {
char solar[20]; // name of the solar system
double diameter; // diameter of the solar system in km
planet_t planets[9]; // array of planets
} solar_system;
Manipulating Individual components of structures
Accessing Components within the Hierarchy
typedef struct {
char solar[20]; // name of the solar system
double diameter; // diameter of the solar system in km
planet_t planets[9]; // array of planets
} solar_system;
solar_system solar1;
In the main:
planet_t planet1;
scan_planet(&planet1);
Dynamic Memory Allocation
Function of the stdlib.h library for dynamic allocation
int *nump;
char *letp; STEP 1
planet_t *planetp;
Memory is reserved in
memory area called
heap
Referencing to the newly allocated memory
Same way that we would access contents using pointers
*nump = 307;
*letp = ‘Q’;
Example for the use of malloc
#include<stdio.h>
// library for allocating memory
#include<stdlib.h>
int main(void)
{
int *temp; //pointer to an int
int x, y;
x=(*temp)*10;
y=(*temp)*2;
free(temp); // freeing memory allocated to temp
printf("%d, %d\n", x,y);
return (0);
}
Dynamic Allocation of Memory for a Structure
typedef struct {
char name[20]; // name of the planet
double diameter; // diameter of the planet in km
int moons; // number of moon
double orbit_time; // orbit around the sun in years
double rotation_time; // orbit around itself in years
} planet_t;
int main(void)
{
planet_t *pl3; // pointer to a planet
name age
inp=fopen("database.dat", "r");
while(!feof(inp))
{
temp=(node_t *)malloc(sizeof (node_t)); // creation of memory
scan_fun(temp, inp); // initialization of element of list
if (current==NULL)
headp=temp; // setting the head of the list
else
current->listp=temp; // else connecting to previous element
i++; // count number of elements added
current=temp; // updating the current element
temp->listp=NULL; // setting pointer to null.
}
Traversing the List
Adding a new Element at the End of the List
Searching the List by Name