c programming course agenda. 2 copyright © 2005, infosys technologies ltd er/corp/crs/la07/003...

51
C Programming Course Agenda

Upload: meredith-todd

Post on 17-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

C Programming

Course Agenda

Page 2: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

2Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Agenda

• Day 1– Refresher

• Arrays and Functions• Pointers• Structures

– Lists• Static and Dynamic lists• Memory Management in C• Dynamic Lists – Singly Linked Lists

– Concepts of linked lists– Creation and traversal of a single linked list

– gcc and gdb– Assignments for the day

Page 3: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

3Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Agenda…

• Day 2– Unions– Lists

• Stacks and Queues

– Command Line Arguments– File handling in C– Assignments for the day

• Day 3– Storage classes (auto, static, extern, register)– Enumerated Data– Preprocessor directives– makefile– Assignments for the day

Page 4: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

4Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Agenda…

• Day 4– More on Pointer

• Constant Pointers• Pointers and Array• Pointer Arithmetic• Function Pointers• Pointer to pointer

– Golden rules for memory management– Violation of the golden rules– Assignments for the day

• Day 5– Test (50 marks)– Project briefing (OS stream POST project briefing)– Building and executing the project

Page 5: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

5Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Agenda…

• Day 6– Submission of all assignments– Building and executing the project

• Day 7– Building and executing the project– Project Evaluation – 50 marks

Page 6: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

6Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

References

• Kernighan & Ritchie, The C programming language, Prentice Hall India.

• Yeshwant Kanetkar, Let Us C, BPB publication.

• Mullish Cooper, The Spirit of C, Jaico.

• Herbert Schildth, Teach Yourself C, Obsorne MCGraw – Hill.

• Yeshwant Kanetkar, Understanding Pointers in C, BPB publication.

Page 7: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

7Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Course Evaluation

• 50 marks for the test to be held on day 5• 50 marks for the successful completion of the project

Page 8: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

C Programming

Day 1

Page 9: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

9Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher

• Built in data types of C– char, int, float, double

• Variables– Named memory location that can hold values– Has to be declared before it can be used– Declared with the type of value that it stores (Hungarian

notation)– Variable name should clearly indicate what it’s use

• Conditional control structures– If, if-else, & nested if-else– switch-case statements

• Iterative control structures - Loops– for– while– do while

Page 10: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

10Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

• Arrays in C– Array is a collection of similar data types– The size of the array must be specified while declaring an array– In C there is no check on array boundariesExample– int iaNumbers[10];

• Declares an array of 10 integers

Page 11: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

11Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

• Functions in C– Functions are the building blocks of a C program– Every C Program has at least one function i.e. main()– First function that is called is main()

• Every function – Prototype declaration– Body definition– Declaration must be given before the first use of the function

• Function arguments can be passed by value or by reference– Pass by value, a copy of the argument is passed to the function– Pass by reference, address of the argument is passed to the

function

• Best Practices– Function name should be prefixed with fn– Name of the function should indicate what the function is doing

Page 12: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

12Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

• Pointers in C– Pointer is a data type

• stores the address of a memory location

– If p holds the address of another variable i• p is a pointer variable and p is said to point to i

Page 13: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

13Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

• To declare a pointer variable, – Syntax: data type *pointer_var_name;

• Example1. int *piCount;

piCount will store the address of an integer value2. float *pfBasic;

pfBasic contain the address of a float variable

Note:The size of the pointer variable is platform dependent.Size of Pointer on Windows: 4 Bytes

Page 14: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

14Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

• How to store the address of an integer into an integer pointer?– Syntax: int *pointer = & integer_variable;

• Example

– iCount is an integer variable – piCount is an integer pointer– & is the “address of” operator

int iCount = 8;int *piCount;

piCount = &iCount;

Page 15: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

15Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

08

(Address)

2A 3018

Integer variable

(int iCount)

A Pointer to an

Integer

(int* piCount)

(Address)

2A 3018

Pointer to Integer

int* piCount;

00

00

00

08

2A 3018

2A 3019

2A 301A

2A 301B

Inte

ge

rMemoryAddress

Contents ofMemory Location

Integer Variableint iCount;

00

00

00

08

2A 3018

2A 3019

2A 301A

2A 301B

Inte

ge

r

00

2A

30

18

01 2C70

01 2C71

01 2C72

01 2C73

PO

INT

ER

MemoryAddress

Contents ofMemory Location

...

...

...

...

Po

ints to

Integer Variableint iCount;

Pointer to Integerint * piCount;

int iCount = 8;int *piCount;

piCount = &iCount;

Page 16: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

16Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

• Accessing the value at the address stored in the pointer, use *pointervariable Example

– “*” is called the indirection operator– It fetches the value at the address stored in piCount

• An uninitialized pointer should be set to NULLExample/* Initializing pointer to NULL */int *piCount = NULL;

printf(“%d”, *piCount);

Page 17: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

17Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

• Pointers and Arrays– Array name is the address of the 0th element of the array– Array elements are allocated memory contiguously– The name of the array acts like a pointer to the first

element in the array

5040302010

09D409D009CC09C809C4

AddressElements

09C4

aiMarks

Page 18: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

18Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

• If we extract the base address of array into a pointer we can use it to access the elements of the array

NOTE: The above printf statement can be written using *(piArr + iCount) also

void main(){ int iCount; /* Loop Counter */ int iaAr[5]={1,2,3,4,5}; /* Array of 5 integers */ int *piArr=iaAr; /* Pointer to array */

for(iCount=0;iCount<5;iCount++) { /* Print the content of array using the pointer */ printf("\n%d",*piArr); piArr++; }}

Page 19: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

19Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

• Structures in C– A structure is a set of interrelated data– Represented in C with the keyword “struct”– Different primitive data types are grouped together to form a new

data type– A structure declaration ends with a semicolon

Example

– iDay, iMonth and iYear are member variables of the ‘date’ structure

– The member variables cannot be initialized within a structure

struct date { short iDay; short iMonth; short iYear;};

Page 20: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

20Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

• Once a structure is declared, it can be used just like any primitive data type

Example

• Each member variable in a structure can be accessed individually

• To access individual members of the structure, the ‘.’ operator is usedExample

/* sToday is a variable of the type struct date */struct date sToday;

sToday.iDay = 17;sToday.iMonth = 7;sToday.iYear = 2005

Page 21: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

21Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

• typedef Keyword– C allows one type of data to be renamed with a different name

using the ‘typedef’ keyword Example

/* Declare the structure _date */struct _date{ short iDay; short iMonth; short iYear;};/* Define struct _date as a new data type ‘date’ */typedef struct _date sDate;

/* Create an instance of date structure */sDate today;

Page 22: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

22Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

• Pointer to a structure

2A3080

MemoryAddress

2A3081

2A3082

2A3083

2A3084

2A3085

short iDay;

short iMonth;

short iYear;

typedef struct _date {short iDay;short iMonth;short iYear;

} date;……/* instance of date */date sToday;…/* date pointer! */date* psToday;

/* Address of sTodayis populated intopsToday */psToday = &sToday;

00

2A

30

80

2A3090

2A3091

2A3092

2A3093

sToday

psToday

………

Page 23: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

23Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Refresher…

• Accessing member variables of structure using pointersExample

date *psToday;psToday = &sToday; …psToday->iDay = 30;psToday->iMonth = 6;psToday->iYear = 2005;

Page 24: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

24Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Lists

• A “node” is the basic element that makes up a list

• A “node” can be a single primitive data type or a structure– For example:

• In a list of numbers every node is a number (int)• In a list of students every node is a structure with the details of each

student

• A “list” is a collection of nodes where all nodes are of the same data type– A list of numbers– A list of students in class– A list of books in the library– A list of conference rooms in the building

• A list can be static or dynamic

Page 25: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

25Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Static Lists

• Static list is one which does not grow but has a fixed size• To create a static list, a node has to be defined and then the

list is created

• Static lists have a cap on the maximum number of nodes• This will either cause

– Lack of nodes if there is increase above the maximum– Waste of space if there are very less number of nodes

30499

Abishek B.

asStudents[0]

30500

Donald D.

asStudents[1]

30501

Janette

asStudents[2]

30502

Karim

asStudents[3]

Page 26: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

26Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Static Lists…

/*Structure to define a node of the list*/struct studentProfile { int iEmpID; char cName[20];};

/* Structure defines list of students*/struct Batch{ int iNumStudents; struct studentProfile asStudents[100];};

Page 27: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

27Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Dynamic Lists

• Dynamic lists– Can grow when the need arises and – There is no memory space wasted

• Creating a dynamic list would involve the following steps– The structure of the node has to be defined– New node has to be created as required

• Space is allocated in memory for the new node• The data is updated to the node• The node is appended to the list

• We have already seen how to create the structure of a node

• We need to understand how to – manage memory space i.e. allocate and free memory– then create the dynamic list

Page 28: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

28Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Memory Management in C

• When using arrays in C the drawbacks are– Size of the array has to be specified while declaring– Not always possible to predict the size requirements before its

usage– Once the array is declared we cannot increase/decrease its size at

runtime

• In certain circumstances, allocation of memory should happen at runtime based on inputs from the user

• Some of the ANSI C library functions used to manage memory are:– malloc(), calloc(), realloc() and free()

• The functions are defined in the ANSI header ‘stdlib.h’ which has to be included when these functions are used

Page 29: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

29Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Memory Management in C…

• malloc()

– Syntax: void* malloc (size);

– accepts the amount of the memory to be allocated at runtime

– allocates memory on the heap

– returns starting address of the allocated memory block

– Returns NULL if memory is not available

• Address returned is a void pointer, it must be type-cast to the required type

• Example:

– char* pcName = (char*) malloc (200 * sizeof (char));

– Allocates memory for 200 characters and returns pointer to first element

Page 30: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

30Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

• calloc()

– similar to malloc(), used to allocate memory

– Syntax: void *calloc(numMembers, size);

• calloc() accepts 2 parameters

– first one specifies the no of elements (numMembers)

– second one specifies the size of each element (size)

• calloc() returns a void pointer

– needs to be type-cast to the appropriate pointer type

• Example:

– calloc(10,4)

• will allocate an array of 10 integers where each integer occupies 4 bytes

Memory Management in C…

Page 31: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

31Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

• realloc() – used to expand/shrink the memory that was earlier allocated

– Syntax: void *realloc(void *ptr, size);

• realloc() accepts 2 arguments, – first one specifies pointer to previously allocated memory block

(ptr)

– second one specifies the new size in bytes (size)

– If ptr is NULL, the call is equivalent to malloc(size);

– if size is set to 0, the call is equivalent to free(ptr);

• Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc()

• Copies the values from the old memory block to the new block

Memory Management in C…

Page 32: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

32Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Memory Management in C…

• Memory allocated must be released if no longer needed• Syntax: void free (void* ptr);• free() accepts a void pointer,

– the starting address of the memory block dynamically allocated• Freeing an invalid pointer is illegal and the results are unpredictableExample

int main (int argc, char** argv) { char *pcName; /* Pointer to charecter */

/* Space for storing string of 25 charecters */ pcName = (char *)malloc(25 * sizeof(char));

... /* Free the pointer */ free(pcName); return 0;}

Page 33: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

33Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Memory Leak: An example

Memory Management in C…

int main(int argc, char* argv[]){

Allocate(5);/* some code*/return 0;

}void Allocate(int size){

int *p;p=(int*)malloc(size*sizeof(int));

…}

The pointer is a local variable which is on the

stack.However, it points to space

allocated on the heap.

Page 34: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

34Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Dynamic Lists – Singly Linked List

CONCEPTS• A linear data structure which stores data in nodes• Every node has 2 sections

– Data (stores the data)– Pointer (stores the address of the next node)

• To traverse the list we need– the address the first node (Head)

• The last node of the list (Tail) points to null• If address to next node is NULL then it is the end of linked list

Node

Data Pointer tonext node

Node

Data Pointer tonext node

Data Pointer tonext node

Data Pointer tonext node

Data Pointer tonext node NULL

(Head) (Tail)

Page 35: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

35Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Dynamic Lists – Singly Linked List

CONCEPTS: Inserting a node• Insertion of a new node in a linked list requires setting of two

pointers

To insert a node between the second and third node1. Pointer in the new node has to be replaced2. Pointer in the second node points to new node

Node

Data Pointer tonext node Data

Pointer tonext node Data

Pointer tonext node Data

Pointer tonext node NULL

Data Pointer tonext node

21

New node to beinserted.

Page 36: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

36Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Dynamic Lists – Singly Linked List

CONCEPTS : Deleting a node• Deleting a node from a linked list requires simply changing

the pointer to point to the node next to the deleted node

To delete third node (between the second and fourth node)1. Pointer in the second node has to be removed2. Pointer in the second node points to fourth node3. Pointer of third node is removed

Node

Data Pointer tonext node

Data Pointer tonext node

Data Pointer tonext node

Data Pointer tonext node

NULL

1

2

3

Page 37: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

37Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Dynamic Lists – Singly Linked List

Discussion: Implementation of singly linked list

typedef struct _node {int iEmpID;struct _node *psNextNode;

}node;

• This structure definition clearly indicated that “psNextNode” is a pointer of the same type as the structure because it will be pointing to a structure which is of the same type

Node

iEmpID psNextNode

Page 38: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

38Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Dynamic Lists – Singly Linked List

int main (int argc, char** argv) {

/* Create a new node for the linked list*/psLinkedList=(node*)malloc(sizeof(node));

/* Assign the Head of the linked list*/psHead = psLinkedList;

/* User adds data for first node */printf("Please enter the first ID in the list: ");scanf("%d",&psLinkedList->iEmpID);

/* Assign next node to NULL */psLinkedList->psNextNode = NULL;

iEmpID psNextNode

psLinkedList

iEmpID psNextNode

psLinkedList

psHead

iEmpID NULL

psLinkedList

psHead

Page 39: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

39Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Dynamic Lists – Singly Linked List

printf ("\nDo you want to add more elements? (y/n): ");scanf ("%c", &cOption);fflush (stdin);

/* Traversing the list and adding new nodes to the list */while (cOption == 'y' || cOption == 'Y') {

/*Allocate memory for new node*/psNewNode = (node *) malloc(sizeof(node));

/* Get input from user to add the data for new node*/printf("Please enter the new ID for the list: ");scanf("%d",&psNewNode->iEmpID);

iEmpID NULL

psLinkedList

psHead

iEmpID psNextNodepsNewNode

Page 40: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

40Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Dynamic Lists – Singly Linked List

/* Traverse the list */ psLinkedList->psNextNode = psNewNode; psNewNode->psNextNode = NULL; psLinkedList = psNewNode;

/* Choice to grow linked list */ printf ("\nDo you want to add more elements? (y/n): "); scanf ("%c", &cOption); fflush (stdin);

/* Increment the number of nodes */ iCount++;} /* End of While Loop */

psLinkedList

iEmpID NULLpsHead

psNewNode

iEmpID psNextNode

psNewNode

iEmpID NULL

psLinkedList

Page 41: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

41Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Dynamic Lists – Singly Linked List

/* Start with the first node */psLinkedList = psHead;

for(iLoop=0; iLoop<iCount; iLoop++){

/* Iterate till the last node */printf ("%d\n", psLinkedList->iEmpID);psLinkedList = psLinkedList->psNextNode;

}

/* Free the linked list*/free(psLinkedList);

/* Return a success code to the operating system */return 0;

} /* End of the main() function */

Page 42: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

42Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

C compiler : gcc

• GNU Compiler Collection (earlier it was GNU C Compiler)– Collection of compliers for different programming languages– gcc for C, g++ for C++, gnat for ADA etc.

• gcc myprogram.c– Compile the program myprogram.c – Generate an executable file– Default executable file is a.out

• Steps in gcc processing– Pre-processing– Compiling and assembly (if required)– Linking

Page 43: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

43Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

C compiler : gcc

• gcc is used with many options – Some options control the preprocessor – Some control the compiler, assembler and linker– Most are rarely used.

• Some commonly used options:–c– Says not to run the linker– Output consists of object files– Example: gcc –c myprogram.c

• Will generate the object file myprogram.o

–o FILE– This option places the output in a file FILE– Example: gcc –o myprogram.exe myprogram.c

• Generate executable file myprogram.exe instead of a.out

Page 44: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

44Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

C compiler : gcc

• Compiling and executing multiple files– Example: gcc main.c add.c sub.c product.c– main.c, add.c, sub.c and product.c are part of the same program– gcc compiles the three files – Generate the default executable file a.out

• What will the following command do?– gcc –o final.exe main.c add.c sub.c product.c

Page 45: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

45Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

The Debugger: gdb

• GNU symbolic debugger• Helps the programmer to debug the program• gcc -g try1.c add1.c sub1.c -o final1

– -g option prepares the output for debugging the three C files– -o option is for having output in specific file “final1”

Executing the program and debugging the program• Execute the above output with the command final1• To debug the program

– run final1 in debug mode

Page 46: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

46Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

The Debugger: gdb

Debug Mode• Like in any compiler debugging can be in

– step by step– break point

• To run the output file in debug modegdb final1

• gdb prompt will be displayed• To initiate the execution in debug mode the run command has

to be used– run [command line parameters]

• Example: gdb> run “hello” “hi”

Page 47: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

47Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

The Debugger: gdb

Breakpoints• break try1.c:4

– breaks at line 4• break add

– breaks at function add()• break main

– breaks at function main()

Clear breaks• clear main

– will clear the break point at function main()• Disable or enable option is also available

To continue• c

– will continue the execution from the breakpoint.

Page 48: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

48Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

The Debugger: gdb

Stepwise execution• n

– will execute the next instruction– A function call is taken as one instruction so pressing n will not

debug inside the function, it will step over

• s– Similar to n but gets steps into the function being executed

watch• watch a• watch b• All the variables in the current context will be displayed

Page 49: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

49Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

The Debugger: gdb

List• Helps in listing the contents of the file• Useful in case of adding breakpoints• list: will list the file ten lines at a time• list main

– main is a function name

• list try1.c:1,20 will list 20 lines from line 1• list add1.c:add will list the function from file add1.c

Print• Online watching a variable can be done using print variable

name

Page 50: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

50Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

The Debugger: gdb

Frames• Whenever a function is called a stack is created

– These are called as activation records• If we step into a called function

– then we will not be able to watch or – print the variables in the calling function

• The way to handle this situation is through frames– frame 0 is the frame corresponding to the presently running function

(called function)– Frame 0

• print a will print the value of the variable in called function– Frame 1

• print c will print the value of the variable in calling function.

• Always frame 0 corresponds to the last called function in the nested sequence

Page 51: C Programming Course Agenda. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Agenda Day 1 –Refresher Arrays and Functions

51Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0

Thank You!