C++ stack, parameter passing and pointers

Publisher:vnerwb池塘边Latest update time:2015-05-07 Source: 51heiKeywords:C++ Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//1. Pointer function

#include "stdafx.h"

 
void fun1(int a,int b)
{
printf("%d %d ",a,b);
}
 
 
int _tmain(int argc, _TCHAR* argv[])
{
 
void (*fun)(int x,int y); //void is the return value type of the function being pointed to, int is the parameter type of the function being pointed to
for=for1;
fun(10,20);
return 0;
}

2. Parameter passing

// 0224.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
int a=3;
int b=4;
void fun(int &x,int &y) // This is a reference transfer. That is, no new space is created in the stack, and the memory data of x and y are exchanged.
{//Note that the meaning of & here is not to get the address
int tem;
has=x;
x=y;
y=tem;
}
void fun1(int x, int y) // In this case, the value is passed, and two spaces x and y will be opened in the stack, which will exchange the values ​​in the stack but will not affect the heap.
{
int tem;
has=x;
x=y;
y=tem;
}
 
void fun2(int *p1,int *p2)
{
int tem;
has=*p1;
*p1=*p2;
*p2=has;
}
int _tmain(int argc, _TCHAR* argv[])
{
fun(a,b);
printf("a=%d  b=%d ",a,b);
 
fun1(a,b);
printf("a=%d  b=%d ",a,b);
 
fun2(&a,&b); //Formal parameter is pointer, actual parameter is address
printf("a=%d  b=%d ",a,b);
 
return 0;
}
Keywords:C++ Reference address:C++ stack, parameter passing and pointers

Previous article:C++ while loop control
Next article:Using C++ array parameters

Recommended ReadingLatest update time:2024-11-15 15:05

MCU C Language Tutorial (V)
  In the previous section, we learned about the data types supported by the KEIL C51 compiler. How are these data types used in the definition of constants and variables? What is the meaning of constants? Is there anything you should pay attention to? Let's take a look. Wow! You still can't tell the differen
[Microcontroller]
MCU C Language Tutorial (V)
The seventh example of one hundred examples of C language
Code: /* The seventh example of C language Question: Output a special pattern, please run it in the c environment and take a look, Very Beautiful! Program analysis: There are 256 characters in total. Different characters have different graphics. Just output the corresponding characters. */ #include stdio.h voi
[Microcontroller]
The seventh example of one hundred examples of C language
STC12C5A60S2 ADC interrupt mode
/*------------------------------------------------------------------*/ /* --- STC MCU Limited ---------------------------------------------*/ /* --- STC12C5Axx Series MCU A/D Conversion Demo -------------------*/ /* --- Web: www.STCMCU.com -----------------------------------------*/ /* If you want to use the program o
[Microcontroller]
Writing of C51 delay function delay
For example, to write a delay function with a delay of 10ms: (12M crystal oscillator) void delay_10ms() {      unsigned char i,j,k;      for(i=5;i 0;i--)      {            for(j=4;j 0;j--)            {                    for(k=248;j 0;j--);             }       } } Principle: 3 layers of for loops, the number of loo
[Microcontroller]
Exynos4412 C language implementation of water lamp
Earlier we used assembly to complete a running light experiment:  Tiny4412 assembly running light code, Tiny4412 bare metal LED operation However, assembly language is not very readable, so in this section we use C language to implement the same function, and we will try to use C language for future experiments. When
[Microcontroller]
Exynos4412 C language implementation of water lamp
MSP430 C language keyword expansion
1.interrupt is used for interrupt function. The definition is as follows: Syntax: interrupt void function name() or interrupt void function name() Parameters: The interrupt function has no parameters. The interrupt function needs to specify the interrupt vector. Return: The interrupt function is generally
[Microcontroller]
Application of I2C of PIC microcontroller (24LC02)
 I2C Bus Features     The main advantages of the I2C bus are its simplicity and effectiveness. Since the interface is directly on the components, the I2C bus takes up very little space, reducing the space on the circuit board and the number of chip pins, reducing the cost of interconnection. The bus can be up to 25 fe
[Microcontroller]
Application of I2C of PIC microcontroller (24LC02)
Analysis of const, pointers and references in C++
The parameters of functions in C++ are much more complicated than those in C, mainly because C++ introduces references and const qualifiers. The introduction of these two objects makes the function parameters in C++ extremely complex and changeable, and each type has a more suitable scope of use. This article analyzes
[Microcontroller]
Analysis of const, pointers and references in C++
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号