As long as the pointer is initialized, it can be assigned directly

We have analyzed in this article :

Uninitialized pointers cannot be used.

Pointer initialization: Pointer is an address. Pointer initialization is to point the pointer to a section of memory space. We manipulate the data in this section of memory through the pointer.

 1. Correct writing:

char ch[6];
char *p=”hello”;
strcpy(ch,p);

 2. Wrong writing:

char *ch;
strcpy(ch,p);//这样会报错!!!!  

At this time ch does not point to the memory space, so an error will occur! ! ! !

For example:

char *p = new char[10];
void func(char *pch)
{
    memcpy(pch,"hello");
    return;
}

//函数调用
func(p);
printf("%s",p);

to sum up:

1) The original value can be changed by pointer operation! ! !

2) The pointer must be initialized before operation, and memory space must be assigned to the pointer variable! ! ! ! !

 

Guess you like

Origin blog.csdn.net/modi000/article/details/113929144