C language pointer array and sample code _c language

Source: Internet
Author: User

If all the elements in an array hold a pointer, then we call it an array of pointers. The array of pointers is defined in the general form:

DataType *arrayname[length];

[] has a higher precedence than *, which should be understood as:

DataType * (Arrayname[length]);

The parentheses indicate that the arrayname is an array that contains the length elements, and the outer brackets indicate that each element is of type datatype *.

In addition to the different data types for each element, the pointer array and the normal array are all the same in other ways, and here's a simple example:

#include <stdio.h>
int main () {
  int a =, B = 932, c = m;
  Defines an array
  of pointers int *arr[3] = {&a, &b, &c};//can also not specify length, direct writing int *parr[]
  //define a pointer
  int **parr pointer array arr;
  printf ("%d,%d,%d\n", *arr[0], *arr[1], *arr[2]);
  printf ("%d,%d,%d\n", * * (PARR+0), * * (PARR+1), * * (parr+2));
  return 0;
}

Run Result:

16, 932, 100
16, 932, 100

Arr is an array of pointers, which contains 3 elements, each of which is a pointer, and when we define ARR, we initialize it with the address of variable A, B, and C, which is similar to the normal array.

Parr is a pointer to an array of arr, specifically a pointer to the NO. 0 element of Arr, whose defined form should be interpreted as int * (*parr), and the * in parentheses means that the Parr is a pointer, and the int outside the brackets represents the type of data that the Parr points to. Arr the type of the first element is int *, so add two * to define the Parr.

In the first printf () statement, Arr[i] means to get the value of the I element, which is a pointer, and you need to add a * before to get the data it points to, which is the form of *arr[i.

In the second printf () statement, Parr+i represents the address of the first element, * (parr+i) means to get the value of the first element (the element is a pointer), * * (parr+i) to get the data that the first element points to.

An array of pointers can also be used in conjunction with a string array, see the following example:

#include <stdio.h>
int main () {
  char *str[3] = {
    "c.biancheng.net",
    "C language Chinese net",
    "C Language"
  };
  printf ("%s\n%s\n%s\n", Str[0], str[1], str[2]);
  return 0;
}

Run Result:

C.biancheng.net
C Language Chinese web
C Language

It should be noted that character array Str holds the first address of the string, not the string itself, the string itself is in other memory regions, and the character array is separate.

And only if the type of each element in the pointer array is char *, you can assign a value to the pointer array as above, and no other type.

To make it easier to understand, you can change the string array above into the following form, which are all equivalent.

#include <stdio.h>
int main () {
  char *str0 = "c.biancheng.net";
  Char *str1 = "C language Chinese net";
  Char *str2 = "C Language";
  Char *str[3] = {STR0, str1, str2};
  printf ("%s\n%s\n%s\n", Str[0], str[1], str[2]);
  return 0;
}

The above is the C language pointer array of data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.