Pointer variable review, pointer variable

Source: Internet
Author: User

Pointer variable review, pointer variable

1. the pointer variable contains four bytes. If the variable occupies more than four bytes, the code is given:

 1 #include <stdio.h> 2 int main(int argc, char *argv[]) 3 { 4     double arr[]={1,2,3,4,5}; 5     double * p=&arr[0]; 6     printf("%p\n",p); 7     p=&arr[1]; 8     printf("%p\n",p); 9     10             return 0;11 }

2. If you use an address that does not return a function to modify the pointer variable, the Code is as follows:

 1 #include <stdio.h> 2 #include <stdlib.h> 3 void p_add(double **q); 4 int main(int argc, char *argv[]) 5 { 6     double arr[]={1,2,3,4,5}; 7     double * p=&arr[0]; 8     printf("%p\n",p); 9     p_add(& p);10     printf("%p\n",p);11             return 0;12 }13 void p_add(double **q)14 {15     *q=(double *)malloc(sizeof(double *));16 }

3. The relationship between the pointer and the array. If you know the first address and length of the array, you can use the pointer to represent the array.

1 # include <stdio. h> 2 # include <stdlib. h> 3 void output (int * q, int len); 4 int main (int argc, char * argv []) 5 {6 int arr [] = {1, 2, 4, 4, 5}; 7 int len = sizeof (arr)/sizeof (arr [1]); // it must be a positive integer 8 output (arr, len); 9 return 0; 10} 11 void output (int * q, int len) 12 {13 int id; // the c language must first declare 14 for (id = 0; id <len; id ++) 15 {16 printf ("% d \ t", * (q + id); 17 // printf ("% d \ t ", q [id]); 18} 19 printf ("\ n"); 20}

In fact, pointers and arrays are not a concept. They are different. For example, the array name arr serves as the first address of the array, which is equivalent to & arr [0], but sizeof (arr) this is different from sizeof (arr [0]) and sizeof (q ).

To be continued ......

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.