| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
共有 386 人关注过本帖
标题:关于数组和指针好玩的一些小程序~
只看楼主 加入收藏
九转星河
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:长长久久
等 级:版主
威 望:48
帖 子:4932
专家分:13916
注 册:2016-10-22
结帖率:100%
  问题点数:0  回复次数:4   
关于数组和指针好玩的一些小程序~
最近看数组和指针突然有些发现,写出来看看(据说这是写不可维护代码的需要掌握的技能之一)~


这个是数组版的~

程序代码:

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

void test0( void );
void test1( void );
void test2( void );
void test3( void );
void test4( void );
void test5( void );
void test6( void );
void test7( void );
void test8( void );

int main( void )
{
    test0();
    test1();
    test2();
   
    puts("");
   
    test3();
    test4();
    test5();
   
    puts("");
   
    test6();
    test7();
    test8();
   
    return 0;
}

void test0( void )
{
    char s[8]={"abcde"};

    char* const p=s;
   
    printf("test0: ");
   
    *p=*"wxyz123";
   
    puts(s);
}

void test1( void )
{
    char s[8]={"abcde"};

    short* const p=( short* )s;
   
    printf("test1: ");
   
    *p=*(const short* const )"wxyz123";
   
    puts(s);
}

void test2( void )
{
    char s[8]={"abcde"};

    int* const p=( int* )s;
   
    printf("test2: ");
   
    *p=*(const int* const )"wxyz123";
   
    puts(s);
}

void test3( void )
{
    char s[8]={"abcde"};

    void* const p=s;
   
    printf("test3: ");
   
    *p=*(const char* const )"wxyz123";
   
    puts(s);
}

void test4( void )
{
    char s[8]={"abcde"};

    void* const p=s;
   
    printf("test4: ");
   
    *p=*(const short* const )"wxyz123";
   
    puts(s);
}

void test5( void )
{
    char s[8]={"abcde"};

    void* const p=s;
   
    printf("test5: ");
   
    *p=*(const int* const )"wxyz123";
   
    puts(s);
}

#define TEST( NAME, TYPE )    \
void NAME ()    \
{    \
   char s[8]={"abcde"};    \
   \
    void* const p=s;    \
    \
    printf(    \
        #NAME": "   \
    );    \
    \
    *p^=*( TYPE* )s;    \
    *p^=*(const TYPE* const )"wxyz123";    \
    puts(s);    \
}

TEST(test6,char)
TEST(test7,short)
TEST(test8,int)


[此贴子已经被作者于2018-4-1 19:44编辑过]

2018-04-01 17:32
Jonny0201
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:17
帖 子:308
专家分:1423
注 册:2016-11-7
  得分:0 
本来还想看看结果
结果 9 errors
2018-04-03 18:27
九转星河
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:长长久久
等 级:版主
威 望:48
帖 子:4932
专家分:13916
注 册:2016-10-22
  得分:0 
回复 2楼 Jonny0201
具体说说看看,为啥我那个编译器就那么无语地过了(难道是最近我没有再用vc么)~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2018-04-03 18:51
Jonny0201
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:17
帖 子:308
专家分:1423
注 册:2016-11-7
  得分:0 
回复 3楼 九转星河
CLion:
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:82:7: error: incomplete type 'void' is not assignable
    *p=*(const char* const )"wxyz123";
    ~~^
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:95:7: error: incomplete type 'void' is not assignable
    *p=*(const short* const )"wxyz123";
    ~~^
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:108:7: error: incomplete type 'void' is not assignable
    *p=*(const int* const )"wxyz123";
    ~~^
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:129:1: error: invalid operands to binary expression ('void' and 'int')
TEST(test6,char)
^~~~~~~~~~~~~~~~
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:124:7: note: expanded from macro 'TEST'
    *p^=*( TYPE* )s;    \
    ~~^ ~~~~~~~~~~~
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:129:1: error: invalid operands to binary expression ('void' and 'int')
TEST(test6,char)
^~~~~~~~~~~~~~~~
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:125:7: note: expanded from macro 'TEST'
    *p^=*(const TYPE* const )"wxyz123";    \
    ~~^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:130:1: error: invalid operands to binary expression ('void' and 'int')
TEST(test7,short)
^~~~~~~~~~~~~~~~~
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:124:7: note: expanded from macro 'TEST'
    *p^=*( TYPE* )s;    \
    ~~^ ~~~~~~~~~~~
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:130:1: error: invalid operands to binary expression ('void' and 'int')
TEST(test7,short)
^~~~~~~~~~~~~~~~~
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:125:7: note: expanded from macro 'TEST'
    *p^=*(const TYPE* const )"wxyz123";    \
    ~~^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:131:1: error: invalid operands to binary expression ('void' and 'int')
TEST(test8,int)
^~~~~~~~~~~~~~~
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:124:7: note: expanded from macro 'TEST'
    *p^=*( TYPE* )s;    \
    ~~^ ~~~~~~~~~~~
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:131:1: error: invalid operands to binary expression ('void' and 'int')
TEST(test8,int)
^~~~~~~~~~~~~~~
/Users/jonny/Library/Mobile Documents/com~apple~CloudDocs/Xcode/C/CDebug/main.c:125:7: note: expanded from macro 'TEST'
    *p^=*(const TYPE* const )"wxyz123";    \
    ~~^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 errors generated.
2018-04-03 19:45
九转星河
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:长长久久
等 级:版主
威 望:48
帖 子:4932
专家分:13916
注 册:2016-10-22
  得分:0 
回复 4楼 Jonny0201
明白了,空指针里面的内容不能直接赋值,或者先进的编译器已经考虑到用户这样的输入情况,正常来说是不会这样写代码的,何况这个帖子的代码本来就不是正常的,后面不能通过编译的函数去掉,看前3个就可以了

正常来说要把内容拷贝到空指针里面是用memcpy或者memmove的~

[此贴子已经被作者于2018-4-3 20:07编辑过]


[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2018-04-03 20:05







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030027 second(s), 8 queries.
Copyright©2004-2018, BCCN.NET, All Rights Reserved