One-dimensional array, two-dimensional array, three-dimensional array, array and pointer, struct array, by changing the pointer type to change the way to access the Array

Source: Internet
Author: User

One-dimensional array, two-dimensional array, three-dimensional array, array and pointer, struct array, by changing the pointer type to change the way to access the Array
Zookeeper

  1. Print each element in the array and the address of each element:

    # Include

    # Include

    Voidmain (void)

    {

    Inta [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    For (int * p = a; p

    {

    Printf ("\ n % p, % d", p, * p );

    }

    Getchar ();

    }

    1. Pointer Array

      # Include

      # Include

      Voidmain (void)

      {

      // When poll the array, you can use the pointer to poll

      // Address management through pointer Array

      Char * str [5] = {"calc", "notepad", "tasklist", "pause", "mspaint "};

      For (char ** pp = str; pp

      {

      System (* pp );

      }

      Getchar ();

      }

      1. Array pointer (equivalent to a two-dimensional array)

        # Include

        # Include

        Intmain (intargc, char * argv [])

        {

        // Two-dimensional array, equivalent to a second-level pointer

        Charstr [5] [10] = {"calc", "notepad", "tasklist", "pause", "mspaint "};

        Printf ("% p", str );

        For (char (* p) [10] = str; p

        {

        // Print the address string

        Printf ("\ n % p, % s", p, p );

        System (char *) p );

        }

        Return 0;

        }

        1. Two-dimensional array

          # Include

          # Include

          Intmain (intargc, char * argv [])

          {

          Charstr [5] [10] = {"calc", "notepad", "tasklist", "pause", "mspaint "};

          // The pointer address is the same, but the type is different.

          // Str indicates the row address, & str indicates the address of the entire array, and * str indicates the address of the first character.

          Printf ("% p, % p, % p", str, & str, * str );

          System ("pause ");

          Return 0;

          }

          1. The method for opening up two-dimensional arrays has been printed:

            # Include

            # Include

            Intmain (intargc, char * argv [])

            {

            // Open up the space of two-dimensional arrays

            Int ** p;

            P = (int **) malloc (sizeof (int) * 10 );

            Inti, j;

            For (I = 0; I <10; I ++)

            {

            P [I] = (int *) malloc (sizeof (int) * 10 );

            }

            // Initialize the Array

            For (I = 0; I <10; I ++)

            {

            For (j = 0; j <10; j ++)

            {

            * (P + I) + j) = I * j;

            }

            Putchar (10 );

            }

            // Print a two-dimensional array by pointer

            For (I = 0; I <10; I ++)

            {

            For (j = 0; j <10; j ++)

            {

            Printf ("% d", * (p + I) + j ));

            }

            Putchar (10 );

            }

            System ("pause ");

            Return 0;

            }

            1. Addition and subtraction of pointers:

              # Include

              # Include

              Intmain (intargc, char * argv [])

              {

              // C-language operation rule. The addition principle is actually followed by the size of the element * Plus number.

              // What is actually subtracted from the subtraction principle is: the size of the element * The number of subtracted

              Inta [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

              Printf ("% p \ n", );

              Int * p1 = & a [0] [0];

              Int * p2 = p1 + 4;

              Printf ("% d \ n", * p2 );

              Int * p3 = p2-3;

              Printf ("\ n % d", * p3 );

              Printf ("\ n % d", p2-p3 );

              System ("pause ");

              Return 0;

              }

              1. Two-dimensional array Printing

                # Include

                # Include

                Intmain (intargc, char * argv [])

                {

                // C-language operation rule. The addition principle is actually followed by the size of the element * Plus number.

                // What is actually subtracted from the subtraction principle is: the size of the element * The number of subtracted

                Inta [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

                // Print each element in the array one by one

                For (int * p = & a [0] [0]; p <& a [0] [0] + 12; p ++)

                {

                // The following sentence implements a line feed every four elements.

                If (p-& a [0] [0]) % 4 = 0)

                {

                Printf ("\ n ");

                }

                Printf ("% 5d", * p );

                }

                Printf ("\ n ");

                // A is the row pointer of a constant. The type of a is equivalent to that of px.

                Int (* px) [4] =;

                For (inti = 0; I <3; I ++)

                {

                For (intj = 0; j <4; j ++)

                {

                // Printf ("% 5d", a [I] [j]); // print by array name

                // Printf ("% 5d", px [I] [j]); // It is implemented by pointer downloading.

                // Printf ("% 5d", * (px [I] + j); // implemented by pointer

                Printf ("% 5d", * (px + I) + j); // It is implemented by the following method:

                }

                Printf ("\ n ");

                }

                System ("pause ");

                Return 0;

                }

                1. Struct Array

                  # Include

                  # Include

                  # Include

                  Structpos {

                  Intx;

                  Inty;

                  };

                  Structpospos2 [8] = {

                  {100,200 },

                  {100, 0 },

                  {200,400 },

                  {300,600 },

                  {390,600 },

                  {190,900 },

                  {990,100 },

                  {1, 1390,600}

                  };

                  Structpospos1 [8] [2] = {

                  {200, 0 },{ 900,800 }},

                  {0, 0 },{ 800,130 0 }},

                  {{ 1500,200 },{ 600,900 }},

                  {{ 800,700 },{ 700,800 }},

                  {{ 300,100 },{ 600,700 }},

                  {{ 900,800 },{ 700,700 }},

                  {100,200 },{ 800,800 }}

                  };

                  Voidmain ()

                  {

                  HWND * win = find0000wa ("Notepad ++", "Notepad ++ ");

                  If (win = NULL)

                  {

                  Return;

                  }

                  SetWindowPos (win, NULL, 0, 0,100,300, 1 );

                  For (inti = 0; I <8; I ++)

                  {

                  // Set the window position and size

                  SetWindowPos (win, NULL, pos1 [I] [0]. x, pos1 [I] [0]. y, pos1 [I] [1]. x, pos1 [I] [1]. y, 1 );

                  Sleep (3000 );

                  }

                  System ("pause ");

                  }

                  1. As a function to change the values of parameters in a two-dimensional array, and print related knowledge points

                    # Include

                    # Include

                    /*************************************** *********************************/

                    /* The one-dimensional array does not have a copy mechanism, nor does the two-dimensional array. The array as a parameter is a transfer address */

                    /*************************************** *********************************/

                    Intsearchmax (inta [3] [4])

                    {

                    // The array passed through the array, which is 4 for the size (32-bit System)

                    Printf ("\ nsearch = % d", sizeof ());

                    Intb [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2 };

                    Printf ("\ nsearch B = % d", sizeof (B ));

                    // Start to calculate the maximum value of the array below

                    Intmax; // The maximum value of Storage

                    Max = a [0] [0];

                    For (inti = 0; I <3; I ++)

                    {

                    For (intj = 0; j <4; j ++)

                    {

                    If (a [I] [j]> max) // compare the size

                    {

                    // The largest receiving address

                    Max = a [I] [j];

                    }

                    }

                    }

                    Returnmax;

                    }

                    Intmain (intargc, char * argv [])

                    {

                    Inta [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2 };

                    Intmax = searchmax ();

                    Printf ("the maximum value of \ n two-dimensional array is % d \ n", max );

                    System ("pause ");

                    Return 0;

                    }

                    1. Returns the minimum value of an element in a two-dimensional array.

                      # Include

                      # Include

                      Intsearchmin (int (* p) [4])

                      {

                      // Assume that the first one is the smallest

                      // Int min = p [0] [0];

                      // You can also use the following method to implement

                      Intmin = * (p + 0) + 0 );

                      For (inti = 0; I <3; I ++)

                      {

                      For (intj = 0; j <4; j ++)

                      {

                      // The first method is implemented through the array downlink

                      // If (p [I] [j]

                      //{

                      // Min = p [I] [j]; // obtain the minimum number.

                      //}

                      If (* (p + I) + j)

                      {

                      Min = * (p + I) + j );

                      }

                      }

                      }

                      Returnmin;

                      }

                      Intmain (intargc, char * argv [])

                      {

                      Inta [3] [4] = {1, 2, 3, 4, 5, 16,-7, 8, 9, 10, 1, 2 };

                      Printf ("\ nmin = % d \ n", searchmin ());

                      System ("pause ");

                      Return 0;

                      }

                      1. Returns the maximum value in a two-dimensional array by pointer.

                        # Include

                        # Include

                        /*************************************** *********************************/

                        /* The two-dimensional array degrades to a pointer to an array with four elements */

                        /*************************************** *********************************/

                        Staticintsearchmax (int (* p) [4])

                        {

                        // Maximum storage value

                        Intmax = p [0] [0];

                        For (inti = 0; I <3; I ++)

                        {

                        For (intj = 0; j <4; j ++)

                        {

                        If (p [I] [j]> max)

                        {

                        Max = p [I] [j];

                        }

                        }

                        }

                        Returnmax;

                        }

                        Intmain (intargc, char * argv [])

                        {

                        Inta [3] [4] = {1, 2, 3, 4, 5, 16,-7, 8, 9, 10, 1, 2 };

                        Printf ("\ nmin = % d \ n", searchmax ());

                        System ("pause ");

                        Return 0;

                        }

                        12. Define 3D arrays and print them out through Arrays

                        # Include

                        # Include

                        VoidprintArray (inta [3] [4] [5])

                        {

                        Inti, j, k;

                        For (I = 0; I <3; I ++)

                        {

                        For (j = 0; j <4; j ++)

                        {

                        For (k = 0; k <5; k ++)

                        {

                        Printf ("% 4d", a [I] [j] [k]);

                        }

                        Printf ("\ n ");

                        }

                        Printf ("\ n ");

                        }

                        }

                        Intmain (intargc, char * argv [])

                        {

                        Inta [3] [4] [5];

                        Intnum = 0;

                        // Obtain the array size as 240

                        Printf ("% d \ n", sizeof ());

                        // Linear Initialization

                        For (int * p = & a [0] [0] [0]; p <& a [0] [0] + 60; p ++)

                        {

                        * P = num;

                        Num ++;

                        }

                        PrintArray ();

                        System ("pause ");

                        Return 0;

                        }

                        13. Define 3D arrays and print them out using pointers.

                        # Include

                        # Include

                        StaticvoidprintArray (int (* p) [4] [5])

                        {

                        Inti, j, k;

                        For (I = 0; I <3; I ++)

                        {

                        For (j = 0; j <4; j ++)

                        {

                        For (k = 0; k <5; k ++)

                        {

                        // Printf ("% 4d", p [I] [j] [k]);

                        Printf ("% 4d", * (p + I) + j) + k ));

                        }

                        Printf ("\ n ");

                        }

                        Printf ("\ n ");

                        }

                        }

                        Intmain (intargc, char * argv [])

                        {

                        Inta [3] [4] [5];

                        Intnum = 0;

                        // Linear Initialization

                        For (int * p = & a [0] [0] [0]; p <& a [0] [0] + 60; p ++)

                        {

                        * P = num;

                        Num ++;

                        }

                        PrintArray ();

                        System ("pause ");

                        Return 0;

                        }

                        14. Access the array by changing the pointer type

                        # Include

                        # Include

                        Intmain (intargc, char * argv [])

                        {

                        // Create a one-dimensional array

                        Int * p = (int *) malloc (sizeof (int) * 40 );

                        For (int * px = p, I = 0; px

                        {

                        // Assign a value

                        * Px = I;

                        // Pointer Loop

                        // Printf ("% d, % p \ n", * px, px );

                        }

                        Intb [5] [8];

                        Printf ("\ n ");

                        // The pointer type determines the access method.

                        Int (* pp) [8] = (int (*) [8]) p;

                        For (inti = 0; I <5; I ++)

                        {

                        For (intj = 0; j <8; j ++)

                        {

                        // Printf ("% 5d", pp [I] [j]); print data

                        Printf ("% 5d", * (pp + I) + j); // pp [I] [j]

                        }

                        Printf ("\ n ");

                        }

                        Printf ("\ n ");

                        Int (* ppp) [2] [5] = (int (*) [2] [5]) p;

                        For (inti = 0; I <4; I ++)

                        {

                        For (intj = 0; j <2; j ++)

                        {

                        For (intk = 0; k <5; k ++)

                        {

                        // Print the element

                        // Printf ("% 5d", ppp [I] [j] [k]);

                        Printf ("% 5d", * (ppp + I) + j) + k ));

                        }

                        Printf ("\ n ");

                        }

                        Printf ("\ n ");

                        }

                        System ("pause ");

                        Return 0;

                        }

                        Summary: The array defined on the left below is equivalent to the pointer on the right.

                        Array

                        Pointer

                        Array access

                        Pointer access

                        Int a [I]

                        Int * p

                        A [I]

                        * (P + I)

                        Int a [I] [j]

                        Int (* p) [j]

                        A [I] [j]

                        * (P + I) + j)

                        Int a [I] [j] [k]

                        Int (* p) [j] [k]

                        A [I] [j] [k]

                        * (P + I) + j) + k)

                        Int a [I] [j] [k] [l]

                        Int (* p) [I] [j] [k]

                        A [I] [j] [k] [l]

                        * (P + I) + j) + k) + l)

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.