Home > Back-end >  Pointer to a problem
Pointer to a problem

Time:11-26

# include & lt; Stdio. H>
# include & lt; Stdlib. H>

Int main (void)
{
Int * p * q, * * pp;
P=(int *) malloc (3 * sizeof (int));
for(int i=0; i <3; I++)
{
* (p + I)=I + 1;
}
Q=(int *) malloc (4 * sizeof (int));
for(int i=0; i <3; I++)
{
* (q + I)=I + 5;
}

Pp=(int) * * malloc (2 * sizeof (int *));
* pp++=p;
* pp=q;
Printf (" % d, % d \ n ", * (* (pp + 1) + 1), * * (pp));
Free (p); Free (q); Free (pp);

return 0;
}
A: the output of the program results are as follows:
6, 1

The
* pp++=p;
* pp=q;
Printf (" % d, % d \ n ", * (* (pp + 1) + 1), * * (pp));
What do you mean, respectively represent?

CodePudding user response:

Is derived as follows:
P [3]={1, 2, 3}
Q [4]={5,6,7,0}
* pp++=p is equal to the pp [0]=p
Rear * pp=q (+ +) is equivalent to the pp [1]=q

* (pp + 1)=q;
* (* (pp + 1) + 1)=* (q + 1)=q [1]=6;

Pp++=q=="this sentence after the pp + 1=& gt; Pp [1]
* (pp)=p=& amp; P [0]=& gt; Pp - first="pp [0] * (pp) [0]=& gt; P=& gt; & P [0]
* * (pp)=p [0]=1;

Also don't know clearly not, for your reference

CodePudding user response:

Want to analyze the meaning of these Pointers, can consider to put the pointer address printed % p, in order to analysis,

 * (* (pp + 1) + 1) 

This place of crossing the line, because the pp applied for two int * type of space, and in print before pp has pointed to the second int *, so pp + 1 has been crossed again, so the output is undefined behavior,

 # include & lt; Stdio. H> 
# include & lt; Stdlib. H>

Int main (void)
{
Int * p * q, * * pp;
int i;

P=(int *) malloc (3 * sizeof (int));
for(int i=0; i <3; I++)
{
* (p + I)=I + 1;
}
Q=(int *) malloc (4 * sizeof (int));
for(int i=0; i <3; I++)
{
* (q + I)=I + 5;
}

Pp=(int) * * malloc (2 * sizeof (int *));
Printf (" % % % % p, p, p, p \ n ", p, q, pp, pp + 1);
* pp++=p;
* pp=q;
Printf (" % % % % p, p, p, p \ n ", p, q, pp, pp - 1);
Printf (" % % % % p, p, p, p \ n ", p, q, pp, pp + 1);
For (I=0; i <3; I++)
Printf (" % d \ t ", * (* pp + I));
Putchar (10);
Printf (" % d, % d \ n ", * (* (pp + 1) + 1), * * (pp));
Free (p);
Free (q);
Free (pp);

return 0;
}

In the code:
* pp++=p; Is assigned to p * pp, then pp++; Pp elements, pointing to the second int * (applied for two int * malloc and pp pointing to the first int *)
* pp=q; This is understandable, the second element deposit qq address,

CodePudding user response:

 

* pp++=p;
//postfix operator precedence than the assignment operator, * pp++=p execution sequence: * pp, pp=p, pp++ is above said pp [0]=p

* pp=q;
//after pp++, pp [1]=q

Printf (" % d, % d \ n ", * (* (pp + 1) + 1), * * (pp));
/*
* (pp + 1)=q
(* (pp + 1) + 1)=q + 1
* (* (pp + 1) + 1)=* (q + 1) - & gt; Q [1]=6
* (* (pp + 1) + 1)=6
Step after pp + 1 is on pp [1], (pp)=pp [0]
* (pp)=* pp [0] - & gt; * p [0]
P [0]=1, * * (pp)=1
*/

CodePudding user response:

These three statements,
* pp++=p;
* pp=q;
Printf (" % d, % d \ n ", * (* (pp + 1) + 1), * * (pp));
Since after the first pp has increased, so to execute (pp + 1) crossing the line,
But it has a BUG, is the problem of printf execution order, here to perform (pp), the program for the mistake

CodePudding user response:

The original poster can try only output a
Printf (" % d \ n ", * (* (pp + 1) + 1));
You will find the problem
  • Related