What is the output of the following program?
void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
int main()
{
int a = 100, b = 200;
swap(a, b);
printf("a = %d, b = %d\n", a,
b); /* ??? */
return 0;
}
How can we fix it?
void min_max(int x, int y, int *min, int* max)
{
if (x < y)
{
*min = x;
*max = y;
}
else
{
*min = y;
*max = x;
}
}
int main()
{
int big, small;
min_max(3, 5, &small, &big);
printf("big = %d, small =
%d\n", big, small);
return 0;
}
void printBalance(double* balance)
{
printf("your balance = $%f\n",
*balance);
*balance = 0;
}
int main()
{
double checking_balance = 100;
printBalance(&checking_balance);
/* now pay some bills ??? */
return 0;
}
1. Assume the following declarations have been made:
int nums[] = {100, 200, 300, 400, 500, 600};
int *p = &nums[1]; // = 119
int **q = &p; // = 222
Also assume:
sizeof(int) = 4
What are the values of the following expressions:
*p + 1
*(p + 1)
&nums[0]
q
*q
**q
&p
nums
*nums
*(nums + 1)
nums* + 1
2. Assume the following declarations have been made:
char* state = "
What output is produced by the following code:
int i;
for(i = 0; i < 5; i++)
{
printf("%c..", state[i]);
}
char* p;
for(p = state; *p; p++)
{
printf("%c..", *p);
}
Be able to identify globals, locals, formal parameters and actual parameters in the following code:
double critical = 2000; /* global */
double temperature = 0; /* global */
void incTemp(double amt) /* formal parameter = amt */
{
double temp = temperature + amt; /*
local */
if (temp < critical)
{
temperature = temp;
}
}
int main()
{
int max = 500; /* local */
int i; /* local */
for(i = 0; i < max; i++)
{
int increment = 20; /* local */
incTemp(2 * increment); /* actual
parameter = 40 */
}
return 0;
}