A simple analysis of the difference between pointer array and array pointer in C language _c language

Source: Internet
Author: User
Tags array definition

First, take a look at a small example of an array of pointers:

#include <stdio.h> 
#include <string.h> 
 
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 (strcmp (key, table[i]) = = 0) 
    { 
      ret = i; 
      break; 
    } 
  } 
  return ret; 
} 
 
#define DIM (Array) sizeof (array)/sizeof (*array) 
 
int main () 
{ 
  const char* keyword[] = {"Do 
      ", 
      ' for ', 
      ' if ', 
      ' register ', 
      ' switch ', 
      ' while ', 
      ' case ', 
      ' static ', 
  }; 
 
  printf ("%d\n", Lookup_keyword ("Static", keyword, DIM (keyword)); 
 
  return 0; 
} 

Array pointers:

#include <stdio.h> 
 
int main () 
{ 
  int i; 
  int* PI = &i; generic type 
 
  typedef int (AINT5) [5]; 
  aint5* P1; 
  int array[5]; 
  P1 = &array; Array pointer 1 
 
  int (*P2) [5] = &array;/array pointer 2 (not recommended) 
 
  Int (*P3) [4] = &array;//X array pointer 3 (not recommended) return 
 
  0;< c12/>} 

The two names are different, of course, the meaning is different. I just started to see this scare, mainly Chinese is too broad and profound, the whole of this abbreviation is too professional, the people are around dizzy. It is easier to understand from the English explanation or the Chinese full name.

Array of pointers: array of pointers, which is used to store pointers, that is, array elements are pointers

Array pointer: A pointer to a array, which points to the array pointer

Also note the differences in their usage, as illustrated below.

int* a[4] Pointer array

Indicates that the elements in array A are INT-type pointers

Element means: *a[i] * (A[i]) is the same, because [] priority is higher than *

int (*a) [4] Array pointer

Indicates: pointer to array a

Element representation: (*A) [i]

Note: In practical applications, we often use this for pointer arrays:

typedef int* Pint;
Pint a[4];

This is the same as the meaning expressed by the array definition of the pointer above, except that the type transformation is taken.

The code demonstrates the following:

#include <iostream>
 
using namespace std;
 
int main ()
{
int c[4]={1,2,3,4};
int *a[4]; pointer array
int (*b) [4];//array pointer
b=&c;
Assigns an element in array C to array a for
(int i=0;i<4;i++)
{
a[i]=&c[i];
}
Output to see the result
cout<<*a[1]<<endl;//Output 2 on
cout<< (*b) [2]<<endl;//Output 3 on return
0;
}

Note: The array pointer is defined, which points to the first address of the array. Must give the pointer to specify an address, easy to make is wrong, do not give B address, directly with (*B) [i]=c[i] to the array B element assignment, when the array pointer does not know where to point, debugging may be correct, but the runtime must be a problem, Pay attention to this problem when using pointers. But why a doesn't need to give him an address, the element of A is a pointer, and in fact the For loop has given an address to the element in array A. But if you write *a[i]=c[i in the For loop, this also goes wrong. In a word, the definition of the pointer must know where the pointer, or to be tragic.

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.