Array pointers and pointers arrays of C (29)

Source: Internet
Author: User
Tags array definition

Let's take a look at the legendary pointer arrays and arrays of pointers today. In the C language, arrays have their own specific types. So what is the type of the array? It is determined by both the element type and the size of the array. For example, the type of int array[5] isInt[5]。

In the C language we can rename the array type by typedef , in the format:typedef type (name) [size], where the array type can be expressed as:typedef int (AINT5) [5] ; typedef float (AFLOAT10); then the array definition can be expressed as:AINT5 iarray; AFLOAT10 Farray;

An array pointer is used to point to an array whose name is the starting address of the first element of the array, but not the address of the array; The starting address of the array can be obtained by taking the address & function to the array name, and the array pointer can be defined by the array type:arraytype* Pointercan also be directly defined as:type (*pointer) [n], where pointer is the array pointer variable name, type is the element type of the point, and N is the size of the array pointed to.

Let's get down to the sample code to analyze the code below

#include  <stdio.h>typedef int (AINT5) [5];typedef float (AFLOAT10) [10];typedef char ( ACHAR9) [9];int main () {    aint5 a1;    float farray[10] ;    afloat10* pf = &farray;    achar9 carray;         char (*PC) [9] = &carray;     char (*PCW) [4] = carray;        int i = 0;         printf ("%d, %d\n",  sizeof (AINT5),  sizeof (A1));         for (i=0; i<10; i++)     {          (*PF) [i] = i;    }         for (i=0; i<10; i++)     {         printf ("%f\n",  farray[i]);    }         printf ("%p, %p, %p\n",  &carray, pc+1, pcw+1);         return 0;}

Let's analyze this code, define three types of arrays in 3-5 rows, define a pointer of type char[9] in line 14th, and point it to CArray, because the CArray type is also char[9], so there is no error in this sentence. A pointer of type CHAR[4] was defined on line 10th, but it was initialized with CArry, which would cause a problem. The error is due to different types. In line 23rd, PF is a pointer to the array farray, which is equivalent to the value in the array, so the meaning of this sentence is to assign a value to the array Farray. In line 31st, the first print is the address of the array cArray, the PC + 1 equals the pointer operation, i.e. (unsigned int) pc + sizeof (*PC) ==> (unsigned int) PC + 1 * 9; PCW + 1 ==> (unsigend int) PCW + sizeof (PCW) ==> (unsigned int) PCW + 1 * 4; Let's take a look at the compilation results.

We see that the program has only a warning in line 15th, and the last three lines are the result of what we have analyzed. As a professional program ape, we must treat each warning as an error, because the warning means that the program may not run properly and no one knows what will happen.

Let's go down and talk about the pointer array, in fact the pointer array is a normal array, and each element inside it is a pointer . Pointer array definition:type* parray[n], where type* is the type of each element in the array, parray an array name, and N is an array size.

Let's take a sample code to analyze the code below

#include  <stdio.h> #include  <string.h> #define  dim (a)   (sizeof (a)/sizeof (*a)) int  lookup_keyword (const char* key, const char* table[], const int  Size) {    int ret = -1;        int  i = 0;        for (i=0; i<size; i++)      {        if (&NBSP;STRCMP (key, table[i])  ==  0 )         {             ret = i;             break;        }    }         return ret;} Int main () {    const char* keyword[] =  {             "Do",              "for",              "If",             "register",              "Return",              "Switch",              "while",             "case",              "Static"     };         printf ("%d\n",  lookup_keyword ("Return",  keyword,  dim (keyword));     printf ("%d\n",  lookup_keyword ("main",  keyword, dim (keyword)));         return 0;} 

Our function is simply to find a string in a pointer array and return it if it is found. If not found, return-1, we look at the results of the compilation


We see that the function does implement this function. Through the learning of array pointers and pointers arrays, we summarize the following:1. The type of the array is determined by the element type and the size of the arrays; 2. The array pointer is a pointer to an array of the corresponding type, and the pointer array is an array in which each element is a pointer; 3, the array pointer follows the pointer algorithm. Pointer arrays have various characteristics of C-language arrays .


Welcome Everybody to study C language together, can add me qq:243343083.

Array pointers and pointers arrays of C (29)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.