Understanding of c arrays and pointers

Source: Internet
Author: User

1#include <stdio.h>2 3 intMainvoid)4 {5     inta[]={1,2,3,4,5};6     int*p = (int*) (&a +1);7printf"%d,%d\n", *a+1, * (P-1));8 9//int(*PTR1) [5] = &A; √Ten//int(*PTR2) [5] =A; X One//int(*PTR1) [3] = &A; X A//int(*PTR2) [3] =A; X - } -  the //2,5

The array name can only be used as the right value!

When we define an array, the compiler determines the allocated memory size based on the number and type of elements specified. and assign the name of the address to an array name.

A[0], a[1] ... An array element, but not an element name!

Pointers, 32 systems always have a value of 4 bytes (0x11111111) to hold only one address cell, so the first address is always stored.

However, you need to move the pointer according to the type size when you visit.

Array Name: The value is equivalent to the "first address" of the array "first element" (pointer to the first element of the array)

P + 1

Char* moves a byte, int* moves 4 bytes! Array pointer moves an array element type length! The second level pointer moves a pointer length (4)!

&a: Represents an array pointer (pointer to array variable a)

Access to an array, always converted to access to pointers!

Two-dimensional arrays
1 int b[2[2] = {{1,2},{3,4}}; 2 // int *p2 = b;         X3//  Int (*P5) [2] = b;    √4//  int **p      = b;    X

Two-dimensional array name, pointer to the first element b[0], array pointer "one-level pointer"!

1 // int *p3 = b[0];             √2//  Int (*P4) [2][2] = &b;    √

Access to all elements of a two-dimensional array (first-level pointers):

1#include <stdio.h>2 intMain ()3 {4     intiarray[2][3] =        {{1,2,3},{4,5,6}};5     int*parray =NULL;6 7Parray = (int*) IArray;8   9 Tenprintf"array[0][0] =%d\n", *Parray); Oneprintf"array[1][2] =%d\n", * (Parray +5)); Aprintf"array[1][2] =%d\n", * (Parray +1*3+2));/*The array itself is continuously arranged in the address space.*/ -printf"array[1][2] =%d\n", *((int*)(*((int(*) [3]) Parray +1)) +2)); -     return 0; the}

Access to all elements of a two-dimensional array (array pointers):

1#include <stdio.h>2 3 intMain ()4 {5     intiarray[2][3] = {{1,2,3},{4,5,6}};6     int(*parray) [3] = NULL;7    8Parray =IArray;9 Tenprintf"array[0][0] =%d\n", **Parray); Oneprintf"array[1][2] =%d\n", * (* (parray+1)+2)); A     return 0; -}

1 intMain ()2 {3         intiarray[2][3] =        {{1,2,3},{4,5,6}};4 5         int(*parray) [3] =NULL;6 7Parray =IArray;8 9printf"array[0][0] =%d\n", parray[0][0]);Tenprintf"array[1][2] =%d\n", parray[1][2]); One         return 0; A}

Second-level pointers

As a pointer to a pointer, the value pointed to must be a pointer.

1 intMain ()2 {3         intiarray[2][3] =        {{1,2,3},{4,5,6}};4         int*iparray[2] = {iarray[0], iarray[1]};5         int**parray =NULL;6 7Parray =Iparray;8      9printf"array[0][0] =%d\n", parray[0][0]);Tenprintf"array[1][2] =%d\n", parray[1][2]); One       A         return 0; -}

Understanding of c arrays and pointers

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.