c版本与c++版本的动态数组代码
C版本:
vim stash.h
#ifndef STASH_H
#define STASH_H
typedef struct STASHTag {
int size; /* Size of each space */
int quantity; /* Number of storage spaces */
int next; /* Next empty space */
/* Dynamically allocated array of bytes: */
unsigned char* storage;
} Stash;
void initialize(Stash* S, int Size);
void cleanup(Stash* S);
int add(Stash* S, void* element);
void* fetch(Stash* S, int index);
int count(Stash* S);
void inflate(Stash* S, int increase);
#endif //STASH_H
vim stash.c
/* Error testing macros: */
#include <assert.h>
/* Dynamic memory allocation functions: */
#include <stdlib.h>
#include <string.h> /* memcpy() */
#include <stdio.h>
#include "stash.h"
#define STEP 10
void initialize(Stash* S, int Size) {
S->size = Size;
S->quantity = 0;
S->storage = 0;
S->next = 0;
}
void cleanup(Stash* S) {
if(S->storage) {
puts("freeing storage");
free(S->storage);
}
}
int add(Stash* S, void* element) {
/* enough space left? */
if(S->next >= S->quantity)
inflate(S, STEP);
/* Copy element into storage,
starting at next empty space: */
memcpy(&(S->storage[S->next * S->size]),
element, S->size);
S->next++;
return(S->next - 1); /* Index number */
}
void* fetch(Stash* S, int index) {
if(index >= S->next || index < 0)
return 0; /* Not out of bounds? */
/* Produce pointer to desired element: */
// return &(S->storage[index * S->size]);
return (S->storage+index*S->size);
}
int count(Stash* S) {
/* Number of elements in stash */
return S->next;
}
void inflate(Stash* S, int increase) {
void* v =
realloc
相关文档:
本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的数据结构和函数,并对计时、时间的获取、时间的计算和显示格式等方面进行了阐述。本文还通过大量的实例向你展示了time.h头文件中声明的各种函数和数据结构的详细使用方法。
关键字:UTC(世界标准时间),Calendar Time( ......
typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。当typedef与结构结合使用时,会有一些比较复杂的情况,而且在C语言和C++里面略有差别,本文将详细讲解typedef struct的用法。
第一章 typedef struct 与 struct的区别
1. 基本解释
ty ......
C陷阱于缺陷这本书到图书馆借了很久,一直都没有细细的看,现完整的看了之后把认为重要的,一般人可能忽视的问题给做了笔记,希望有所帮助
2010.02.25
二维数组:
int calendar[12][31];
int * p;
int i;
calendar[4]的含义:
calendar[4]是calendar数组的第5个元素,是calendar数组中12个有着31个整型元素的数组之一 ......
1.打开“我的电脑”-“工具”-“文件夹选项”-“查看”-在“显示所有文件和文件夹”选项前打勾-“确定”
2.删除以下文件夹中的内容:
x:\Documents and Settings\用户名\Cookies\下的所有文件(保留index文件)
x:\Documents and Settings\用户名\Local Settings\Temp\下的所有文件(用户临时文件)
x:\ ......
简述C和C++程序员学习历程
收藏
< type="text/javascript">
document.body.oncopy = function() {
if (window.clipboardData) {
setTimeout(function() {
......