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 ......