Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer science C programming language Lesson 5

Similar presentations


Presentation on theme: "Computer science C programming language Lesson 5"— Presentation transcript:

1 Computer science 2009-2010 C programming language Lesson 5

2 Aims of lesson 5 Character strings Structures Linked lists

3 Character strings How to store a character string ? Statically :
A character string is an array of characters (type char) whose last element is the null character “ \0 ”. How to store a character string ? Statically : char string1[21] ; Stores a string with no more than 20 characters (because of the \0) Dynamically : char *string2; int n = 20 ; string2=malloc(n+1) ; At the address string2, a string with no more than 20 characters can be stored.

4 Character strings How to declare and to initialise a character string at the same time ? char string1[20] ; string1=« bonjour » ; FORBIDDEN char string1[20] = « bonjour »; OK 20 bytes are reserved from the address chaine1

5 Functions manipulating strings
To copy the contents of string2 at the memory place pointed by string1, use the strcpy function : #include <string.h> char string1[100] = "eso"; char string2[100] = "ose"; strcpy(string1,string2); printf("%s\n%s\n",string1,string2); chaine1=chaine2 The functions manipulating strings are in the library <string.h> strlen : length of a character string strcmp : comparison of 2 strings cf poly str(r)chr : search for a character in a string sprintf, sscanf: equivalent to printf and scanf strcat : concatenate two character strings

6 Building of a character string
int sprintf (char *destination, const char *format) Same use as the function printf except that it stores the result in the character destination Ex : char chaine[100], ext[4]=« tif »; int i=4,val=4; sprintf(chaine, « file_%d_par_%d.%s », i, val, ext); printf(« %s \n », chaine); //displays « file_4_par_5.tif »

7 Structures A structure is an example of composite type, i.e a data type containing several variables that can be of differents types. Example : definition of a complex number struct complexe { double re; /* real part */ double im; /* imaginary part */ }; struct complexe z; // Declaration of a variable of type complex z.re = 1; z.im =2; // Initialisation of the fields struct : keyword complexe : name of the structure re and im : fields of the structure The name of the new data type is “struct complexe”

8 Typedef The new data type can be defined : struct complexe {
double re; double im; }; typedef struct complexe COMPLEXE; ... void main( ) COMPLEXE z ; // Declaration of the variable z of COMPLEXE type } This definition has to be done out of the main, in a .h for example By convention, these new names are written in capital letters Another writing, more compact : typedef struct complexe {double re; double im;} COMPLEXE;

9 Structures and functions
This new type of variable can be used exactly as the others (int,double …) COMPLEXE sum(COMPLEXE z1,COMPLEXE z2) { COMPLEXE z; z.re = z1.re + z2.re; z.im = z1.im + z2.im; return z; } The elements of a structure may have different types typedef struct customer { char name[50]; /* name of the customer */ char firstname[50]; /* first name of the customer */ double amount; /* money available on the account */ } CUSTOMER;

10 Pointers and structures
We can define a pointer on a structure : COMPLEXE *z1 ; Initialisation : z1 = malloc(sizeof(COMPLEXE)); To access the different fields : z1 -> re z2 -> im

11 It is difficult to increase, to reduce and to insert data dynamically
Linked lists An array is a useful and easy-to -use tool that enables to manipulate a lot of data at the same time. But : its size is fixed the position of the data in memory is fixed It is difficult to increase, to reduce and to insert data dynamically To solve the problem : singly linked lists Data Pointer on the following node node

12 Singly linked lists Implementation with a structure :
typedef struct node { char val ; NODE* next; } NODE ; Here, the data is a character. It can be any data type, even a structure. The structure is self-referenced.

13 Management functions Creation of a linked list : create a node
top it points to NULL Implémentation : NODE* LC_Init () { NOEUD* top; // Allocation and initialisation of top top = malloc(sizeof(NODE)); top->val='0'; top->next=NULL; return top; } Returns a pointer to the first node that defines the list.

14 Management functions Insertion of a node after a given node :
To insert s e m o top current next 1. Creation of the node to insert (allocation of a pointer) 2. The node to insert points to the next node 3. The current node points to the node to insert

15 Management functions Implementation :
NODE* LC_Insert (char c, NODE* n_current) { NODE* n_added; n_added = malloc(sizeof(NODE)); n_added->val = c; n_added->next =n_current->next; n_current->next = n_added; return(n_added); } 1. Creation of the node to insert 2. The node to insert points to the next node 3. The current node points to the node to insert

16 Other management functions
1. Counts the number of nodes of a list 2. Displays the value of all the nodes located after a given node 3. Suppresses a node after a given node …


Download ppt "Computer science C programming language Lesson 5"

Similar presentations


Ads by Google