Often confusing pointers, special pointers, main function parameter passing

often confusing pointers

First group

  1. int *a[10];
    This is an array of pointers, and there are 10 integer pointer variables in array a a[0] - a[9].
  2. int (*a)[10];
    Array pointer variable, which is a pointer variable. It occupies 4 bytes and stores the address number.
    It points to an array, and if it adds 1, it points to the next array.
  3. int **p;
    This is a pointer to a pointer, which holds the address of the pointer variable.
    It is often used to hold the address of a pointer:
  • Common usage 1:
int **p;
int *q;
p = &q;
  • Common usage 2:
int **P;
int *q[10];
// q是指针数组的名字,是指针数组的首地址,是q[0]的地址。
// q[0]是个int *类型的指针。 所以q[0]指针变量的地址,是int **类型的

Second Group

  1. int *f(void);
    Note: *fnot enclosed in parentheses.
    It is a declaration of a function, and the return value of the declared function is of type int *.
  2. int (*f)(void);
    Note *fthat the parentheses are used to modify the description* , and it is a pointer variable. It is a function pointer variable, which stores the address of the function. The function it points to must have an int return value and no parameters.ff
    f

special pointer

pointer to void type ( void * )

A pointer of type char * points to data of type char;
a pointer of type int * points to data of type int;
a pointer of type float* points to data of type float;
does void * point to data of type void? No, because there is no variable of type void.
The pointer of the corresponding type can only store the data address of the corresponding type
void* general pointer, and any type of pointer can assign a value to the pointer variable of void* type. It is mainly used in the position of the parameters and return value of the function.

int *p
void *q; 
q=p; // 是可以的,不用强制类型转换

eg
function The function of void * memset(void *s,int c,size_t n);
this function is to assign all the first n bytes of the memory pointed to by s to c.
Memset can set the contents of character arrays, integer arrays, and floating-point arrays, so the first parameter must be a general pointer.
Its return value is the first address of the memory pointed to by s, which may be a different type of address. So the return value must also be a generic pointer.
Note: The pointer variable of void* type is also a pointer variable, which occupies 4 bytes under the 32-bit system

NULL

null pointer:

char *p=NULL;

We can think that p points to nowhere, or that p points to the storage unit with memory number 0.
In the four bytes of p, 0x00 00 00 00 is stored, and NULL is generally used to initialize pointers.

The main function passes parameters

int main(int argc, char *argv[])

argc: is a variable of int type, which identifies the number of parameters passed in by the command terminal;
argv: is an array of pointers, used to save the parameters passed in by each command terminal.

#include <studio.h>
int main(int argc, char *argv[])
{
    
    
	int i;
	printf("argc = %d\n", argc);
	for(i=0;i<argc;i++)
	{
    
    
		printf("argv[%d]=%s\n",i,argv[i]);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/shuting7/article/details/129913240