Array is a kind of data structure that can store multiple data of the same type using the single variable name and index. As you get into the world of programming, array would be one of the data structure that you start using the most frequently. Why ? It is mainly for convinience.
For example, if you want to store 100 integers. Would would get angry if I ask you to create 100 different variable name (e.g, int a1,a2,a3,...,a100). Even C compiler may get angry -:). Obviously it would be much easier if you can define a single variable name and store 100 integers into the variable with using index.
- Declaring and Assigning Array
- Declaring with Initialization
- Assinging char Array with strcpy, strncpy
- Passing char Array into a function
- Returning char Array from a function
Declaring and Assigning Array
: The most common way to declare an Array is as follows
data_type array_name[number_of_elements];
The most common way to assign (store) values to the array is as follows.
array_name[number_of_elements] = value;
|
#include <stdio.h>
int main() {
int i = 0;
for(i=0; i < 5; i++){
printf("aryInt[%d] = %d\n", i, }
printf("\naryChar = "); for(i=0; i < 12; i++){
printf("%c", }
return 1; } |
Result :
|
aryInt[0] = 10 aryInt[1] = 20 aryInt[2] = 30 aryInt[3] = 40 aryInt[4] = 50
aryChar = Hello World |
Declaring with Initialization
If you know the values to be store in the array when you write the code, you can declare an array and store the value at the same time as shown in following examples.
|
#include <stdio.h>
int main() {
int i = 0;
for(i=0; i < 5; i++){
printf("aryInt[%d] = %d\n", i, aryInt[i]); }
printf("\naryChar1 = %s\n",aryChar1); printf("aryChar2 = %s\n",aryChar2);
return 1; } |
Result :
|
aryInt[0] = 10 aryInt[1] = 20 aryInt[2] = 30 aryInt[3] = 40 aryInt[4] = 50
aryChar1 = C-Tutorial aryChar2 = Hello World |
Assinging char Array with strcpy, strncpy
In many cases you would find it difficult (cumbersome) to assign the values to array just using the index. If the array is 'char array' there are a couple of handy functions to assign the values to array. It is to use strcpy or strncpy as shown in the following example.
|
#include <stdio.h> #include <string.h>
int main() {
char aryChar1[12] = ""; char aryChar2[12] = "";
printf("\naryChar1 = %s\n",aryChar1); printf("aryChar2 = %s\n",aryChar2);
return 1; } |
Result :
|
aryChar1 = Hello World aryChar2 = Hello |
Passing char Array into a function
Like other data type, you can pass an array to a function as in these examples.
|
#include <stdio.h> #include <string.h>
void printCharAry( {
int i = 0; for( i = 0; i < 255; i++) { if(cAry[i]=='\0') break; printf("cAry[%d]=%c\n",i,cAry[i]); } }
int main() {
char aryChar1[12] = "";
strcpy(aryChar1,"Hello World");
return 1; } |
Result :
|
cAry[0]=H cAry[1]=e cAry[2]=l cAry[3]=l cAry[4]=o cAry[5]= cAry[6]=W cAry[7]=o cAry[8]=r cAry[9]=l cAry[10]=d |
|
#include <stdio.h> #include <string.h>
void printCharAry( {
int i = 0; for( i = 0; i < 255; i++) { if(cAry[i]=='\0') break; printf("cAry[%d]=%c\n",i,cAry[i]); } }
int main() {
char aryChar1[12] = "";
strcpy(aryChar1,"Hello World");
printf("\n");
return 1; } |
Result :
|
cAry[0]=H // Result of printCharAry(&aryChar1[0]); cAry[1]=e cAry[2]=l cAry[3]=l cAry[4]=o cAry[5]= cAry[6]=W cAry[7]=o cAry[8]=r cAry[9]=l cAry[10]=d
cAry[0]=W // Result of printCharAry(&aryChar1[6]); cAry[1]=o cAry[2]=r cAry[3]=l cAry[4]=d |
: This example shows the way to define a pointer type argument and pass the array to the pointer argument.
|
#include <stdio.h> #include <string.h>
void printCharAry( {
int i = 0; for( i = 0; i <n; i++) { if(cAry[i]=='\0') break; printf("cAry[%d]=%c\n",i,cAry[i]); } }
int main() {
char aryChar1[12] = "";
strcpy(aryChar1,"Hello World");
printf("\nprintCharAry(aryChar1,12)------------\n"); printCharAry(
printf("\nprintCharAry(&aryChar1[0],12)------------\n"); printCharAry(
printf("\nprintCharAry(&aryChar1[6],12)------------\n"); printCharAry(
return 1; } |
Result :
|
printCharAry(aryChar1,12)------------ cAry[0]=H cAry[1]=e cAry[2]=l cAry[3]=l cAry[4]=o cAry[5]= cAry[6]=W cAry[7]=o cAry[8]=r cAry[9]=l cAry[10]=d
printCharAry(&aryChar1[0],12)------------ cAry[0]=H cAry[1]=e cAry[2]=l cAry[3]=l cAry[4]=o cAry[5]= cAry[6]=W cAry[7]=o cAry[8]=r cAry[9]=l cAry[10]=d
printCharAry(&aryChar1[6],12)------------ cAry[0]=W cAry[1]=o cAry[2]=r cAry[3]=l cAry[4]=d |
Returning char Array from a function
There are many different ways to return an array from a function as shown in following examples. Since an Array is assigned as a kind of pointer. If you think of the manipulation of pointer, you can treat the array variable like a pointer when you returning it from a function.
: Since an array is passed to a faction as a kind of pointer, you can change the contents of the passed array from the function and take the changed array as a return value. Strictly speaking this is not exact function return, but in practical sense you can regard this as a way to return.
|
#include <stdio.h> #include <string.h>
void toUpper(char cAry[255],int n) {
int i = 0; for( i = 0; i <n; i++) { if(cAry[i]=='\0') break; cAry[i] = toupper(cAry[i]); // change the content and save it to the same array } }
int main() {
char aryChar1[12] = "";
strcpy(aryChar1,"Hello World");
printf("\nprintCharAry(aryChar1,12)------------\n"); printf("%s\n",aryChar1);
toUpper(aryChar1,12);
printf("\nprintCharAry(aryChar1,12) after toUpper() ------------\n"); printf("%s\n",aryChar1);
return 1; } |
Result :
|
printCharAry(aryChar1,12)------------ Hello World
printCharAry(aryChar1,12) after toUpper() ------------ HELLO WORLD |
: In this example, an array will be passed into the function as a pointer and the return value will be saved to the same array as in example 01.
|
#include <stdio.h> #include <string.h>
void toUpper( {
int i = 0; for( i = 0; i <n; i++) { if(cAry[i]=='\0') break; cAry[i] = toupper(cAry[i]); // change the content and save it to the same array } }
int main() { char aryChar1[12] = "";
strcpy(aryChar1,"Hello World");
printf("\nprintCharAry(aryChar1,12)------------\n"); printf("%s\n",aryChar1);
toUpper(aryChar1,12);
printf("\nprintCharAry(aryChar1,12) after toUpper() ------------\n"); printf("%s\n",aryChar1);
return 1; } |
Result :
|
printCharAry(aryChar1,12)------------ Hello World
printCharAry(aryChar1,12) after toUpper() ------------ HELLO WORLD |
: In this example, the input and output(return) are specified as a separate variable.
|
#include <stdio.h> #include <string.h>
void toUpper( {
int i = 0;
for( i = 0; i <n; i++) { if(cAryIn[i]=='\0') break; cAryOut[i] = toupper(cAryIn[i]); } }
int main() {
char aryChar1[12] = ""; char aryCharUpper[12] = "";
strcpy(aryChar1,"Hello World"); toUpper(aryChar1,aryCharUpper,12);
printf("\nprint aryChar1 ------------\n"); printf("%s\n",aryChar1);
printf("\nprint aryCharUpper ------------\n"); printf("%s\n",aryCharUpper);
return 1; } |
Result :
|
pprint aryChar1 ------------ Hello World
print aryCharUpper ------------ HELLO WORLD |
: Strictly speaking all the above examples cannot be called 'returning an array' even though we can calling 'outputting an array'. I just used 'returning' and 'outputting' interchangeably.
This example would comply to the strict definition of function returning.
|
#include <stdio.h> #include <string.h> #include <stdlib.h>
{
int i = 0; char* cAryOut;
cAryOut = (char *)malloc(n);
for( i = 0; i <n; i++) { if(cAryIn[i]=='\0') break; cAryOut[i] = toupper(cAryIn[i]); }
}
int main() {
char aryChar1[12] = ""; char *aryCharUpper;
strcpy(aryChar1,"Hello World"); aryCharUpper = toUpper(aryChar1,12);
printf("\nprint aryChar1 ------------\n"); printf("%s\n",aryChar1);
printf("\nprint aryCharUpper ------------\n"); printf("%s\n",aryCharUpper);
return 1; } |
Result :
|
pprint aryChar1 ------------ Hello World
print aryCharUpper ------------ HELLO WORLD |