C language_MCU_break,return,continue,pointer priority

Publisher:码字狂人Latest update time:2022-04-24 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The test code is as follows


#include "reg51.h"

#include "stdio.h"

#include "intrins.h"

 

typedef void (*p_fun)(unsigned int*);  //void (*p_fun)(unsigned int*); 

p_fun p_fun_a; //Function pointer variable

 

void p_fun_parameter(p_fun p_fun_1,unsigned int *test_b) //function pointer as parameter

{

  p_fun_1(test_b);

}

 

 

 char putchar(char s)

{

    SBUF=s;

   while(TI==0)

   {

      _nop_();

   }

   TI = 0;

  return 0;

}

 

 

void test_return(unsigned int *i)

{

  while((*i)--)

  {

   if((*i)<5)

   {

     return; //When (*i)=4, exit the test_return function

   }

  }

  *i=(*i)+1;

}

 

 

void test_break(unsigned int *i)

{

  while((*i)--)

  {

   if((*i)<5)

   {

     break; //When (*i)=4, exit the while loop and continue to execute *i=*i+1;

   }

  }

  *i=*i+1;

}

 

 

void test_continue(unsigned int *i)

{

  while((*i)--)

  {

   if((*i)<5)

   {

     continue; ///When (*i)=4, exit this loop and continue to execute the next while loop

     *i=*i+1;

   }

  }

  *i=*i+1;

}

 

 

void test_priority(unsigned int *i)

{

  while(*i--)

  {

   if(*i<5)

   {

     break;  

   }

  }

  *i=*i+1;

}

 

 

unsigned int a=10,b=10,c=10,d=10;

void main()

{

  void (*test)(unsigned int*);

  p_fun_parameter(test_return,&a);

  printf("a=%drn",a);

  p_fun_parameter(test_break,&b);

  printf("b=%drn",b);

  test=test_continue;

  test(&c);

  printf("c0=%drn",c);

  test_priority(&d);

  printf("d=%drn",d);

  printf("c1=%drn",c);

  while(1);

}

The test results are as follows

The address of a in memory is 0x22


The address of b in memory is 0x24


The address of c in memory is 0x26


The address of d in memory is 0x28


(The above addresses are the addresses assigned by the microcontroller. Different programs and CPUs assign different addresses.)


Question 1: Why is the answer to c 0 the first time?


When analyzing this problem, we need to pay attention to while((*i)--), note that it is a signed number, and that (*i)-- is used first and then decremented.


Question 2: Why is the second output of c 1?


When analyzing this problem, we need to pay attention to while(*i--), pay attention to the priority of-- and the pointer, * and-- have the same priority, and are combined from right to left, so first the pointer is reduced by 1, and then the content is taken.


As for *i+3, * has a higher priority than +, so the content is taken first and then three is added.


Array names are different from pointers, so the * operation cannot be performed on array names. When passed as actual parameters, the first address is passed. At this time, the * operation can be performed on the formal parameters inside the function, and it can also be understood as an array name, degenerating into a pointer.


Summarize:


The function of return is to exit the function where the loop body is located, which is equivalent to ending the function.

The function of break is to end the loop, jump out of the loop body, and execute the subsequent program, which is the same as the break under the case in switch.

The function of continue is to end this loop and proceed to the next loop;

Reference address:C language_MCU_break,return,continue,pointer priority

Previous article:Notes on using function pointers in Keil C51
Next article:C language_floating point_IEEE 754 standard_MCU_floating point precision

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号