Array of string pointers, pointers to pointers

Source: Internet
Author: User

An array of string pointers, that is, each item in the array is a pointer to a string.

Definition: char* s[3]; that is, an array of three pointers, written in this form, can also be better understood, that is, the type of array stored is char*.

Another point: The array name generally refers to the first address, so the first element of the array address &s[0], because S[0] is a pointer, so the array name is a pointer to the pointer, char** p=s;

Then the operation of the array is as follows:

intMain () {Char* a="hello!"; Char* b="pangpang!"; Char* c="How is it ?"; Char* s[3]={a,b,c}; intlen=sizeof(s)/sizeof(Char*); Char* * p=s;  for(intI=0; i<len;++i) {cout<<p[i]<<Endl; }    return 0;}

The length of sizeof (s) is the total number of bytes in the array, 3 pointers is 12 bytes (32-bit machine), so that the length of the pointer is 4, it can be concluded that the length of the array is 12/4=3

Or so the output is also possible:

intMain () {Char* a="hello!"; Char* b="pangpang!"; Char* c="How is it ?"; Char* s[4]={a,b,c,null}; Char* * p=s;  for(p;*p!=null;++p) {cout<<*p<<Endl; }    return 0;}

Array of string pointers, pointers to 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.