Pointer basics: what do the variables represent here, and which statement is correct?
#include
int main()
{
int i = 10;
int *j = &i;
return 0;
}
-
Aj and i are pointers to an int
-
Bi is a pointer to an int and stores address of j
-
Cj is a pointer to an int and stores address of i
-
Dj is a pointer to a pointer to an int and stores address of i
-
Ei and j are both integers with identical values
Answer
Correct Answer: j is a pointer to an int and stores address of i
Explanation
Introduction / Context:This straightforward question confirms understanding of a pointer declaration and initialization in C. It asks you to identify which variable is a pointer and what address it holds.
Given Data / Assumptions:
- int i = 10; defines an integer object i.
- int *j = &i; defines j as a pointer to int, initialized to the address of i.
- No other indirection levels or arrays are used.
Concept / Approach:In C, a declaration of the form int *j means j is a pointer to an int. The ampersand operator & yields the address of its operand. Thus, &i is the address of i, and assigning that to j makes j point to i.
Step-by-Step Solution:i is a plain integer with value 10.j is declared with a star, so it is a pointer to int.j is initialized with &i, meaning j stores the address where i lives.Therefore, *j would yield the value 10 if dereferenced later.
Verification / Alternative check:Adding printf statements like printf("%d", *j); would print 10, demonstrating that j indeed points to i. Printing j with %p would show a nonzero address value.
Why Other Options Are Wrong:i is not a pointer; it is an int. j is not a pointer to a pointer (that would be int *j;). Claiming both are pointers or both are identical integers is incorrect.
Common Pitfalls:Misplacing the star (e.g., int j, int *j—stylistic spacing does not change meaning); confusing the value stored in i with the address stored in j.
Final Answer:j is a pointer to an int and stores address of i