Introduction to C Programming CE003121 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Introduction to C Programming CE003121

Description:

and Ragged Arrays. Allocation of Static Memory ... Ragged array example. Read a list of strings (one on each line) into an array where ... – PowerPoint PPT presentation

Number of Views:122
Avg rating:3.0/5.0
Slides: 13
Provided by: rgh1
Category:

less

Transcript and Presenter's Notes

Title: Introduction to C Programming CE003121


1
Introduction to C ProgrammingCE00312-1
  • Lecture 18
  • Dynamic Memory Allocation
  • and Ragged Arrays

2
Allocation of Static Memory
  • A declaration such as char name100021,
    allocates sufficient memory for 1000 names of 21
    characters each.
  • This storage of 21 000 characters is reserved at
    compilation time, i.e. before the program begins
    execution.
  • This wastes space as most names are rather less
    than 20 letters.
  • It also restricts names to a maximum of 20
    characters.
  • We can resolve this wastage and restriction by
    allocation of memory as and when required, during
    execution of the program.

3
Dynamic Allocation using malloc function
  • A standard function, malloc(n) allocates n bytes
    (for n characters) in memory and returns a
    pointer to it.
  • For example the following code
  • Prompts the user for the size of the string
  • Reads the size
  • Allocates memory for a string of this size
  • Reads the string

4
Example using malloc
  • int size
  • char s
  • printf(\nhow many characters?\n)
  • scanf(d, size)
  • s malloc(size1) // one extra for \0
  • printf(type string\n)
  • gets(s)
  • allocates only enough memory for the expected
    string.

5
Use of malloc for any data type
  • malloc can be used to allocate memory for any
    ltdata typegt.
  • ltdata typegt ltvariablegt // pointer to ltdata
    typegt
  • // some other code here
  • ltvariablegt (ltdata typegt )malloc(n
    sizeof(ltdata typegt))
  • allocates sufficient memory for n values of
    ltdata typegt and returns a pointer which must be
    casted as the same pointer type as the
    ltvariablegt.

6
Using malloc for an array of integers
  • For example
  • To allocate storage for 10 integers
  • int p
  • p (int ) malloc (10 sizeof(int))
  • This is machine-independent, as sizeof returns
  • size of an integer for any machine.

7
Ragged Arrays
  • Declare 6 variable length names array of string
    pointers
  • char name6
  • abdul, abraham, al , bill, fred,
    jean-pierre
  • We can declare functions on ragged arrays
  • void print_list(char table1 , int n)
  • so that it can deal with strings of any length.

8
Ragged array example
  • Read a list of strings (one on each line) into an
    array where
  • the size of each element is precisely the length
    of each
  • string, ie the strings are of variable length - a
    ragged array.
  • include "stdio.h"
  • include "string.h"
  • include "stdlib.h" // for malloc
  • // prototypes
  • void sort(char , int)
  • int read_strings(char , int)
  • void print_strings(char , int)

9
Main function
  • int main(void)
  • char list1000
  • // array of 1000 string pointers
  • int n // actual number of strings
  • n read_strings(list, 1000)
  • // read n strings, max of 1000
  • sort(list, n) // sort n strings
  • print_strings(list, n) // print n strings
  • return (0)

10
print_strings function
  • void print_strings(char list, int n)
  • int i
  • printf("\nalphabetical order is\n\n")
  • for (i0 iltn i)
  • printf("s\n", listi)

11
void sort(char a, int n) // array of n
strings as pointers int i, j char temp //
string pointer for (j n -1 j gt 0 j--) //
each pass of j comparisons for (i0 i lt j
i) // each comparison if
(strcmp(ai, ai1) gt 0) // swap
the POINTERS temp ai
ai ai1 ai1
temp // else no swap //
end of pass // end of all passes // end
sort
12
int read_strings(char list, int max) int i
0 char line81 // store for each
line printf("\ntype one string per
line\n") while (gets(line) ! NULL i lt max)
// while not end of input listi
malloc (strlen(line)1) // listi big
enough // for line 1 for \0
strcpy(listi, line) // copy line
into listi i // next element return
i // actual number of strings
Write a Comment
User Comments (0)
About PowerShow.com