Reflections on Array Pointers

Source: Internet
Author: User

Each time you want to use pointers instead of arrays, there is an obsessive-compulsive disorder in which it is inconvenient to use an array if the number of elements changes, but each time you replace an array with pointers, you will make a variety of errors:

First, the pointer is sure to initialize an address, there are two ways to initialize an address, the first one is to use new or malloc to allocate a memory, naturally generate an address, the second is to create an array, with the array of the first pointer to the value, so it seems that there is no meaning. So it is common to assign new or malloc.

Secondly, do not use int* i=&x; This form, this idea is wrong, because this is not an assignment, so change the address of the pointer, so that the i++ into a wild pointer. only with *i=x;

In summary, the use of pointers in place of arrays, and the principle of array storage is a dime.

int  I, *p= (int*) (malloc (int) *10)); for (i = 0; i <; i++) {* (p+i) = i;} for (i = 0; i<10; i++) printf ("a[%d]=%d\n", I, p[i]);

  

Reflections on Array 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.