如何在"C"中构造全局指针? [英] How to do global pointers to struct in "C"?

查看:103
本文介绍了如何在"C"中构造全局指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何帮助将不胜感激.
我在下面粘贴了四个文件.当我尝试使用以下命令进行编译时:
gcc -o test Test.c main.c

出现以下错误:
在Test.h:4包含的文件中,
来自Test.c:1:
Types.h:9:7:警告:文件末尾没有换行符
Test.c:9:错误:状态"之前的语法错误
在Test.h:4包含的文件中,
来自main.c:1:
Types.h:9:7:警告:文件末尾没有换行符
main.c:在函数main中:
main.c:6:错误:未声明"Status_Ptr_In_Header"(此函数中的首次使用)
main.c:6:错误:(每个未声明的标识符仅报告一次
main.c:6:错误:出现在每个函数中.)

main.c:

Any help is greatly appreciated.
I have four files pasted below. when I try to compile with this command:
gcc -o test Test.c main.c

Got these errors:
In file included from Test.h:4,
from Test.c:1:
Types.h:9:7: warning: no newline at end of file
Test.c:9: error: syntax error before "Status"
In file included from Test.h:4,
from main.c:1:
Types.h:9:7: warning: no newline at end of file
main.c: In function `main'':
main.c:6: error: `Status_Ptr_In_Header'' undeclared (first use in this function)
main.c:6: error: (Each undeclared identifier is reported only once
main.c:6: error: for each function it appears in.)

main.c:

#include "Test.h"

int main(void)
{
	Status * Status_Ptr;
	Status_Ptr = (Status *) Status_Ptr_In_Header;

	int xx = (int)(&Status_Ptr->Status_1);

	printf("Status_Ptr->Status: %d", xx);
	printf("This is main.");

	return 1;
}


Test.h:


Test.h:

#ifndef TEST_H
#define TEST_H
#include "Types.h"

typedef struct STATUS_STRUCT {
	Status_Enum Status_1;
	Status_Enum Status_2;
	Status_Enum Status_3;
}Status;

extern Status * Status_Ptr_In_Test;

#endif


Test.c:


Test.c:

#include "Test.h"

Status MyStatus = {
	STATUS_ONE,
	STATUS_ONE,
	STATUS_ZERO
};

(Status *)Status_Ptr_In_Test = &MyStatus;


Types.h:

#ifndef TYPES_H
#define TYPES_H

typedef enum STATUS_ENUM{
	STATUS_ZERO = 0,
	STATUS_ONE = 1
}Status_Enum;

#endif

推荐答案

test.c中的代码有些cr脚.
解决另一个错误.

归结为::

The code in test.c is a bit crappy.
Solve one error after the other.

What it boils down to is this :

Status MyStatus = {
STATUS_ONE,
STATUS_ONE,
STATUS_ZERO
};



您想在这里做什么?创建一个数组或枚举吗?
我的猜测是这里应该是数组吗?
那么应该是:
状态Mystatus [] = {STATUS_ONE,STATUS_ONE,STATUS_ZERO};
这也很好:
状态Mystatus [3] = {STATUS_ONE,STATUS_ONE,STATUS_ZERO};
但是实际上第一种方法更好,因为如果您添加一个元素,则不必将3更改为4,编译器会找出数组中有多少元素

我实际上看到了很多错误,因为例如在main.c中,您会执行
Status_Ptr =(状态*)Status_Ptr_In_Header;
并且从不声明Status_Ptr_In_Header.您需要的是将其更改为Status_Ptr_In_Test;



What are you trying to do here ? Create an an array or enum ?
My guess is here is that it is suppused to be an array right ?
Then it should be :
Status Mystatus[] = {STATUS_ONE, STATUS_ONE, STATUS_ZERO};
this is also fine :
Status Mystatus[3] = {STATUS_ONE, STATUS_ONE, STATUS_ZERO};
But actually the first method is better, because if you add an element you don''t have to change the 3 into a 4, the compiler finds out how many elements are in the array

I actually see a lot more errors because for instance in main.c you do
Status_Ptr = (Status *) Status_Ptr_In_Header;
and Status_Ptr_In_Header is never declared. What you need here is this to change it into Status_Ptr_In_Test;


这篇关于如何在"C"中构造全局指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆