Arrays in C language learn how to initialize arrays

  • 2020-05-07 20:05:58
  • OfStack

Arrays in all languages, C is the simplest, with 1 starting address, plus 1 array length, and basically no function at all. However, in all array usage, it is the simple array form of C that amazes with its flexibility and efficiency.

C array logically, the consistency is fractal (think of other words to describe the), the meaning of fractal, is the structure of the part and the overall keep 1 to form, which is an array of any 1 part are also array, such as 1 integer array {1, 2, 3, 4, 5}, straight out of which 1 continuous part, can be seen as an array, {2, 3} is an array, {1, 2, 3} is an array, {4, 5} is an array, the difference is only the starting address of the array and the number of elements. Even if it's any element in an array, you can view it as an array of length 1. So this one format of the C array is handy when you pass it as an argument to a function, especially a recursive function.

Some basics

Let's start with the basics.

An array can be initialized with 1 column value, for example:


int arr[] = {1,2,3,4};
char arr[] = {'a','b','c',0};

Simple 1 integer array initialization example:


#include "stdio.h"

int main()
{
  /*
  author: www.nowamagic.net
  */
  int i, length;
  int arr[] = {1,2,3,4};

  length = sizeof(arr) / sizeof(int);
  printf("sizeof(arr) To: %d\n", sizeof(arr));
  printf("sizeof(int) To: %d\n", sizeof(int));
  printf(" The array length is: %d\n", length);
  for(i = 0; i < length; i++)
  {
    printf("%d\n", arr[i]);
  }

  return 0;
}

Program running results:


sizeof(arr) To: 16
sizeof(int) To: 4
 The array length is: 4
1
2
3
4

Simple 1 char array initialization example:


#include "stdio.h"

int main()
{
  /*
  author: www.nowamagic.net
  */
  int i, length;
  char arr[] = {'a','b','c',0};

  length = sizeof(arr) / sizeof(char);
  printf("sizeof(arr) To: %d\n", sizeof(arr));
  printf("sizeof(char) To: %d\n", sizeof(char));
  printf(" The array length is: %d\n", length);
  for(i = 0; i < length; i++)
  {
    printf("%c\n", arr[i]);
  }

  return 0;
}

Program running results:


sizeof(arr) To: 4
sizeof(char) To: 1
 The array length is: 4
a
b
c

Suppose you initialize a 12-dimensional array, and you initialize every element of the array to 0

There are two ways:

1) assign the value of the elements of the array to 0 one by one using a loop;

2) memory operation function memset is used to set the memory content occupied by the array to 0;

The test code is as follows:


#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <string.h>

#define     K    15
#define     DIM   10
#define     LOOP  1000000

int main(int argc, char** argv)
{
    double     o_centers[K*DIM];
    int       i = 0, j = 0, k = 0;


    MPI_Init(&argc, &argv);

    printf("Start to test array assign...\n");
    double     starttime1 = MPI_Wtime();
    for(k = 0; k < LOOP; k++)
        for(i = 0; i < K; i++)
            for(j = 0; j < DIM; j++)
                o_centers[j + i*DIM] = 0;
    double     endtime1 = MPI_Wtime();
    printf("Array assign takes %5.12f seconds...\n", endtime1 - starttime1);


    printf("Start to test memset assign...\n");
    double     starttime2 = MPI_Wtime();
    for(k = 0; k < LOOP; k++)
        memset(o_centers, 0, K*DIM*sizeof(double));
    double     endtime2 = MPI_Wtime();
    printf("Memset assign takes %5.12f seconds...\n", endtime2 - starttime2);

    MPI_Finalize();
    return 0;
}

After compiling and running, the results are as follows:


Start to test array assign...
Array assign takes 0.624787092209 seconds...
Start to test memset assign...
Memset assign takes 0.052299976349 seconds...

notes

If the size of an array is not specified when it is defined, and the initialization takes the form of list initialization, the size of the array is determined by the number of list elements at the time of initialization. So the arrays in the above example are of type int[4] and char[4], respectively. If the array size is specified explicitly, an error occurs when the number of elements specified exceeds this size during initialization.

If the number of elements specified is smaller than the array size, the remaining elements are initialized to 0. Such as:


int arr[8]={1,2,3,4};

Is equivalent to


int arr[8]={1,2,3,4,0,0,0,0};

An array of characters can be easily initialized directly as a string.

C string, is also very simple, it is also an array, but the last one element is a '\ nul, so after 1 point limit, string naturally lose the fractal array, but C string still do not look down upon, because of the string, just bring the' \ nul ', can be regarded as a string, like, "hello" this string, as long as change the starting address, they can easily get ello ", "" llo", "lo", "o" this several sliver string, This feature simplifies many string operations and is most efficient. In addition, the C string, if you want, can be used as an array of characters, thus restoring the multicomponent function, C library and WINDOWS API, there are many functions that deal with C character arrays.

A lot of the C stuff, that's it, is amazingly powerful because of its simplicity. Function, structure, array, GOTO, enumerate these simple things, cleverly used together, can play a lot of unexpected magical functions out, people hit the joint admiration, and not like C++, there is what is called mental burden. In addition, C in the macro, hey hey, I did not mention, the variety of the end, unpredictable ghost. As for C, I am more and more in awe of it. It is far from as simple as it seems on the surface. Even if you can see every line of C code and know in your heart its corresponding assembly code, so what, it always has a way to surprise you.

C CodeBase plan: for programming problems, eliminate 1 point, comfortable 1 point; A lot of elimination, much more comfortable; Completely eliminated, completely comfortable.


Related articles: