| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 452 人关注过本帖
标题:类型转换
只看楼主 加入收藏
venus85
Rank: 6Rank: 6
等 级:侠之大者
帖 子:159
专家分:477
注 册:2010-11-27
结帖率:64.71%
收藏
已结贴  问题点数:20 回复次数:4 
类型转换
大神帮忙看看这个程序,C++编程思想的第一个程序,问题主要出在void*   fetch(Stash* S , int index);函数的返回值类型上,后面调用fetch函数时
出现类型转换不成功,不知道这个是我的编译器不支持还是程序本身有问题,谢谢


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

#define BUFFSIZE 25

typedef struct STASHtag
{
    int size;
    int    quantity;
    int    next;
    unsigned    char*    storage;
}Stash;

void    init(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);


void init(Stash* S , int Size)
{
    S->size = Size;
    S->quantity = 0;
    S->next = 0;
    S->storage = NULL;
}

void cleanup(Stash* S)
{
    if(S->storage != NULL)
    {
            puts("freeing storage!");
            free(S->storage);
    }
    else
    {
        puts("storage is alreadly empty!");
    }
}

int add(Stash* S , void* element)
{
    if(S->next > S->quantity)
    {
        inflate(S , 100);   
    }   
    memcpy(&(S->storage[S->next * S->size]) , element , S->size);
    S->next++;
    return(S->next - 1 );
}

void* fetch(Stash* S , int index)
{
    if(index >= S->next || index < 0)
        return 0 ;
    return (S->storage[index * S->size ]);   
}

int count(Stash* S)
{
    return S->next;   
}

void inflate(Stash* S ,int increase)
{
    void* v = realloc(S->storage , (S->quantity + increase)*S->size);
    assert(v);
    S->storage = v;
    S->quantity += increase;   
}


int main(void)
{
    Stash intStash , stringStash;
    int i;
    FILE* file;
    char buf[BUFFSIZE];
    char* cp;
   
    init(&intStash , sizeof(int));
    for(i = 0 ; i < 50 ; i++)
        add(&intStash , &i);
        
    init(&stringStash , sizeof(char)*BUFFSIZE);
    file = fopen("funtion.cpp" , "r");
    assert(file);
    while(fgets(buf , BUFFSIZE , file))
    {
        add(&stringStash , buf);   
    }
    fclose(file);
    for(i = 0; i<count(&intStash) ; i++)
    {
        printf("fetch(&intStash , %d) = %d\n" , i , *(int*)fetch(&intStash , i));
    }
    i = 0;
    while((cp = fetch(&stringStash , i++)) != 0)
    {
        printf("fetch(&stringStach , %d) = %s\n" , i-1 , cp);
    }
    putchar('\n');
    cleanup(&intStash);
    cleanup(&stringStash);
}
搜索更多相关主题的帖子: void quantity include storage 
2013-03-24 14:50
qq337478309
Rank: 1
等 级:新手上路
帖 子:2
专家分:5
注 册:2013-3-24
收藏
得分:5 
强制类型转换时:(类型名)(表达式)
2013-03-24 15:48
tompobing
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:260
专家分:809
注 册:2012-12-9
收藏
得分:5 
void*,不太了解
2013-03-25 20:03
小xiong
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:388
专家分:1722
注 册:2013-2-8
收藏
得分:5 
返回空类型指针,可以强制转换为其它指针
2013-03-26 11:10
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:5 
void类型的指针函数有返回么

Maybe
2013-03-26 11:20
快速回复:类型转换
数据加载中...
 
   



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

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