0
pointers c
void swap(int *num1, int *num2){ int temp; temp = *num1; *num1 = *num2; *num2 = temp; } int main(){ int num1 = 2, num2 = 9; swap(&num1, &num2); printf("First num %d, second num %d \n", num1, num2); return 0; } Why i need to use '*' here temp = *num1; *num1 = *num2; *num2 = temp; when i also use '*' for arguments
2 Antworten
+ 2
The inputs for the swap function are pointers to integers num1 and num2, which are pointing to the addresses of them.
The function needs to access and change the values in those addresses. Therefore, we use the asterisks. Asterisks are used to get values in the addresses which are pointed by pointers.