Studying Pointers
InterviewTime limit1sMemory limit1024 MB
Given N, print the declarations of an N-level pointer chain to the int variable a, one declaration per line, with pointers named ptr, ptr2, up to ptrN.
- Level
Easy1 of 10
- Topics
- Implementation
- Solved
- No attempts yet
Problem
Yongmo learned about pointers in his object-oriented programming class today. In a programming language, a pointer is a variable that points to another variable, or to the memory address of that variable.
In C/C++, a pointer is declared as (type) *(variable name), and its value is set with &(already declared variable name). In a declaration, the '*' symbol means the variable is a pointer, and the '&' symbol gives the memory address of a variable.
For example, a pointer ptr to an int variable a can be declared as follows. Here, ptr points to the memory address where a is stored.
int a;
int *ptr = &a;
A pointer can also point to another pointer, which makes multiple levels of pointers. To make a 4-level pointer to an int variable a, write the code as follows. The double pointer ptr2 holds the address of ptr, and the triple pointer ptr3 holds the address of ptr2. You can keep declaring pointers this way for as many levels as you need.
int a;
int *ptr = &a;
int **ptr2 = &ptr;
int ***ptr3 = &ptr2;
int ****ptr4 = &ptr3;
Yongmo was assigned to write the declarations for an N-level pointer to the int variable a. Each pointer variable must be named "ptr", "ptr2", ..., "ptrN" (N is a number), and the code format must match the examples above.
Yongmo hated studying and loved games, and he did not want to spend his time typing code. So he decided to write a program that prints the assignment code for him, then go play games. Try writing that program yourself.
Input
An integer N is given.
Output
Print the variable declarations Yongmo must write for the assignment, over N + 1 lines.
Constraints
1 ≤ N ≤ 100