Computer science C programming language Lesson 5

Slides:



Advertisements
Similar presentations
Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.
Advertisements

Introduction to C Programming
Pointers.
Programming Languages and Paradigms The C Programming Language.
Structures Spring 2013Programming and Data Structure1.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Characters and Strings File Processing Exercise C Programming:Part 3.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
LINKED LISTS.
Pointers and Dynamic Arrays
Computer Organization and Design Pointers, Arrays and Strings in C
Strings CSCI 112: Programming in C.
Linked List :: Basic Concepts
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Fundamentals of Characters and Strings
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 6: Data Types Lectures # 10.
Lecture 8 String 1. Concept of strings String and pointers
Programming Languages and Paradigms
UNIT-3 LINKED LIST.
Programming Paradigms
Module 2 Arrays and strings – example programs.
C Programming:Part 3 Characters and Strings File Processing Exercise.
Arrays in C.
Introduction to Data Structures
CSCE 210 Data Structures and Algorithms
Programming Languages and Paradigms
LINKED LISTS.
C Stuff CS 2308.
Lists, Strings & Multi-dimensional arrays
Engr 0012 (04-1) LecNotes
INC 161 , CPE 100 Computer Programming
Pointers and Pointer-Based Strings
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Data Structures and Programming Techniques
Linked Lists Chapter 4.
Strings What is a string? It is an array of characters terminated with
Chapter 8 Character Arrays and Strings
C Strings Prabhat Kumar Padhy
Homework Starting K&R Chapter 5 Good tutorial on pointers
Chapter 9: Pointers and String
Lecture 19: Working with strings
Programming Languages and Paradigms
C Characters and Strings
Dynamic Data Structures
Presentation transcript:

Computer science 2009-2010 C programming language Lesson 5

Aims of lesson 5 Character strings Structures Linked lists

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.

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

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

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 »

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”

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;

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;

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

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

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.

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.

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

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

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 …