Why does C use array parameters as pointers for programming? array/pointer arguments

Source: Internet
Author: User

Why does C use array parameters as pointers for programming? array/pointer arguments

#include
 
  void print_array_test(char ca[]){        printf("ca : %s\n",ca);        printf("&ca : %s\n",&ca);        printf("&(ca[0]) : %s\n",&(ca[0]));        printf("&(ca[1]) : %s\n",&(ca[1]));        printf("*(ca+0) : %c\n",*(ca+0));        printf("*(ca+1) : %c\n",*(ca+1));        printf("ca+1 : %s\n",ca+1);}void print_ptr_test(char *pa){        printf("pa : %s\n",pa);        printf("&pa : %s\n",&pa);        printf("&(pa[0]) : %s\n",&(pa[0]));        printf("&(pa[1]) : %s\n",&(pa[1]));        printf("pa+1 : %s\n",pa+1);        printf("pa[1] : %c\n",pa[1]);        printf("*(pa+1) : %c\n",*(pa+1));        printf("++pa : %s\n",++pa);}int main(){        char abc[]="hello,world!";        print_array_test(abc);        print_ptr_test(abc);        return 0;}
 

Array abc [20] = "hello, world! "

And abc [I] have the same meaning as abc + I -- the address of abc [I] is used.

Pointer pa = abc;

Pa [I] is equivalent to * (pa + I) -- the value of abc [I] is used.

In view of the above program, run:

[Root @ localhost code] #./arrayandptr
Ca: hello, world!
& Ca :???,??;
& (Ca [0]): hello, world!
& (Ca [1]): ello, world!
* (Ca + 0): h
* (Ca + 1): e
Ca + 1: ello, world!
Pa: hello, world!
& Pa :???,??;
& (Pa [0]): hello, world!
& (Pa [1]): ello, world!
Pa + 1: ello, world!
Pa [1]: e
* (Pa + 1): e
++ Pa: ello, world!
[Root @ localhost code] #



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.