c language array

Posted by hezll on Thu, 03 Feb 2022 02:03:56 +0100

Pointer array

char *arr4[]=   {"hello","world"};    //Eindimensionales Array aus Zeigern

amount to:

const char *anonymous1="hello";
const char *anonymous2="World";
char *arr5[2] = {anonymous1,anonymous2};
printf("%c %s",arr5[1][4],arr5[0]);

Array pointer

Define int (*p)[n];
() high priority. First, it means that P is a pointer to an integer one-dimensional array. The length of this one-dimensional array is n, which can also be said to be the step size of P. That is, when p+1 is executed, P should span the length of n integer data.
To assign a two-dimensional array to a pointer, assign the value as follows:

int a[3][4];
int (*p)[4]; //This statement defines an array pointer to a one-dimensional array containing four elements.
 p=a;        //Assign the first address of the two-dimensional array to p, that is, a[0] or & a[0] [0]
 p++;       //After the statement is executed, that is, p=p+1;p crosses row a[0] [] and points to row a[1] []
	char str[2][6] = {"hello","world"};
    char (*str2)[6];
    str2 = str;
    printf("%s", *++str2); // world

Therefore, array pointers are also called pointers to one-dimensional arrays, or row pointers.

3D array

Char arr3[5][4][3];
Char *ptr1=&arr3[1][3][0];  //Zeiger auf Array arr3[1][3][0];
*ptr1            //entspricht arr3[1][3][0]
Ptr1[1];         //entspricht arr3[1][3][1];
*(ptr1‐1);       //entspricht arr3[1][2][2];
 Neuer Abschnitt 1 Seite 3 Char (*ptr2)=arr3[3][1];    //Zeiger auf Array arr3[3][1][0..2]
Ptr2[2]          //entspricht arr3[3][1][2]
*(ptr2+2)
Char (*ptr3)[3]=  arr3[2];  //Zeiger auf Array arr3[2][0..3][0..2]
Ptr3[3][1]       //entspricht arr3[2][3][1]
char ptr4[4][3]  = arr3[2];     //???
char ptr5[ ] [3] = arr3[2];     //???
char (*ptr6)[4][3] = arr3; //Zeiger auf Array arr3[0..4][0..3][0..2]
Ptr4[4][3][2]     //entspricht arr3[0][4][3][2]

Reserved space

The storage space of the array can also be reserved as heap space.

short (*ptr1)[3][2]=malloc(sizeof(short)*2*3*4711);
ptr1[4700][2][1]=11;

In order to initialize each bit of the array to 0, it is recommended to use calloc.

short  (*ptr2)[3][2]=calloc(sizeof(sort),2*3*4711);

Initialize array

Initialize the array with braces like a structure

int arr1[3] = {1, 2, 3};
int arr2[] = {1, 2, 3};//Here, we arr[3] don't need to write the numbers;
int arr3[3] = {1, 2};//It's OK, just initialize the last number to 0
long long vector1[4]={1LL,2LL,3LL,4LL};
long long vector2[]   ={7,8,9,10};

String array can be initialized with quotation marks, and the last bit defaults to '\ 0'.

char string1[5]="4711";
char arr1a[4]={1,2,3,4};    //OK
char arr1b[4]={1,2,3,4,5};  //KO compiler‐fehler
char arr1c[4]={1,2,3};      //OK, Fehlende Elemente werden auf 0 gesetzt
char arr1d[] ={1,2,3,4};    //OK, Dimension ergibt sich aus der Anzahl der Initialisierungslemente
char arr1e[100]={0};        //OK, Fehlende Elemente werden auf 0 gesetzt
char arr2a[6]="hello";      //OK
char arr2b[5]="hello";      //OK, aber die Speicher overflow
char arr2c[4]="hello";      //OK, aber die Speicher overflow
char arr2d[8]="hello";      //OK, aber compiler fuellt arr[6] und arr[7] mit '\0'
char arr2e[] ="hello" "world" "ich"   //OK, entspicht char arr2e[14] = "helloworldich"
int  arr3f[6]="hello";      //KO
char arr4a[4][3]  =  { {1,2,3},{4,5,6},{7,8,9},{10,11,12} }; //OK
char arr4b[4][3]  =  { 1,2,3,4,5,6,7,8,9,10,11,12 };         //OK,
char arr4c[3][10] =  {  "hallo","du","da" };                 //OK

In C99, the user is allowed to assign a value to a specific bit like a structure during array initialization:

char arr5[20] = {[0]='a', [8]=33,34,[3]=35,36, [10 ... 12]=3 };
char arr6[4][3]=  {[1][1]=7};

Fill the array with 0

For arrays with a constant length, each array element can be assigned a value of 0.0 by Trick = {0} However, for other arrays, memset can be used to assign 0

char arr7[5][4];
memset(arr7,0,sizeof(arr7));
float   arr8[8][9];
memset(arr8,1,sizeof(float)*8*9);          //Each array element is a floating-point number consisting of 0x01 0x01 0x01 0x01

Equality judgment of arrays

The array name is actually a pointer to the array, so directly comparing arrays actually compares addresses

char arr1[4],arr2[4];
if(arr1 == arr2)       //Für Compiler OK, jedoch erfolgt hier ein Pointervergleich

To really compare arrays, you must manually write a for loop or use the function memcmp():

For(int lauf=0;lauf<4;lauf++) if(arr1[lauf]!=arr2[lauf]) break;
if(memcmp(arr1,arr2,sizeof(arr1)) == 0)

Copy of array

Using arr2 = arr1 directly will only copy the address of arr1

int arr[10] = {[1] = 2};
    int arr2[10];
    // arr2 = arr;  //KO
    int *arr3;
    arr3 = arr; //OK

Use memcpy(char *dst, char *src, sizeof(src));

memcpy(arr2,arr1,sizeof(arr1));

Array as function parameter

When a function passes an array, there are many ways to define it:

//Method 1
#include<stdio.h>
void fun(int b[][3])
{
	b[1][2]=3;
}
void main()
{
	int a[3][3]={0};
	fun(a);
	printf("%d\t%d\n",b[1][2],b[0][0]); //3	0
}
//Method 2
#include<stdio.h>
void fun(int **a)
{
	a[1][3]=3;
	printf("%d\t%d\n",a[1][3],a[0][0]);
}
void main()
{
	int *p[3],b[3][3]={0};
    p[0]=b[0];
	p[1]=b[1];
	p[2]=b[2];
	fun(p);
}

Note:

  1. Define int *p in the main function; p[0] = 0; This is not allowed.
  2. An array variable is defined in the sub function and assigned a value to the variable. After leaving the function body, the variable and its contents will be cleared, but the string constant will be kept in the string constant area of memory and will not be clear:
char *function(void){
    // char str[11] = "helloworld";  //  Unable to return the value
    char *str = "helloworld";  // You can return the value
    return str;
}
int main(int argc, char const *argv[])
{
    char *arr;
    arr = function();
    printf("%s", arr);
}

Topics: C