0603 - pointers and functions

Function parameters change the value of the actual parameter

Passing by value, the formal parameter does not affect the value of the actual parameter
Passing by address, the formal parameter can change the value of the actual parameter

Array name as function parameter

The array name is used as a function parameter, and the formal parameter of the function will degenerate into a pointer. The array names here not only refer to the array names of one-dimensional arrays, but also include the array names of multi-dimensional arrays. When they are used as function parameters, they degenerate into pointers and lose the dimensions of the array .

Pointer as return value of function

When a pointer is used as the return value of a function, it essentially returns an address in a memory space outside the function.

char* nextChar(char* str) {
    
    
	return str + 1;
}

int main() {
    
    
	char str[] = "123456";
	char* p = nextChar(str);
	printf("next char: %c", *p);
}

Execute the above code, the output is as follows:
insert image description here

Guess you like

Origin blog.csdn.net/jiejingguo/article/details/131017439