创建只读数据数组? [英] Create readonly array of data?

查看:77
本文介绍了创建只读数据数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个只读数组,然后是一个

结构的只读数组。这是我访问的数据,但我希望它能够防止意外更改

。以下是我的测试代码。


#include" stdafx.h"


struct LVC

{

unsigned short int lo;

unsigned short int hi;

};


void main ()

{

//这似乎有用

static const unsigned short int LV [4] =

{0xAC00,

0xAC1C,

0xAC38,

0xAC54};


//这个没有工作。编译器投诉

static const struct LVC [4] = {

{0xAC01,0xAC1B},

{0xAC1D,0xAC37},
{0xAC39,0xAC53},

{0xAC55,0xAC6F},

};


unsigned short i ,j;


i = LVC [2] .lo;

j = LVC [2] .hi;

}


我正在使用Microsoft Visual C ++ Express Edition

(我也想摆脱#include" stdafx.h" if

有一些编译器配置允许我这样做。$

解决方案

< blockquote> Susan Riceaécrit:


//这个没有工作。编译器控制

static const struct LVC [4] = {



static const struct LVC是类型。你必须给该数组一个* name *




例如

static const struct LVC MyTable [4] = {




然后你使用这个名字:

i = MyTable [2] .lo;

j = MyTable [2] .hi;


jacob


PS

它不是无效的()但是int main(void)。

但这是一个细节。


---
http://www.cs.virginia.edu/~lcc-win32

Windows的免费C编译器


Susan Rice写道:


我想创建一个只读的数据数组,然后是

结构的只读数组。这是我访问的数据,但我希望它能够防止意外更改

。以下是我的测试代码。


#include" stdafx.h"



见下文。


struct LVC



大多数人都倾向于为宏保留所有上限。


{

unsigned short int lo;

unsigned short int hi;

};


void main()



这应该是int main(无效)


{

//这似乎有效

static const unsigned short int LV [4] =

{0xAC00,

0xAC1C,

0xAC38,

0xAC54};


//这样做没事。编译器控制

static const struct LVC [4] = {



LVC是您在上面创建的struct类型的名称。你想要什么

是这样的:


static const struct LVC LVC_array [4] = {


{0xAC01,0xAC1B},

{0xAC1D,0xAC37},

{0xAC39,0xAC53},

{0xAC55,0xAC6F} ,

};


unsigned short i,j;


i = LVC [2] .lo;

j = LVC [2] .hi;



然后更改这两个名字。


}


我正在使用Microsoft Visual C ++ Express Edition

(我也想摆脱#include" stdafx.h"如果

有一些编译器配置允许我

这样做。)



这与预编译的头文件有关VC ++。我不知道

专门用来纠正Express,但检查你的项目

设置以消除任何依赖。如果你无法弄明白,那么你需要一个微软新闻组。



Brian





我正在使用Microsoft Visual C ++ Express Edition


(我'我也想摆脱那个#include" stdafx.h"如果

那里有一些编译器配置允许我

这样做。)



当你用msvc ++创建项目时


新项目 - > Win32(win32控制台应用模板自动

选中) - >

(输入项目名称) - > next->选择空项目

选项 - >完成


然后添加你需要的标题和源文件。


更简单......

new project-> selectgeneral项目类型窗口中的选项

- >完成


//这样做没事。编译器投诉

static const struct LVC [4] = {

{0xAC01,0xAC1B},

你没有将模板命名为..已经回答上面


问候


I want to create a readonly array of data, then a readonly array of a
structure. This is data I access but I want it protected against
accidental change. The following is my test code.

#include "stdafx.h"

struct LVC
{
unsigned short int lo;
unsigned short int hi;
};

void main()
{
//This seems to work
static const unsigned short int LV[4] =
{0xAC00,
0xAC1C,
0xAC38,
0xAC54 };

//THIS DOESN''T WORK. COMPILER COMPLAINS
static const struct LVC[4] = {
{ 0xAC01, 0xAC1B },
{ 0xAC1D, 0xAC37 },
{ 0xAC39, 0xAC53 },
{ 0xAC55, 0xAC6F },
};

unsigned short i,j;

i = LVC[2].lo;
j = LVC[2].hi;
}

I''m using Microsoft Visual C++ Express Edition
(I''d also like to get rid of that #include "stdafx.h" if
there''s some compiler configuration that will allow me
to do that.)

解决方案

Susan Rice a écrit :

//THIS DOESN''T WORK. COMPILER COMPLAINS
static const struct LVC[4] = {

static const struct LVC is the type. You have to give a *name*
to that array.

for instance
static const struct LVC MyTable[4] = {
etc

Then you use THAT name:
i = MyTable[2].lo;
j = MyTable[2].hi;

jacob

P.S.
it is not void main() but int main(void).
But that is a detail.

---
http://www.cs.virginia.edu/~lcc-win32
A free C compiler for windows


Susan Rice wrote:

I want to create a readonly array of data, then a readonly array of a
structure. This is data I access but I want it protected against
accidental change. The following is my test code.

#include "stdafx.h"

See below about this.

struct LVC

It''s generally prefered by most to reserve all caps for macros.

{
unsigned short int lo;
unsigned short int hi;
};

void main()

This should be int main(void)

{
//This seems to work
static const unsigned short int LV[4] =
{0xAC00,
0xAC1C,
0xAC38,
0xAC54 };

//THIS DOESN''T WORK. COMPILER COMPLAINS
static const struct LVC[4] = {

LVC is the name of struct type that you created above. What you wanted
was something like:

static const struct LVC LVC_array[4] = {

{ 0xAC01, 0xAC1B },
{ 0xAC1D, 0xAC37 },
{ 0xAC39, 0xAC53 },
{ 0xAC55, 0xAC6F },
};

unsigned short i,j;

i = LVC[2].lo;
j = LVC[2].hi;

Then change these two names as well.

}

I''m using Microsoft Visual C++ Express Edition
(I''d also like to get rid of that #include "stdafx.h" if
there''s some compiler configuration that will allow me
to do that.)

This has to do with pre-compiled headers in VC++. I don''t know
specifically to correct that in Express, but check your project
settings to remove any dependence. If you can''t figure it out, you''ll
need a Microsoft newsgroup.


Brian




I''m using Microsoft Visual C++ Express Edition

(I''d also like to get rid of that #include "stdafx.h" if
there''s some compiler configuration that will allow me
to do that.)

when u create your project in msvc++

new project->Win32(win32 console app template is automatically
selected) ->
(enter a name for the project)->next->select "empty project"
option->finish

then add the header and source files that u need.

and more simply...
new project->select "general " option from the Project type window
->finish

//THIS DOESN''T WORK. COMPILER COMPLAINS
static const struct LVC[4] = {
{ 0xAC01, 0xAC1B },
u did not name the "template" ..it has been answered above

regards


这篇关于创建只读数据数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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