Added: pass_value
This commit is contained in:
		
							
								
								
									
										40
									
								
								pass_value/main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								pass_value/main.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| #include <stdio.h> | ||||
|  | ||||
|  | ||||
| void add_one(int *x); | ||||
|  | ||||
| int main(void){ | ||||
|  | ||||
| 	int j; | ||||
| 	int *p; | ||||
|   int k; | ||||
|  | ||||
|  | ||||
| 	j = 5; | ||||
|  | ||||
| 	/* Pointer "p" is pointing to address "j" */ | ||||
|   p =&j; | ||||
|   /* Set k to the value of p  | ||||
| 	 * the '*' is the dereference operater | ||||
| 	 * this means the value that p is pointing to | ||||
| 	 * instead of the pointer itself*/ | ||||
| 	k = *p; | ||||
| 	printf("%d\n", *p); | ||||
| 	printf("This is k: %d\n", k); | ||||
|    | ||||
| 	/*Here we pass the address of j to the function */ | ||||
| 	add_one(p); | ||||
|  | ||||
| 	/*Or don't use any pointers | ||||
| 	 * add_one(&j); | ||||
| 	 * | ||||
| 	 * here we pass by address value | ||||
| 	 */ | ||||
|  | ||||
| 	printf("%d\n", *p); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| void add_one(int *x){ | ||||
| 	*x = *x+1; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user