学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
 15 12
发新话题
打印

关于 .h .c 多文件程序的问题,搞得晕头转向,大家帮忙

关于 .h .c 多文件程序的问题,搞得晕头转向,大家帮忙

我想建一个栈,顺便练习一下用多文件写一个程序
代码如下,一共四个文件(V.h ,F.h ,F.c ,do.c)请高人指教,编译的时候总是出错:

/*   V.h  文件   共享宏及类型 的定义  */

#ifndef V_H
#define V_H


#define TRUE 1
#define FALSE 0

#define LENTH 100

typedef struct {
    int elem[LENTH];
    int top;
} STACK;

#endif



/*  F.h 函数的声明 */
#ifndef F_H
#define F_H

void init_stack(STACK *S);


int is_empty(STACK *S);


int is_full(STACK *S);


int push(STACK *S ,int x);


int pop(STACK *S ,int *x);


int get_top(STACK *S ,int *x) ;


#endif


/* F.c 函数的实现 */
#include <stdio.h>
#include "V.h"
#include "F.h"


void init_stack(STACK *S)
{
    S->top = -1;
}


int is_empty(STACK *S)
{
    if(S->top == -1)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}



int is_full(STACK *S)
{
    if(S->top == LENTH - 1)
    {
        return  TRUE;
    }
    else
    {
        return FALSE;
    }
}

int push(STACK *S ,int x)
{
    if(S->top == LENTH - 1)
    {
        return  FALSE;
    }

    (S->top)++;
    (S->elem[S->top]) = x;
    return TRUE;
    
}

int pop(STACK *S ,int *x)
{
     if(S->top == -1)
    {
        return FALSE;
    }

     else
     {
         *x = S->elem[S->top];
         (S->top)--;
         return TRUE;
     }

}

int get_top(STACK *S ,int *x)  
{
     if(S->top == -1)
    {
        return FALSE;
    }

     else
     {
         *x = S->elem[S->top];
         return TRUE;
     }

}



/* do.c  这是一个简单的测试*/
#include <stdio.h>
#include "F.h"
#include "V.h"

int main()
{
    STACK *S1;
    int a = 7;
    int *b;
    
    init_stack(S1);
    push(S1,a);
    pop(S1,b);

    printf("%d\n",*b);
    return 0;

}




就这么多,不知道为什么老是出错

TOP

C文件都加到工程中了么?
“视频教程网”免费提供教学资源
C不限制你的自由!
条件是自己承担滥用自由的恶果!

TOP

厄。。。不太清楚,我刚刚学,用的是vc 6.0  怎么加?

TOP

F.h 这个文件少了一句
#include "V.h"加上应该可以了
“视频教程网”免费提供教学资源
C不限制你的自由!
条件是自己承担滥用自由的恶果!

TOP

出来的是这个
do.obj : error LNK2001: unresolved external symbol _pop
do.obj : error LNK2001: unresolved external symbol _push
do.obj : error LNK2001: unresolved external symbol _init_stack
Debug/do.exe : fatal error LNK1120: 3 unresolved externals

是不是因为c.h文件没有被添加的原因,怎么添加呢?

TOP


我用的VS2005,就是这个样子
1>------ 已启动生成: 项目: first, 配置: Debug Win32 ------
1>正在编译...
1>F.c
1>正在链接...
1>LINK : 没有找到 F:\code\Ccode\VSProject\Debug\first.exe 或上一个增量链接没有生成它;正在执行完全链接
1>正在嵌入清单...
1>生成日志保存在“file://f:\code\Ccode\source\first\Debug\BuildLog.htm”
1>first - 0 个错误,0 个警告
========== 生成: 1 已成功, 0 已失败, 0 最新, 0 已跳过 ==========
附件: 您所在的用户组无法下载或查看附件
“视频教程网”免费提供教学资源
C不限制你的自由!
条件是自己承担滥用自由的恶果!

TOP

正在摸索中,谢谢指教,等实在搞不出来,再来请教。

再次感谢:)

TOP

现在compile 和 build 都没问题了,之前是因为没有把F.c文件添加到工程中去




单步调试到 F.c 文件的
void init_stack(STACK *S)
{
    S->top = -1;
}  处时出错,


Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
First-chance exception in ff.exe: 0xC0000005: Access Violation.

什么意思.希望有人指教

[ 本帖最后由 schoolbug 于 2008-5-5 00:36 编辑 ]

TOP

S1这个指针没有初始化,就传入init_stack用非常危险,造成一个内存访问违例。

在S1传入init_stack之前,首先需要初始化
S1 = new STACK; 或者 S1 = (STACK *)malloc(sizeof(STACK));
assert(S1); // 调用assert宏,请包含assert.h,这里确保指针分配成功
程序结束之前,请回收
delete S1; 或者 free(S1);
// 其中malloc和free在string.h中有包含, 在TC中则包含在ALLOC.h或Stdlib.h

另一种方法,就是声名一个已实例化的STACK对象,方法如下
STACK S1;
使用方法
init_stack(&S1);
push和pop同样以这种方法将S1的地址传入。
由于是自动变量,程序结束由系统回收。
我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光

TOP

多谢指点,问题解决了。
两种方法都试过了,都可以。再次感谢!

那种更常用一些?
或者在应用上应该注意些什么,就是那种情况下用第一种方法,那种情况下用第二种方法呢?

TOP

 15 12
发新话题