Comment on the following pointer declaration.int *ptr, p;
Question
Comment on the following pointer declaration.int *ptr, p;
Solution
The given pointer declaration int *ptr, p; is a common source of confusion for those new to C or C++ programming. This is because of the way the syntax works in these languages.
Here's a step-by-step breakdown:
-
int *ptr, p;is declaring two variables,ptrandp. -
ptris declared as a pointer to an integer. This means thatptris expected to hold the memory address of an integer, not an integer value itself. -
pis declared as a regular integer. This meanspis expected to hold an integer value.
The confusion often arises because it might look like both ptr and p are being declared as pointers due to the placement of the asterisk (*). However, in C and C++, the asterisk is bound to the variable name that directly follows it, not the type. So only ptr is a pointer in this declaration, not p.
To declare both as integer pointers, you would need to include an asterisk before each variable name, like so: int *ptr, *p;.
Similar Questions
Comment on the following pointer declaration.int *ptr, p; ptr is a pointer to integer, p is notptr and p, both are pointers to integerptr is a pointer to integer, p may or may not beptr and p both are not pointers to integer
In C, what is the result of declaring a pointer as int *ptr; without initialization?
What is the correct syntax to declare a pointer in C++?int &ptr;int ptr*;int *ptr;int ptr&;
In C, what is the result of declaring a pointer as int *ptr; without initialization?Marks : 1Negative Marks : 0Answer hereThe value of the variableptr points to a specific known memory locationptr contains a garbage value as its addressptr points to the memory location 0
What does the expression *ptr++ represent when applied to a pointer ptr in C?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.