【编程范式】连续数据的前面部分后移的实现
2013-07-01 21:06
274 查看
学习编程范式到第七节课了,直接上代码:
编译的时候会发现错误,错误如下:
错误解析:
buff[beforeSize]
是错误的,因为这种预定义的数组是不能用变量当作数组大小的。这也会出编译错误。
如果想在运行时决定数组的大小,请用new操作符分配数组,或者malloc之类的堆分配函数。
例如:
char* buff = new char[beforeSize];
// rotate.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "memory.h" #include "string.h" int main(int argc, char* argv[]) { printf("Hello World!\n"); return 0; } /************************************************************************/ /* 连续数据的前面部分后移的实现 */ /************************************************************************/ void rotate(void * front,void * middle,void * end){ int beforeSize = (char *)middle - (char *)front; int endSize = (char*)end - (char *)middle; char buff[beforeSize]; memcpy(buff,front,beforeSize); memmove(front,middle,endSize); memcpy((char *)end-beforeSize,buff,beforeSize); }
编译的时候会发现错误,错误如下:
E:\vc\rotate\rotate.cpp(19) : error C2057: expected constant expression E:\vc\rotate\rotate.cpp(19) : error C2466: cannot allocate an array of constant size 0 E:\vc\rotate\rotate.cpp(19) : error C2133: 'buff' : unknown size
错误解析:
buff[beforeSize]
是错误的,因为这种预定义的数组是不能用变量当作数组大小的。这也会出编译错误。
如果想在运行时决定数组的大小,请用new操作符分配数组,或者malloc之类的堆分配函数。
例如:
char* buff = new char[beforeSize];
相关文章推荐
- 【编程范式】连续数据的前面部分后移的实现
- 【编程范式】连续数据的前面部分后移的实现
- 用JavaScript与WebService实现网页部分数据XML传送
- Jquery实现只提交部分Form表单数据
- SOCKET编程之使用AF_UNIX实现本机数据流通信示例
- 数据结构与算法学习笔记——动态规划的入门与编程实现
- SharePoint:扩展DVWP - 第16部分:为实现可维护的下拉框准备数据
- DB2 XML 编程,第 1 部分:理解 XML 数据模型
- linux 网络编程:使用两线程实现socket同时收发数据
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(一)----基础类库部分
- 浅析 Linux 中的时间编程和实现原理,第 1 部分: Linux 应用层的时间编程
- java编程如何实现多条2017-08-08 22:10:00.0这样的时间数据,相差多少天?(隔24小时为相差1天,否则为0天)
- 日志系统数据采集客户端的实现--并发编程容器选型
- 编程实现求出若干个整数之和为500的连续整数(如98,99,100,101,102),所有组合。
- 实现 Castor 数据绑定,第 2 部分: 编组和解组 XML
- iOS 6 编程--Core Data持久化数据存储(2)-使用Core Data实现简单ShoppingCart应用程序
- 实现在删除数据后,自增列的值连续
- Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--前面6篇博文全部源代码下载地址
- c语言编程实现电脑com口发数据
- 浅析 Linux 中的时间编程和实现原理,第 1 部分: Linux 应用层的时间编程