Day 10
- Reading: Read Chapter 6
- Lab 5 is due at beginning of lab today
- Lab 6 is due at beginning of lab next Tuesday
- Project 3 is due next Monday
- Lecture Topics
- Limiting string input so you don't go past the end of the array
char name[11];
scanf("%10s", name);
Input Parameters (we've seen these before)
void printPoly(int x, int y)
{
printf("%d", 3 * x * x + 7 * y - 4);
}
int calcPoly(int x, int y)
{
return 3 * x * x + 7 * y - 4;
}
Output Parameters
- Functions can return values through their parameters by passing the address
of the parameter.
void readTenInts(int x[])
{
int i;
for(i = 0; i < 10; i++) {
scanf("%d", &x[i]);
}
}
void readRowCol(int *row, int *col)
{
printf("Enter a row and col: ");
scanf("%d %d", row, col);
}
int main(void)
{
int r, c;
readRowCol(&r, &c);
printf("%d %d\n", r, c);
return 0;
}
Uses of '*' : multiplication, point declaration, dereference a pointer
3 * 4
void switchPlayer(char *player)
{
if (*player == PLAYER_ONE) {
*player = PLAYER_TWO;
} else {
*player = PLAYER_ONE;
}
}
Input/Output Parameters
void switchPlayer(char *player)
{
if (*player == PLAYER_ONE) {
*player = PLAYER_TWO;
} else {
*player = PLAYER_ONE;
}
}