设为首页 加入收藏

TOP

void类型参数在C/C++中的不同
2012-11-01 15:46:55 来源: 作者: 【 】 浏览:356
Tags:void 类型 参数 C/C 不同
    在C语言中,void类型是没有问题,代码如下:main.c
   
    [cpp] view plaincopy
   
    #include <stdio.h>
   
    #include <string.h>
   
    #define GENERIC
   
    void *MyStrcpy( void *dst, const void GENERIC *src, unsigned int len )
   
    {
   
    unsigned char *pDst;
   
    const unsigned char GENERIC *pSrc;
   
    pSrc = src;
   
    pDst = dst;
   
    while ( len-- )
   
    *pDst++ = *pSrc++;
   
    return ( pDst );
   
    }
   
    void main()
   
    {
   
    int i=1;
   
    int j=2;
   
    char d[]=“123”;
   
    char s[]=“123456789”;
   
    char d1[]=“123”;
   
    char s1[]=“123456789”;
   
    strcpy(d,s);
   
    printf(“s=%s   d=%s\n”,s,d);
   
    MyStrcpy(d1,s1,3);
   
    printf(“d1=%s   s1=%s\n”,s1,d1);
   
    printf(“i=0X%08X\nj=0X%08X\ns=0X%08X\nd=0X%08X\ns1=0X%08X\nd1=0X%08X\nstrcpy=0X%08X\nprintf=0X%08X\n”,&i,&j,s,d,s1,d1,strcpy,printf);
   
    }
   
    而在C++(www.cppentry.com)中,我们要改成模板,代码如下:main.cpp
   
    [cpp] view plaincopy
   
    #include <stdio.h>
   
    #include <string.h>
   
    #define GENERIC
   
    template <typename T>
   
    void *MyStrcpy( T *dst, const T GENERIC *src, unsigned int len )
   
    {
   
    T *pDst;
   
    const T GENERIC *pSrc;
   
    pSrc = src;
   
    pDst = dst;
   
    while ( len-- )
   
    *pDst++ = *pSrc++;
   
    return ( pDst );
   
    }
   
    void main()
   
    {
   
    int i=0,j=1;
   
    char s[]=“123456789”;
   
    char d[]=“123”;
   
    char s1[]=“123456789”;
   
    char d1[]=“123”;
   
    strcpy(d,s);
   
    printf(“s=%s   d=%s\n”,s,d);
   
    MyStrcpy(d1,s1,3);
   
    printf(“d1=%s   s1=%s\n”,s1,d1);
   
    printf(“i=0X%08X\nj=0X%08X\ns=0X%08X\nd=0X%08X\ns1=0X%08X\nd1=0X%08X\nstrcpy=0X%08X\nprintf=0X%08X\n”,&i,&j,s,d,s1,d1,strcpy,printf);
   
    }
   
    以上代码的功能是一样的,但是我们要进行不同的改变。上面是在VC++(www.cppentry.com)6.0编译环境下测试
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++与C#对比学习:消息,事件 下一篇HDU 2209 翻纸牌游戏

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: