当前位置:网站首页>Basic grammar of C language -- pointer (character, one-dimensional array) learning

Basic grammar of C language -- pointer (character, one-dimensional array) learning

2022-06-26 09:50:00 Tanzanian auduvi Canyon expert

Pointer understanding :

  1. Pointer is also a variable ; Only the address , No constants ;
  2. The pointer generally uses p、q、m、n To express , no need b Express ;
  3. The type of pointer is consistent with the type of variable it points to ;
  4. Pointer operator : * 、 & ( * For the pointer ;& Representative address )

int Occupy memory 2 individual b
float Occupy memory 4 individual b

Ordinary variable pointer

 Insert picture description here

int a=3,b=4,d;
int *b=&a; // *  For the pointer ;&  Representative address 
int *b=5; //  Will report a mistake , Because only the address , No constants 
printf("%d",a);
printf("%d",*b); // Want to use a It's worth it , use  *b ; b  yes  a  The pointer to ;b  yes  a  The address of ;*b  Represents the value of the variable pointed to by the pointer 
printf("%d",b); // Want to use a If you have an address , use  b , The memory address is a hexadecimal value ; Computer memory is generally in the form of 16 Base to represent .b  Indicates the address of the variable pointed to by the pointer 

int c=*b+1; // ;
printf("%d",c);

p1=&a; //  It can't be  *p1=&a , Will report a mistake 
p2=&b;
d=*p1+*p2;
p2=&a;

p=&(a+1); //  Wrong writing 

 Print out :
3
3
22316

4
//  There are two ways to define pointers 
int a, *p; //  One 
p=&a;

int a, *p=&a; //  Two 


Maximum number of pointer versions :


int main(int argc, char *argv[]) {
    
	int a,*p=&a,i,max=0,*p1=&max;
	for(i;i<10;i++){
    
		scanf("%d",p);
		if(*p>*p1){
    
			p1=p;
		}
	}
	printf(" The maximum number is :%d",*p1);
	
	
	
	return 0;
}

Array variable pointer ;

p To whose address , At this time, replace who to operate ;
 Insert picture description here
Be careful :
The array name itself is a special pointer , It's a constant , Cannot participate in operation , such as :
int a[]={1,2} 、char a[]=“Hello W”
a --> Array name itself , The default is the first element of the array

The pointer p It's a variable , such as :
*p=“Hello W”
 Insert picture description here

int *p,i;
int a={
    1,2,3,4,5};
p=a;// *p=a[0]=1
p++; // 2
p=a+4;
printf("%d", *p); // 5  It mainly depends on where the pointer points , Wherever you move, it means where you are 
p--; // 4

main() {
    
	//  Napoch 12 Sum of items   Pointer version  
	int *p,sum=2;
	int a[12]; //  It's usually 12 Elements ;10、11  It's fine too , however 9 No way. , Because there is a sentence below *p=*(p-1)+*(p-2), The pointer can move , Two elements can be extended here 
	p=a; 
	*p=1;
	printf(" The first is  %d",*p);
	p++;
	*p=1;
	printf(" The second is  %d",*p); 

	p++; //  Need to navigate to the subscript 2 Start , That is the first. 3 A start  ; perhaps for In parentheses  p=a+2
	for(;p<=(a+11);p++){
     // p<(a+11)  Or you could write it as  p<=a+11 
		*p=*(p-1)+*(p-2);
		printf("\n%d",*p); // a[i]
		sum=sum+*p;
	}
	printf("\n Before Fibonacci series 12 Xiang He =%d",sum);
 
	return 0;
}

The relationship between pointer and array :
Two dimensional arrays are generally reserved i How to deal with ;
One dimensional arrays can be used either way .
 Insert picture description here

原网站

版权声明
本文为[Tanzanian auduvi Canyon expert]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260911168481.html