Raspberry pie C language experiment 4

Experiment 4 pointer to array, pointer to two-dimensional array, pointer array

Knowledge development:
An Array is a collection of data of the same type. Each piece of data is called an Array Element. All elements in the Array are arranged consecutively in memory, and the entire Array occupies a piece of memory.
Int arr[] = {99, 15, 100, 888, 252}; As an example, the array is distributed in memory as shown in the following figure:

When defining an array, the array name and length should be given. The array name can be regarded as a pointer, which points to the 0th element of the array. In C language, we call the address of the 0th element the first address of the array.
Taking the above array as an example, the following figure shows the direction of arr:

After introducing array pointers, we have two schemes to access array elements. One is to use subscripts, and the other is to use pointers.
1) Use subscript
That is, the array elements are accessed in the form of arr[i]. If P is a pointer to array arr, you can also use p[i] to access array elements, which is equivalent to arr[i].
2) Use pointer
That is, access array elements in the form of * (p+i). In addition, the array name itself is also a pointer. You can also use * (arr+i) to access array elements, which is equivalent to * (p+i).

pointerToArray.c use pointer traversal to output one-dimensional array two-dimensional array

(1) Program description: use the pointer to the array to realize the method of traversing and outputting unary and binary arrays.

(2) Experiment code:

#include<stdio.h>
#define length1 9
#define length2 4
#define length3 4
/*
Program design idea: use the pointer to the array to realize the method of traversing and outputting unary and binary arrays.
*/
void traverseUnary(int * ,int length );
void traverseBinary(int (*) [length3],int,int);
int main()
{
    int unaryArray[length1] = {32,43,23,68,49,30,21,20,34};
    int binaryArray[length2][length3] = {{23,12,23,42},{32,11,33,54},{23,34,56,20},{23,34,13,40}};
    int * upointer = unaryArray;
    int (*bpointer)[length3] = binaryArray;
    traverseUnary(upointer,length1);
    traverseBinary(bpointer,length2,length3);
    
}

void traverseUnary(int * upointer,int ulength)
{

}

void traverseBinary(int (*bpointer)[4],int blength1,int blength2)
{

}


searchWord.c find whether the string pointed to by each pointer is equal to the target string in the pointer array

Knowledge development:
The relevant knowledge of precompiled macros and strings is used in this experiment. This will be learned in the following experiments 7 and 8.

The string in C language is a one-dimensional array of char type.

keyword is an array of pointers (or character arrays). For example, the first element stores a pointer to the character array "hello".
char * keyword[] = {
"hello",
"raspberry",
"hope",
"you",
"enjoy",
"it"
};

SIZE(a) to find the length of array a
#define SIZE(a) (sizeof(a) / sizeof(*a))

C language strcmp() function: comparing two strings
If the return value < 0, it means str1 is less than str2.
If the return value > 0, it means that str2 is less than str1.
If the return value = 0, it means that str1 is equal to str2.

(1) Program description: use the table array to store the pointer to the character array, and use <string h> strcmp() in the table array to determine whether there is a key value in the table array. If there is a key value, the array serial number of the value will be printed. If it does not exist, it means it does not exist. The function searchWord() returns 0 if the string is found, and -1 if it is not found.

(2) Experiment code:

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define SIZE(a) (sizeof(a) / sizeof(*a))
/*
Program description: use the table array to store the pointer to the character array, and use <string h> strcmp() in the
         table Whether the key value exists in the array. If it does, the array serial number of the value will be printed out. It does not exist
         That means it doesn't exist
*/
// searchWord: search for a key in the table. If yes, it returns 0 If not, -1
int searchWord( char* table[],  int size, const char* key, int *pos);
int main()
{
    int pos = 0;
    char * key = "raspber";
    //Pointer array, an array full of pointers,
    char * keyword[] = {
        "hello",
        "raspberry",
        "hope",
        "you",
        "enjoy",
        "it"
    };
    if(searchWord(keyword,SIZE(keyword),key,&pos)==0)
    {
        printf("the %s is in keyword! And the index is %d \n",key,pos);
    }
    else
    {
        printf("sorry,no such word in keywords");
    }

}
int searchWord( char* table[],  int size, const char* key, int *pos)
{

}

Tags: C Algorithm data structure

Posted by adguru on Sat, 04 Jun 2022 00:09:13 +0530