int *p = 25;
It causes an int <<>> int * conversion error, although the * operator is for value-by-address.
This sentence does not use a dereference operator * .
This offer is an ad pointer. Compare for example the following ad.
int a[1] = { 25 };
Here, similarly, the indexing operator [], not used [], and the array declaration takes place.
That is, the same tokens have different meanings depending on the context.
The compiler displays an error message in this declaration.
int *p = 25;
since there is no implicit conversion of an integer literal 25 of type int to the type of the value of the pointer int * .
As for this ad
int a,b,c,d,*p,x,y,z;
then it can be rewritten as
int ( a ), ( b ), ( c ), ( d ), ( *p ), ( x ), ( y ), ( z );
which in this particular case is equivalent to a series of ads
int ( a ); int ( b ); int ( c ); int ( d ); int ( *p ); int ( x ); int ( y ); int ( z );
which is naturally easier to write as
int a; int b; int c; int d; int *p; int x; int y; int z;
In general, a simple declaration (in terms of the C ++ standard simple-declaration ) looks as follows:
decl-specifier-seq init-declarator-list;
That is, a sequence of declaration specifiers (int, long, bool, etc.) and a sequence of declarators (declared variables).
For example, in a single declaration-declaration you can declare, for example, an object of type int , a pointer of type int * , an array with elements of type int and a function (or pointer to a function) having the type of return value int or even int *
View the following demo.
#include <iostream> int f() { return 10; } int * g() { static int x = 20; return &x; } int main() { int x, *p, a[1], f(), *g(), ( *pf )() = f, *( *pg )() = g; p = &x; x = 1; std::cout << "x = " << x << '\n'; ++*p; std::cout << "*p = " << *p << '\n'; a[0] = x + 1; std::cout << "a[0] = " << a[0] << '\n'; std::cout << "f() = " << f() << '\n'; std::cout << "*g() = " << *g() << '\n'; std::cout << "pf() = " << pf() << '\n'; std::cout << "*pg() = " << *pg() << '\n'; return 0; }
Its output to the console:
x = 1 *p = 2 a[0] = 3 f() = 10 *g() = 20 pf() = 10 *pg() = 20
Regarding this code snippet from your question
int a,b,c,d,*p,x,y,z; *p=25;
then in the first sentence-declaration a pointer is declared with the name p, which is not initialized, provided that this variable has an automatic memory duration (local variable), and therefore has an undefined value, or is initialized with a constant null pointer, if the variable has a static memory duration ( declared outside the function).
The second sentence is an expression sentence. It uses a pointer dereference operator * and an assignment operator.
Since the value of the pointer is either undefined or nullptr , this nullptr leads to undefined program behavior.
As for this proposal
int *p; p=&25;
then there is an attempt to take the address of an integer literal, rather than an object in memory. Therefore, the compiler generates an error message.
*p. This is an undefined behavior that the compiler actually swears at very strongly. This example does not work for me, but falls with a segmentation error - andreymal"не работает", specify, either does not compile (and give a message to the compiler (as well as what exactly you are compiling)), or when you run for execution (OS also indicate) what you expected to see, and what you directly observe. - avp