另一个c struct语法问题 [英] Another c struct syntax question

查看:71
本文介绍了另一个c struct语法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉第二篇帖子在几个小时内,都是关于结构。


下面的代码编译。变量test1和test2是数据类型teststruct1和teststruct2的结构

。 testintArray的大小是

在teststruct1和teststruct2中明确地设置为2和3。我想

定义一个通用结构,以便testintArray指向一个

整数的数组。我编写的通用结构是teststruct3。


我似乎无法弄清楚如何用我这样的数据初始化test3

的语法用语法{1,{1,2,3}}完成了test2或test1。

我必须在函数main中使用malloc等很长的路要走。我可以使用语法

像{1,{1,2,3}}初始化struct teststring3的test3实例吗?


我将非常感谢语法方面的帮助。谢谢。

优素福

#include< stdlib.h>


struct teststruct1

{

int testint;

int testintArray [2];

} test1 [3] =

{< br $>
{1,{1,2}},

{2,{1,9}},

{3,{2,9 } $

};


struct teststruct2

{

int testint;

int testintArray [3];

} test2 [3] =

{

{1,{1,2,3 },

{2,{1,4,5}},

{3,{2,3,1}}

};


//我想初始化test3就好像我有

//使用上面的test2和test1完成使用

//易于阅读{1,{1,2,3}}语法或其他内容

//喜欢它。

struct teststruct3

{

int testint;

int * testintArray;

} test3 [3];


int main(void)

{

test3 [0] .testint = 1;

int * testArray = malloc(5 * sizeof(int ));

testArray [0] = 1;

testArr ay [1] = 2;

testArray [2] = 4;

testArray [3] = 7;

testArray [4] = 7;

test3 [0] .testintArray = testArray;

返回0;

}

解决方案

2006年9月23日09:14:34 -0700,Yusuf < as ********** @ hotmail.com>

在comp.lang.c中写道:


我很抱歉第二篇帖子在很长时间内,都是关于结构的。


下面的代码编译。变量test1和test2是数据类型teststruct1和teststruct2的结构

。 testintArray的大小是

在teststruct1和teststruct2中明确地设置为2和3。我想

定义一个通用结构,以便testintArray指向一个

整数的数组。我编写的通用结构是teststruct3。


我似乎无法弄清楚如何用我这样的数据初始化test3

的语法用语法{1,{1,2,3}}完成了test2或test1。

我必须在函数main中使用malloc等很长的路要走。我可以使用语法

像{1,{1,2,3}}初始化struct teststring3的test3实例吗?


我将非常感谢语法方面的帮助。谢谢。

优素福


#include< stdlib.h>


struct teststruct1

{

int testint;

int testintArray [2];

} test1 [3] =

{

{1,{1,2}},

{2,{1,9}},

{3, {2,9}}

};


struct teststruct2

{

int testint;

int testintArray [3];

} test2 [3] =

{

{1,{1 ,2,3}},

{2,{1,4,5}},

{3,{2,3,1}}

};


//我想初始化test3就好像我有

//使用上面的test2和test1完成

//易于阅读{1,{1,2,3}}语法或其他内容

//喜欢它。



你不能用这种方式初始化指针,特别是不能在常量

范围内,除了指向char的指针,可以是用字符串文字初始化

。你能做的是:


int array1 [] = {1,3,4,7,7};

int array2 [] = { 3,5,7,9,12};

int array3 [] = {2,4,6,8,10,12,14};


struct teststruct3

{

int testint;

int * testintArray;

};



struct teststruct3 test3 [3] =

{

{sizeof array1 / sizeof * array1,array1} ;

{sizeof array2 / sizeof * array2,array2};

{sizeof array3 / sizeof * array3,array3};

};


请注意,根据我的经验,大多数有经验的C程序员都会在同一来源中定义类型和类型的对象或数组。



构造。更不用说初始化了。


我这样做的方式为源添加了一些额外的空白区域,

这是完全免费的,还有一个额外的struct teststruct3",便宜,

和一个额外的'';'',非常便宜。


int main(void)

{

test3 [0] .testint = 1;

int * testArray = malloc(5 * sizeof(int));

testArray [0] = 1;

testArray [1] = 2;

testArray [2] = 4;

testArray [ 3] = 7;

testArray [4] = 7;

test3 [0] .testintArray = testArray;

返回0;

}



-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp .lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


Yusuf写道:


我很抱歉第二篇帖子在很多小时内,都是关于结构的。


下面的代码编译。变量test1和test2是数据类型teststruct1和teststruct2的结构

。 testintArray的大小是

在teststruct1和teststruct2中明确地设置为2和3。我想

定义一个通用结构,以便testintArray指向一个

整数的数组。我编写的通用结构是teststruct3。


我似乎无法弄清楚如何用我这样的数据初始化test3

的语法用语法{1,{1,2,3}}完成了test2或test1。

我必须在函数main中使用malloc等很长的路要走。我可以使用语法

像{1,{1,2,3}}来初始化struct teststring3的test3实例吗?



在C89中,你不能。在C99中,你可以。不管你使用什么编译器都不支持C99,这很可能是




[...]
< blockquote class =post_quotes>
struct teststruct3

{

int testint;

int * testintArray;

} test3 [3]



= {

{1,(int []){1,2,3}} ,

{2,(int []){2,4,6}},

{3,(int []){3,6,9}} ,

};


Yusuf schrieb:


/ /我想初始化test3 ...

struct teststruct3

{

int testint;

int * testintArray ;

} test3 [3];


int main(无效)

{

test3 [0] .testint = 1;

int * testArray = malloc(5 * sizeof(int));

testArray [0] = 1;

testArray [1] = 2;

testArray [2] = 4;

testArray [3] = 7;

testAr ray [4] = 7;

test3 [0] .testintArray = testArray;

返回0;

}



您可以使用额外的助手数组:


int testintArray0 [5] = {1,2,4,7,7};

int testintArray1 [5] = {1,1 ,1,1,1};

int testintArray2 [5] = {1,2,3,4,5};

struct teststruct3

{

int testint;

int * testintArray;

} test3 [3] =

{{ 1,testintArray0},

{2,testintArray1},

{3,testintArray2}

};


哈勃


I''m sorry for the second post in as many hours, and both about structs.

The code below compiles. The variables test1 and test2 are structures
of datatype teststruct1 and teststruct2. The size of testintArray is
explictly set to 2 and 3 in teststruct1 and teststruct2. I want to
define a generic structure so that testintArray points to an array of
ints. The generic structure I have written is teststruct3.

What I can''t seem to figure out is the syntax how to initialise test3
with data like I have done for test2 or test1 with syntax {1,{1,2,3}}.
I have had to do it a long way with malloc etc in function main. Can I
initialise the test3 instance of the struct teststring3 using a syntax
like{1,{1,2,3}}?

I would appreciate help with the syntax. Thank you.
Yusuf
#include <stdlib.h>

struct teststruct1
{
int testint;
int testintArray[2];
} test1[3] =
{
{1,{1,2}},
{2,{1,9}},
{3,{2,9}}
};

struct teststruct2
{
int testint;
int testintArray[3];
} test2[3] =
{
{1,{1,2,3}},
{2,{1,4,5}},
{3,{2,3,1}}
};

//I want to initialise test3 like I have
//done with test2 and test1 above using the
//easy to read {1,{1,2,3}} syntax or something
//like it.
struct teststruct3
{
int testint;
int *testintArray;
} test3[3];

int main(void)
{
test3[0].testint=1;
int *testArray=malloc(5*sizeof(int));
testArray[0]=1;
testArray[1]=2;
testArray[2]=4;
testArray[3]=7;
testArray[4]=7;
test3[0].testintArray=testArray;
return 0;
}

解决方案

On 23 Sep 2006 09:14:34 -0700, "Yusuf" <as**********@hotmail.com>
wrote in comp.lang.c:

I''m sorry for the second post in as many hours, and both about structs.

The code below compiles. The variables test1 and test2 are structures
of datatype teststruct1 and teststruct2. The size of testintArray is
explictly set to 2 and 3 in teststruct1 and teststruct2. I want to
define a generic structure so that testintArray points to an array of
ints. The generic structure I have written is teststruct3.

What I can''t seem to figure out is the syntax how to initialise test3
with data like I have done for test2 or test1 with syntax {1,{1,2,3}}.
I have had to do it a long way with malloc etc in function main. Can I
initialise the test3 instance of the struct teststring3 using a syntax
like{1,{1,2,3}}?

I would appreciate help with the syntax. Thank you.
Yusuf
#include <stdlib.h>

struct teststruct1
{
int testint;
int testintArray[2];
} test1[3] =
{
{1,{1,2}},
{2,{1,9}},
{3,{2,9}}
};

struct teststruct2
{
int testint;
int testintArray[3];
} test2[3] =
{
{1,{1,2,3}},
{2,{1,4,5}},
{3,{2,3,1}}
};

//I want to initialise test3 like I have
//done with test2 and test1 above using the
//easy to read {1,{1,2,3}} syntax or something
//like it.

You can''t initialize pointers this way, especially not at constant
scope, with the exception of pointers to char which can be initialized
with a string literal. What you can do is this:

int array1 [] = { 1, 3, 4, 7, 7 };
int array2 [] = { 3, 5, 7, 9, 12 };
int array3 [] = { 2, 4, 6, 8, 10, 12, 14 };

struct teststruct3
{
int testint;
int *testintArray;
};

struct teststruct3 test3 [3] =
{
{ sizeof array1 / sizeof *array1, array1 };
{ sizeof array2 / sizeof *array2, array2 };
{ sizeof array3 / sizeof *array3, array3 };
};

Note that in my experience most experienced C programmers frown on
defining a type and an object or array of that type in the same source
construct. Let alone initializing.

The way I have done it adds some extra white space to the source,
which is completely free, and an extra "struct teststruct3", cheap,
and an extra '';'', very very cheap.

int main(void)
{
test3[0].testint=1;
int *testArray=malloc(5*sizeof(int));
testArray[0]=1;
testArray[1]=2;
testArray[2]=4;
testArray[3]=7;
testArray[4]=7;
test3[0].testintArray=testArray;
return 0;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


Yusuf wrote:

I''m sorry for the second post in as many hours, and both about structs.

The code below compiles. The variables test1 and test2 are structures
of datatype teststruct1 and teststruct2. The size of testintArray is
explictly set to 2 and 3 in teststruct1 and teststruct2. I want to
define a generic structure so that testintArray points to an array of
ints. The generic structure I have written is teststruct3.

What I can''t seem to figure out is the syntax how to initialise test3
with data like I have done for test2 or test1 with syntax {1,{1,2,3}}.
I have had to do it a long way with malloc etc in function main. Can I
initialise the test3 instance of the struct teststring3 using a syntax
like{1,{1,2,3}}?

In C89, you cannot. In C99, you can. It''s very well possible that
whatever compiler you are using does not support C99, though.

[...]

struct teststruct3
{
int testint;
int *testintArray;
} test3[3]

= {
{ 1, (int []) { 1, 2, 3 } },
{ 2, (int []) { 2, 4, 6 } },
{ 3, (int []) { 3, 6, 9 } },
};


Yusuf schrieb:

//I want to initialise test3 ...
struct teststruct3
{
int testint;
int *testintArray;
} test3[3];

int main(void)
{
test3[0].testint=1;
int *testArray=malloc(5*sizeof(int));
testArray[0]=1;
testArray[1]=2;
testArray[2]=4;
testArray[3]=7;
testArray[4]=7;
test3[0].testintArray=testArray;
return 0;
}

You can use an additional "helper" arrays:

int testintArray0[5]={ 1, 2, 4, 7, 7 };
int testintArray1[5]={ 1, 1, 1, 1, 1 };
int testintArray2[5]={ 1, 2, 3, 4, 5 };
struct teststruct3
{
int testint;
int *testintArray;
} test3[3]=
{ { 1, testintArray0 },
{ 2, testintArray1 },
{ 3, testintArray2 }
};

Hubble


这篇关于另一个c struct语法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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