C-diary (1) - the exchange of the two numerical

2010-08-11  来源:本站原创  分类:CPP  人气:145 

In the C language, has a strong pointer in java, the two exchanged a few seemed so complicated. All of this, in C, using a pointer, it will be so easy.

Through pointer variables as function parameters

#include <stdio.h>
#include <stdlib.h>

/*
* Exchange integer
*/
void swap(int *p1,int *p2)
{
    int temp;
    temp=*p2;
    *p2=*p1;
    *p1=temp;
}

void testSwap()
{
     int i=5,j=10;
     printf("before swap i:%d  j:%d",i,j);
     swap(&i,&j);
     printf("after swap i:%d  j:%d",i,j);

}

int main()
{

    //test1();
    testSwap();
    return 0;
}

Compile and run the code above, we get the following results

before swap i: 5 j: 10after swap i: 10 j: 5
Process returned 0 (0x0) execution time: 0.016 s
Press any key to continue.

But one thing to note, do not attempt to change the pointer value of Er Shi parameter pointer argument of the value changes, such as the following code

void swap(int *p1,int *p2)
{
    int *temp;
    temp=p2;
    p2=p1;
    p1=temp;
}

Will not achieve the number of exchange of the two

相关文章
  • C-diary (1) - the exchange of the two numerical 2010-08-11

    In the C language, has a strong pointer in java, the two exchanged a few seemed so complicated. All of this, in C, using a pointer, it will be so easy. Through pointer variables as function parameters #include <stdio.h> #include <stdlib.h> /*

  • Study Diary 2.5 2009-03-18

    - - | I have a month and a half has not written a diary study. Is that this one and a half months, the progress of my self almost to 0. Sin ah, sin. December 14, I've been busy CET go, and then a period of one month is huge draw for the final exam. A

  • Project diary -20090308 2009-04-03

    Forgotten diary yesterday, and was not too lazy to write, but really forgotten. Yesterday was Saturday morning, 10 o'clock many sleep, normal sleep are put back up (usually a serious lack of sleep), yesterday afternoon to play a game of one afternoon

  • Project diary -20090316 2009-04-20

    No diary for several days, and on Thursday at Yiwu Mody research needs. User called a company-wide view of each department manager of our prototype. Found that really difficult to communicate with users ah, all departments consider their demand for t

  • exchange 2003 disaster recovery note 2010-02-11

    exchange 2003 disaster recovery note Customers an exchange 2003 server down machine, the service can not start. First try a good way to repair services. No .. Later, the exchange of stored files directly copied to another partition, the system re-ins

  • Exchange a and b, one-way "value transfer" 2010-03-15

    1, first contact with the C programming language on the understanding of the East, the last written actually confused. Summarize here, in fact, in one sentence: parameter "one-way value passing" parameter did not change the actual parameter valu

  • oracle partition exchange 2010-03-10

    - Building table create table A_TT ( A VARCHAR2 (10), B DATE ) partition by range (A) ( partition PART_1 values less than ('10 ') tablespace SMCRPT_HOME pctfree 10 initrans 1 maxtrans 255 storage ( initial 80K next 1M minextents 1 maxextents unlimite

  • Android JSON-based data exchange application 2010-03-15

    JSON definition: A lightweight data interchange format, has a good quick read and easy to write features. The mainstream technology industry to provide a complete solution (somewhat similar to regular expressions, obtained the support of today's most

  • SOA-based platform for the exchange of government information resources 2010-03-23

    In accordance with the National Leading Group issued the "overall framework of national e-government" e-government network, government information resources directory system and exchange system and information security based on the three major e

  • flash and the background data exchange methods the 1-LoadVars articles 2010-04-10

    PS Recently, some friends always ask flash to interact with the background problem, so I made some conclusions and order, and I think we all have had easy! Now I know, Flash and background data exchange there are several ways the following (if you fe

  • Dialog data exchange - MFC layman 2010-04-12

    Dialog data exchange means the following two actions, or to write data to the corresponding control memory window, or read data from the control window and saved to the memory variable. MFC To simplify these operations, to CDataExchange class and som

  • [Sort structural feature 2] exchange sort 2010-04-14

    1, bubble sort O (N ^ 2) Bubble sort, the process is simple, first the first and second keywords keyword comparison, if the reverse, two records will be exchanged. And then use the second and third keyword keyword compare, and so on, that the first n

  • Classic interview: not using a third variable to complete the exchange of two integers 2010-04-17

    Do not use a third variable to complete the exchange of two integers, how? public class Person { public static void main(String[] args) { // Use to add and subtract int a = 10, b = 5; a = a + b; b = a - b; a = a - b; System.out.println("a=" + a

  • Entertaining, three-layer switching and exchange (Reprinted) 2010-04-21

    Linghu Chong 14 years old into the Mountain, lazy Susan-year-old that year, Yue Buqun day for two pointing swordsmanship, Linghu Chong night time story to the younger, she fell asleep. Later, Yue Buqun gradually collected Radno, Lu Dayou other discip

  • 100 e purchase development diary 2010-04-23

    100 e purchase development diary (1) Lasted about 18 months of development, mainly on weekends and at night, 100 bought last tottering e came to this earth, 100 e buy is what? specific view . From the beginning until now has experienced a lot of inte

  • 100 e purchase development diary (2) 2010-04-23

    100 e purchase development diary (2) Logging - System Diary View previous back: the deployment of Logging, if you used the java world inside Log4j, then the Python in the not too familiar with the general principles of play diary: In accordance with

  • 100 e purchase development diary (1) 2010-04-23

    100 e purchase development diary (1) Lasted about 18 months of development, mainly on weekends and at night, 100 bought last tottering e came to this world, best100shop What is this? specific view . From the beginning until now has experienced a lot

  • 100 e purchase development diary (3) 2010-04-24

    100 e purchase development diary (3) Batch processing (JOB) Many operating systems can be very time-consuming, real-time processing is bound to affect the experience of the system. For example, a time-consuming verification, or need other systems ope

  • 100 e purchase development diary (4) 2010-04-24

    100 e purchase development diary (4) Static files View on the back: the deployment of the system journal batch In the project CSS javascript upload pictures, Django itself does not serve static files so the first step is to configure static files Apa

  • Without a temporary variable exchange value of two numbers 2010-04-25

    Without a temporary variable exchange value of two numbers http://blog.csdn.net/minglois/archive/2009/07/29/4391514.aspx Quote When you want to exchange the value of two numbers, the usual practice is to define a temporary variable, and then exchange