quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif Arrays

An array is defined as a set of homogeneus data items.

   

space.gif

  • Single Dimension Arrays
  • Multi Dimension Arrays
   

space.gif

  ../images/main/bulllet_4dots_orange.gif Single Dimension Arrays
   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Multi Dimension Arrays
   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Unsized Array Initializations

If unsized arrays are declared, the C compiler automatically creates an array big enough to hold all the initializers. This is called an unsized array. Example declaration/initializations are as follows:

   

space.gif


  1 char e1[] = "read error\n";
  2 char e2[] = "write error\n";
  3 
  4 int sgrs[][2] = 
  5 { 
  6   1,1,
  7   2,4,
  8   3,9
  9   4,16,
 10 };
You could download file unsized_array.c here
   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Array Initialization

Arrays may be initialized at the time of declaration. The following example initializes a ten-element integer array:

   

space.gif


 1  int i[10] = { 1,2,3,4,5,6,7,8,9,10 };
You could download file array_init1.c here
   

space.gif

Character arrays which hold strings allow a shorthand initialization, e.g.:

   

space.gif


 1 char str[9] = "I like C";
 2 // which is the same as
 3 char str[9] = { 'I',' ','l','i','k','e',' ','C','\0' };
You could download file array_init2.c here
   

space.gif

When the string constant method is used, the compiler automatically supplies the null terminator.

   

space.gif

Multi-dimensional arrays are initialized in the same way as single-dimension arrays, e.g.:

   

space.gif


 1  int sgrs[6][2] =  
 2 { 
 3 1,1, 
 4 2,4, 
 5 3,9, 
 6 4,16, 
 7 5,25,
 8 6,36
 9 };
You could download file array_init3.c here
   

space.gif

  ../images/main/bullet_green_ball.gif Strings and String Functions

A string is an array of characters terminated by the null character.

   

space.gif

  • String Declaration
  • Initializing a String
  • String Functions
   

space.gif

  ../images/main/bulllet_4dots_orange.gif String Declaration
   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Initializing a String
   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif String Functions

C supports a wide range of string manipulation functions, including:

   

space.gif

Function

Description

strcpy(s1,s2)

Copies s2 into s1.

strcat(s1,s2)

Concatenates s2 to s1.

strlen(s1)

Returns the length of s1.

strcmp(s1,s2)

Returns 0 (false) if s1 and s2 are the same. Returns less than 0 if s1&lts2 Returns greater than 0 if s1>s2

strchr(s1,ch)

Returns pointer to first occurrence ch in s1.

strstr(s1,s2)

Returns pointer to first occurrence s2 in s1.

   

space.gif

Since strcmp() returns false if the strings are equal, it is best to use the ! operator to reverse the condition if the test is for equality.

   

space.gif

   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com