I received the following question on one of my practice problemswhere it says to determine what is printed by this code:
#include <stdio.h>
int main(){
int ids[3] = {100,200,300};
int *salary, salary1, salary2, *salary3;
salary1 = ids[0] * ids[1];
salary = &ids[1] ;
salary2 = *(ids+1)* *(ids+2);
salary3 = ids+2;
printf("*salary = %d\nsalary1 = %d\n", *salary, salary1);
printf("salary2 = %d\nsalary3 = %p\n", salary2, salary3);
}
I am a bit confused about this code. Firstly, in line 4:
int *salary, salary1, salary2, *salary3;
Why is there an asterisk at the beginning of salary3 if an asterisk was already used in the beginning of the line?
Secondly when it says:
salary1 = ids[0] * ids[1];
how are we supposed to determine the value of salary1 when we don't know the value of ids[1]?
*only applies to the variable right after it, not all the variables on the line.int* salaryetc, butint *salaryetc. The*qualifies the variable, not the type.