Data Structure

Topics: C, Data types, String Pages: 47 (15907 words) Published: October 11, 2010
Module 815

Data Structures Using C

M. Campbell

© 1993 Deakin University

Module 815

Data Structures Using C

After working through this module you should be able to create and use new and complex data types within C programs.

Learning objectives
After working through this module you should be able to: 1. Manipulate character strings in C programs. 2. Declare and manipulate single and multi-dimensional arrays of the C data types. 3. Create, manipulate and manage C pointers to data elements. 4. Create and manage complex data types in C. 5. Use unions to define alternate data sets for use in C programs. 6. Allocate memory to variables dynamically. 7. Manipulate characters and bits.

Strings. Arrays. Pointers. Data definitions – Structures. Data definitions – Unions. Dynamic allocation of data Character and bit manipulation

Learning Strategy
Read the printed module and the assigned readings and complete the exercises as requested.

Completion of exercises and the CML test at the end of the module.

Page 813-1

Module 815

Data Structures Using C

References & resources
The C Programming Language. 2nd. edition Brian W. Kernighan and Dennis M. Ritchie Prentice-Hall, 1988 Turbo C/C++ Manuals. Turbo C/C++ MS DOS compiler.

Page 813-2

Module 815 Objective 1

Data Structures Using C After working through this module you should be able to manipulate character strings in C programs

A string is a group of characters, usually letters of the alphabet. In order to format your printout in such a way that it looks nice, has meaningful titles and names, and is aesthetically pleasing to you and the people using the output of your program, you need the ability to output text data. We have used strings extensively already, without actually defining them. A complete definition of a string is ‘a sequence of char type data terminated by a NULL character,’. When C is going to use a string of data in some way, either to compare it with another, output it, copy it to another string, or whatever, the functions are set up to do what they are called to do until a NULL character (which is usually a character with a zero ASCII code number) is detected. You should also recall (from Module 813: Fundamental Programming Structures in C) that the char type is really a special form of integer – one that stores the ASCII code numbers which represent characters and symbols. An array (as we shall discover shortly) is a series of homogeneous pieces of data that are all identical in type. The data type can be quite complex as we will see when we get to the section of this module discussing structures. A string is simply a special case of an array, an array of char type data. The best way to see these principles is by use of an example [CHRSTRG.C]. #include "stdio.h" void main( ) { char name[5]; /* define a string of characters name[0] = 'D'; name[1] = 'a'; name[2] = 'v'; name[3] = 'e'; name[4] = 0; /* Null character - end of text printf("The name is %s\n",name); printf("One letter is %c\n",name[2]); printf("Part of the name is %s\n",&name[1]); }



The data declaration for the string appears on line 4. We have used this declaration in previous examples but have not indicated its meaning. The data declaration defines a string called name which has, at most, 5 characters in it. Not only does it define the length of the string, but it also states, through implication, that the characters of the string will be numbered from 0 to 4. In the C language, all subscripts start at 0 and increase by 1 each step up to the maximum which in this case is 4. We have therefore named 5 char type variables: name[0], name[1], Page 813-3

Module 815

Data Structures Using C name[2], name[3], and name[4]. You must keep in mind that in C the subscripts actually go from 0 to one less than the number defined in the definition statement. This is a property of the original definition of C and...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Essay on Data Structure
  • Data Structure Exam Essay
  • Essay on Course Outline Data Structures
  • Data Structures Essay
  • Balagurusamy C Programming and Data Structures Questions Essay
  • Data Types and Components of Data Structure Essay
  • Kernel Data Structures Essay
  • Essay on Data Types

Become a StudyMode Member

Sign Up - It's Free