`

c - pointer is also pass by value

 
阅读更多

 

c - pointer is also pass by value

 

when pointer as param of function, it is also pass by value:

* you can change the value that the pointer point to by the address(the value of pointer),

* but you can't change the original value of pointer in the function,

* if you want to change the pointer, you need to return the new address from function, and assign it to the pointer outside the function,


e.g.
    pass_by_value.c:
#include <stdio.h>
#include <string.h>

char *test(char *s) {
	s = strdup("s2");
	return s;
}

main() {
	char *s1;
	s1 = "s1";
	printf("%s\n", s1);	// s1
	
	test(s1);
	printf("%s\n", s1);	// s1, because pointer param of function is also pass by value, you can't change the original value of pointer in the function, even though you can change the value it point to by the address(the value of pointer).
	
	s1 = test(s1);
	printf("%s\n", s1);	// s2, because new address is returned from function, and assign to pointer outside the function.
}
 
分享到:
评论

相关推荐

    Bochs - The cross platform IA-32 (x86) emulator

    The default stepping value is 3. - Added ability to disable MONITOR/MWAIT support through .bochsrc CPUID option. The option is available only if compiled with --enable-monitor-mwait configure ...

    数位板压力测试

    This specification is intended to be an open standard, and as such the text and information contained herein may be freely used, copied, or distributed without compensation or licensing restrictions. ...

    acpi控制笔记本风扇转速

    list (if present) is now obtained and also checked for an ID match. Implemented additional support for the PCI _ADR execution: upsearch until a device scope is found before executing _ADR. This ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to ...

    微软内部资料-SQL性能优化5

    Each index row in node pages contains an index key (or set of keys for a composite index) and a pointer to a page at the next level for which the first key value is the same as the key value in the ...

    C Programming

    - **The Program Example**: This is a step-by-step breakdown of the code, including comments and explanations for each line. - **#include **: Explanation of how the standard input/output library is ...

    微软内部资料-SQL性能优化2

    For 32-bit processes, that address space is 4 GB, based on a 32-bit pointer. Each process’s virtual address space is split into user and system partitions based on the underlying operating system. ...

    CE中文版-启点CE过NP中文.exe

    Memory Scan Lua: Scanfiles now also get deleted if the memory scan object is freed before the scan is fully done Fill Memory: Now allows 64-bit addresses Structure Dissect: Fixed the popupmenu "change...

    BobBuilder_app

    Pages in a b+tree are usually implemented as a list or array of child pointers and so while finding and inserting a value is a O(log k) operation the process actually has to move children around in ...

Global site tag (gtag.js) - Google Analytics