|
|
|
|
|
|
|
|
|
|
|
|
Arrays
|
|
|
An array is defined as a set of homogeneus data items. |
|
|
|
|
|
- Single Dimension Arrays
- Multi Dimension Arrays
|
|
|
|
|
|
Single Dimension Arrays
|
|
|
|
|
|
|
|
|
Multi Dimension Arrays
|
|
|
|
|
|
|
|
|
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: |
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
Array Initialization
|
|
|
Arrays may be initialized at the time of declaration. The following example initializes a ten-element integer array: |
|
|
|
|
|
1 int i[10] = { 1,2,3,4,5,6,7,8,9,10 };
You could download file array_init1.c here
|
|
|
|
|
|
Character arrays which hold strings allow a shorthand initialization, e.g.: |
|
|
|
|
|
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
|
|
|
|
|
|
When the string constant method is used, the compiler automatically supplies the null terminator. |
|
|
|
|
|
Multi-dimensional arrays are initialized in the same way as single-dimension arrays, e.g.: |
|
|
|
|
|
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
|
|
|
|
|
|
Strings and String Functions
|
|
|
A string is an array of characters terminated by the null character. |
|
|
|
|
|
- String Declaration
- Initializing a String
- String Functions
|
|
|
|
|
|
String Declaration
|
|
|
|
|
|
|
|
|
Initializing a String
|
|
|
|
|
|
|
|
|
String Functions
|
|
|
C supports a wide range of string manipulation functions, including: |
|
|
|
|
|
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<s2 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.
|
|
|
|
|
|
|
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. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright © 1998-2014 |
Deepak Kumar Tala - All rights reserved |
Do you have any Comment? mail me at:deepak@asic-world.com
|
|