Arrays (Linear Data Structure)

Arrays (Linear Data Structure)
  • The array is a data structure in C language.
  • It can store a fixed-size.
  • It can store value in sequential collection of elements.
  • It has the same data type for all elements.
    • Syntax:
Datatype Variable_name[];
    • Example:
int age[10];
  • Types of Array:
    • An array can be One-Dimensional, Two-Dimensional and Multidimensional.
  • One-Dimensional
    • Syntax:
Datatype Variable_name[];
    • Example:
char name[10];
  • Two-Dimensional
    • Syntax:
Datatype Variable_name[][];
    • Example:
char names[10][10];
  • Multidimensional
    • Syntax:
Datatype Variable_name[][][];
    • Example:
char names[10][10][10];

How to initialize arrays?
  • One-Dimensional
    • Syntax:
Datatype Variable_name[]={v1, v2, v3...};
    • Example:
char name[10]={'s','r','t','h','a','k'};
Note: If char data types then each element enclosed with single quote.
  • Two-Dimensional
    • Syntax:
Datatype Variable_name[][]={{},{},{}...};
    • Example:
char names[2][4]={{'a','b','c','d'},
      {'x','y','z'}};

How to display arrays elements?
  • One-Dimensional:
    • Syntax:
Variable_name[index];
    • Example:
char name[10]={'s','r','t','h','a','k'};
printf("%c",name[2]);

int age[3]={10,20,30};
printf("%d",age[1]);
  • Two-Dimensional:
    • Syntax:
Variable_name[row_index][column_index];
    • Example:
char names[2][4]={{'a','b','c','d'},
      {'x','y','z'}};

printf("%c",name[0][2]);
printf("%c",name[1][2]);

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form