One Dimensional And Two Dimensional Array In C



Write C code to dynamically allocate one, two and three dimensional arrays (using malloc) Its pretty simple to do this in the C language if you know how to use C pointers. Here are some example C code snipptes. One dimensional array. The characters of the array are stored in that 6 blocks of memory. The variable name str points at the first location of the allocated memory locations of the array. In the above image we have the one dimensional character array of size 6 and the memory locations of each element of the array. Print the elements of the one dimensional array. A one-dimensional array, or simply an array, is a complex data type that contains several consecutive elements of the same type. Read more about it on the web page: Arrays in C/C.

Back to: C Tutorials For Beginners and Professionals

One Dimensional Array in C with Examples

In this article, I am going to discuss One Dimensional Array in C with Examples. Please read our previous articles, where we discussed the basics of Array in C Langauge.

One Dimensional Array in C:
One dimensional and two dimensional array in c programming

One dimensional array is an array that has only one subscript specification that is needed to specify a particular element of an array. A one-dimensional array is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value.

Syntax: data-type arr_name[array_size];

Rules for Declaring One Dimensional Array
  • An array variable must be declared before being used in a program.
  • The declaration must have a data type(int, float, char, double, etc.), variable name, and subscript.
  • The subscript represents the size of the array. If the size is declared as 10, programmers can store 10 elements.
  • Array
  • An array index always starts from 0. For example, if an array variable is declared as s[10], then it ranges from 0 to 9.
  • Each array element stored in a separate memory location.
  • Initialization of One-Dimensional Array in C

    An array can be initialized at either following states:

    1. At compiling time (static initialization)
    2. Dynamic Initialization
    Compiling time initialization:

    The compile-time initialization means the array of the elements are initialized at the time of the program is written or array declaration.

    Syntax: data_type array_name [array_size]=(list of elements of an array);
    Example: int n[5]={0, 1, 2, 3, 4};

    Program:

    Output: 0 1 2 3 4

    Run time initialization:

    Run time initialization means the array can be initialized at runtime. That means array elements are initialized after the compilation of the program.

    Program:

    Output:
    99
    display99

    Array declaration, initialization and accessing

    Array declaration syntax: data_type arr_name [arr_size];
    Array initialization syntax: data_type arr_name [arr_size]=(value1, value2, value3,….);
    Array accessing syntax: arr_name[index];

    Example: Integer array example:

    int age [5];
    int age[5]={0, 1, 2, 3, 4};

    age[0]; /*0 is accessed*/
    age[1]; /*1 is accessed*/
    age[2]; /*2 is accessed*/

    Example: Character array example:

    char str[10];
    char str[10]={‘H’,‘a’,‘i’};
    (or)
    char str[0] = ‘H’;
    char str[1] = ‘a’;
    char str[2] = ‘i;

    str[0]; /*H is accessed*/
    str[1]; /*a is accessed*/
    str[2]; /*i is accessed*/

    Example of One Dimensional Array in C

    Output:

    Example:

    Create to create an integer array with size 5 and then takes the values from the keyboard and store in the array and display the elements.

    Output:

    Example:

    Create an integer array with size 5 and then calculate the larger element of that array using the function.

    Output:

    In parameter creation, it is not possible to create an entire array as an argument. In formal argument location if we constructed array syntax then it creates pointer variable only. In formal argument location when we having int arr[] syntax, then it creates pointer variable only and the syntax will indicate that it holds single dimensional array address.

    Copying 1d arrays in C:

    We have two arrays list1 and list2
    int list1[6] = {2, 4, 6, 8, 10, 12};
    int list2[6];

    and we want to copy the contents of list1 to list2. For general variables (e.g. int x=3, y=5) we use simple assignment statement (x=y or y=x). But for arrays the following statement is wrong.
    list2 = list1;

    We must copy between arrays element by element and the two arrays must have the same size.

    Example of Copying One Dimensional Array in C

    One Dimensional And Two Dimensional Array In C Programming

    One

    One Dimensional And Two Dimensional Array In C In C++ Example With Output

    Output:

    Points to Remember About Array in C:
    1. An array is a derived data type in C which is constructed from the fundamental data type of C language.
    2. An array is a collection of similar types of values in a single variable.
    3. In implementation when we required ‘n’ number of the variable of similar data type then we need to go for the array.
    4. When we are working with an array always memory will be created in the contiguous memory location, so randomly we can access the data.
    5. In array, all elements will share the same name with a unique identification value called index.
    6. Always array index will start from 0 and ends with size – 1.
    7. When we are working with an array compile-time memory management will occur i.e. static memory allocation.
    8. Always size of the array must be an unsigned integer value which should be greater than zero.

    In the next article, I am going to discuss the Multi-Dimensional Array in C with examples. Here, in this article, I try to explain One Dimensional Array in C. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article

    Posted on March 27, 2019 by Paul

    In this article I will show you how to pass a multi-dimensional array as a parameter to a function in C. For simplicity, we will present only the case of 2D arrays, but same considerations will apply to a general, multi-dimensional, array.

    There is also a video version of this tutorial:

    In C, a two-dimensional array is a one-dimensional array of one-dimensional arrays. For example, an array of two rows and 3 columns is technically a one-dimensional array of two elements, where each element is a one-dimensional 3 elements array. Here is an example of a 2D array of two rows and three columns:

    Please note, that in C, you need to explicitly define the second, third, fourth and so on dimensions of an array. For example, if you want to create a three-dimensional array:

    The simplest approach to pass a two-dimensional array to a function is to specify the second dimension of the array, e.g.:

    The problem with the above approach is that you will only be able to pass 2D arrays with three columns to the print_2d_array function.

    Please note, that you can write the print_2D_array with the alternative syntax:

    which says that a is a pointer to an array of 3 integers.

    A more flexible approach, is to pass the array as a pointer to the block of memory that stores the arr variable, e.g.:

    The advantage of this approach is that you can pass any 2D array shape to the print_2d_array function, not only 2x3 arrays like for the first version. Another advantage is that you can use the same function to print a dynamically allocated array. A slight disadvantage is that we lose the array shape information, inside the print_2d_array function, and we can’t access the array elements using:

    One Dimensional And Two Dimensional Array In C

    but the more cumbersome syntax:

    Here is an example of passing a dynamically allocated array to a function:

    If your compiler supports C99’s flexible array members you can conveniently group the array and dimensions information in a struct, e.g.:

    The size of the Matrix2D structure is twice the size of an integer, the size of the data array, the so called flexible array member, is zero. Please note that the flexible array member should be the last element in the structure. Another observation is that flexible array members are not allowed in ISO C++, although some C++ compilers have support for this as an extension.

    Here is an example of using the above structure:

    If your C compiler has support for variable length arrays, you can pass the second dimension of the array as a variable to the function. Please note that, at the time of this writing, Visual Studio 2019 doesn’t have support for variable length arrays. So you won’t be able to use the next two examples with the Visual Studio 2019 C compiler. Another observation is that variable length arrays are not allowed in ISO C++.

    Or, if you need to initialize the array at runtime:

    Another interesting case is the so-called dynamically allocated two-dimensional array, technically this is a dynamically allocated array of pointers to dynamically allocated arrays. Here is an example of passing a 2D dynamically allocated array to a function:

    Array

    In order to allocate memory for the above array we need to first allocate memory for the row pointers and next allocate memory for each row in a loop. Here is a possible implementation:

    If you want to learn more about C99/C11 I would recommend reading 21st Century C: C Tips from the New School by Ben Klemens:

    or the classic C Bible, The C Programming Language by B.W. Kernighan, D.M. Ritchie: