Solution for Exercise 03_02-scanf2.c
03_02-scanf2.c
#include <stdio.h>
int main()
{
int count,x,*c;
c = &count;
printf("Enter repeat count: ");
/* no ampserand for a pointer */
scanf("%d",c);
for( x=0; x<count; x++ )
puts("Oh boy!");
return 0;
}
Output
Enter repeat count: 5
Oh boy!
Oh boy!
Oh boy!
Oh boy!
Oh boy!
Notes
* Pointer variable c is declared in the same statement as variables count and x. All three are integer variables.
* Pointer c is initialized, assigned to the address of variable count: c = &count;
* In the scanf() statement, pointer variable c is used as the second argument. No & (address-of) operator is required in that c is an address (a pointer): scanf("%d",c);
* The result is that the repeat value is assigned to variable count and used in the for loop with the value that the user input.
Copyright © 1997-2025 by QPBC.
All rights reserved
