2022. 7. 2. 00:31ㆍCoding - C
Address Value = The starting value of the data stored in the memory. In C, that address value is expressed as 1 byte of data. EX) Data type int is 4 byte, so if the address value is 0X10, 0X11, 0X12, 0X13, it would give 0X10 for the location value since that is the starting value of the data.
Pointer = The variable that holds the memory address. It is also called as Pointer Variable.
Operators = &(Amperstand), *(Asterisk):
Amperstand(&) = used in front of the variable name, returns the address value of that variable.
Asterisk(*) = used in front of the pointer or address, returns the value that is saved on the value on that address.
Pointers are called in form of type *pointername = &variablename or type *pointername = address value. We can't leave it type *pointername;, we need to initialize it first. That is because we can change the memory that we didn't want.
Example 1)
There are two lines of code. The first line is the variable and the second line is the pointer.
If we print out n and *ptr, it would both print 100, because the address value of n would become ptr, then with the asterisk, it would return the value saved on the address, which is 100. Same with **pptr, would return 100, because ptr has the address value of n, then with the first asterisk, *pptr would have the address value, then with the second one, it would return 100.
*We use %p to print out the address value.
int n = 100;
int *ptr = &n;
int **pptr = &ptr
Operations of Pointers
Four Rules:
1) Addition, Multiplication, Divison have no meaning.
2) Subtraction of two pointers are the location difference between pointers.
3) Pointers can be added intergers, but not other types of real numbers.
4) Pointers can be compared or substituted. (Ex: if ptr1 == ptr2, ptr2 = ptr1)
We use ++ to add pointers and -- to add pointers. However, the added or subtracted value depends on the data type, for example, if we do ++ to char, then we add 1 since it is 1 byte, and 4 byte if we ++ to int, since the data type is 4 byte.
Ways of sending arguments
We can the data needed for the function in forms of argument. There are two ways of sending:
1) Call by Value
2) Call by reference
For example of call by value,
void local(int);
int main(void) {
int var = 10
printf("%d", var);
local(var)
printf("%d", var);
return 0;
}
void local (int num){
num += 10;
}
Both var prints will give 10, because even though the local changes the var to 20, that var is a copy of original var inside the function, so it wouldn't effect the original var's value, which will still be 10, as the local will be forgotten as soon as the function end.
*If in void local(int num), we give return num, then the copied var, which is 20, will give its value the original var, modifing the second var to be 20.
For example of call by reference,
void local(int*);
int main(void) {
int var = 10
printf("%d", var);
local(&var)
printf("%d", var);
return 0;
}
void local (int* num){
*num += 10;
}
The first var will print 10, but the second var will give 20, because in the function, using the asterisk, the value saved in that address, which is 10, will be increased by 10, giving 20. Even after the function ended, the address will be the same, which will still give the modified value of 20.
'Coding - C' 카테고리의 다른 글
Memory Management (0) | 2022.07.08 |
---|---|
Pointers and Arrays (0) | 2022.07.02 |
Practice Problem Part 2 & Practice Problem #2 (0) | 2022.06.25 |
Multi-Dimensional Array and Practice Problem: Part 1 (0) | 2022.06.18 |
Recursive calls and Practice Problems (0) | 2022.06.04 |