初始化作为指针指针实现的const矩阵 [英] Initialization of a const matrix implemented as pointer-to-pointer

查看:64
本文介绍了初始化作为指针指针实现的const矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主题:作为指针指针实现的const矩阵的初始化


大家好。


我有在源文件中跟随矩阵定义


static const char ** a;


我需要将它初始化为字符串数组,类似


static const char ** a = {" Alpha"," Beta",Charlie"};


我知道字符串的数量和它们的最大长度,但由于兼容性原因,我不能使用

堆栈分配的矩阵与另一段

代码,它只是定义了一个


char **字符串


矩阵必须分配给它。


我以为我可以为每个字符串写一个静态const声明


static const str1 [MAX_LENGTH] =" Alpha"


然后声明矩阵为


static const char * a [NUM_OF_STRINGS] = {str1,str2,... }


这是正确的吗?


感谢您的关注


AT

Subject: Initialization of a const matrix implemented as pointer-to-pointer

Hello everyone.

I''ve got the following matrix definition in a source file

static const char **a;

I need it to be initialized as an array of strings, something like

static const char **a = {"Alpha", "Beta", Charlie"};

I know the number of strings and their maximum length, but I can''t use a
stack-allocated matrix for compatibility reasons with another piece of
code, which simply defines a

char** strings

to which the matrix has to be assigned to.

I thought I could write a static const declaration for each string

static const str1[MAX_LENGTH] ="Alpha"

and then declare the matrix as

static const char* a[NUM_OF_STRINGS] = {str1,str2,...}

would it be correct?

thanks for your attention

AT

推荐答案

Andrea Taverna(Tavs)写道:
Andrea Taverna (Tavs) wrote:

主题:初始化const矩阵实现为指针指针


大家好。


我在源文件中有以下矩阵定义


static const char ** a;


我需要将它初始化为字符串数组,例如


static const char ** a = {" Alpha"," Beta",Charlie"};


我知道字符串的数量及其最大长度但由于兼容性原因,我不能使用

堆栈分配的矩阵与另一段

代码,它只是定义了一个


char **字符串


矩阵必须分配给它。


我是我可以为每个字符串写一个静态const声明


static const str1 [MAX_LENGTH] =" Alpha"


然后声明矩阵为


static const char * a [NUM_OF_STRINGS] = {str1,str2,...}


这是正确的吗?


感谢您的关注
Subject: Initialization of a const matrix implemented as pointer-to-pointer

Hello everyone.

I''ve got the following matrix definition in a source file

static const char **a;

I need it to be initialized as an array of strings, something like

static const char **a = {"Alpha", "Beta", Charlie"};

I know the number of strings and their maximum length, but I can''t use a
stack-allocated matrix for compatibility reasons with another piece of
code, which simply defines a

char** strings

to which the matrix has to be assigned to.

I thought I could write a static const declaration for each string

static const str1[MAX_LENGTH] ="Alpha"

and then declare the matrix as

static const char* a[NUM_OF_STRINGS] = {str1,str2,...}

would it be correct?

thanks for your attention



对我来说还行。

你不需要NUM_OF_STRINGS。

如果你有可用的字符串写

static const str1 [MAX_LENGTH] =" Alpha";

那么你就不要也不需要str1。


怎么样

static const char * a [] = {" Alpha"," Beta",Charlie" };




/ * BEGIN new.c输出* /


Alpha

Beta

查理


/ * END new.c输出* /


/ * BEGIN new .c * /


#include< stdio.h>


int main(无效)

{ <无线电通信/>
static const char * a [] = {" Alpha"," Beta"," Charlie"};

size_t index;


put(" / * BEGIN new.c output * / \ n");

for(index = 0; index!= sizeof a / sizeof * a; ++ index){

puts(a [index]);

}

puts(" \\\
/ * END new。 c输出* /");

返回0;

}


/ * END new.c * /


-

pete

Looks OK to me.
You don''t need NUM_OF_STRINGS.
And if you have the strings available to write
static const str1[MAX_LENGTH] ="Alpha";
then you don''t need str1 either.

How about
static const char *a[] = {"Alpha", "Beta", Charlie"};
?

/* BEGIN new.c output */

Alpha
Beta
Charlie

/* END new.c output */

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
static const char *a[] = {"Alpha", "Beta", "Charlie"};
size_t index;

puts("/* BEGIN new.c output */\n");
for (index = 0; index != sizeof a / sizeof *a; ++index) {
puts(a[index]);
}
puts("\n/* END new.c output */");
return 0;
}

/* END new.c */

--
pete


2008年9月10日星期三13:57:47 +0200, Andrea Taverna(Tavs)"

< a。**** @ libero.itwrote:
On Wed, 10 Sep 2008 13:57:47 +0200, "Andrea Taverna (Tavs)"
<a.****@libero.itwrote:

>主题:实现为指向指针的const矩阵的初始化

大家好。

我在源文件中有以下矩阵定义

static const char ** a;
>Subject: Initialization of a const matrix implemented as pointer-to-pointer

Hello everyone.

I''ve got the following matrix definition in a source file

static const char **a;



在这种情况下,a的大小可能是4或8.

In this case, sizeof a will probably be 4 or 8.


>
我需要将它初始化为一个字符串数组,例如

static const char ** a = {" Alpha"," Beta",Charlie"};
<我知道字符串的数量和它们的最大长度,但出于兼容性原因,我不能使用
堆栈分配的矩阵与另一段
代码,它只是定义了一个

char **字符串
>
I need it to be initialized as an array of strings, something like

static const char **a = {"Alpha", "Beta", Charlie"};

I know the number of strings and their maximum length, but I can''t use a
stack-allocated matrix for compatibility reasons with another piece of
code, which simply defines a

char** strings


>

矩阵必须分配给它。
>
to which the matrix has to be assigned to.



应该没有问题指定数组这个指针。

There should be no problem "assigning the array" to this pointer.


>
我以为我可以为每个字符串写一个静态const声明

static const str1 [ MAX_LENGTH] =" Alpha"

然后将矩阵声明为

static const char * a [NUM_OF_STRINGS] = {str1,str2,...}
>
I thought I could write a static const declaration for each string

static const str1[MAX_LENGTH] ="Alpha"

and then declare the matrix as

static const char* a[NUM_OF_STRINGS] = {str1,str2,...}



如果NUM_OF_STRINGS为3(与你的例子一致),

的大小可能是12或24.

If NUM_OF_STRINGS is 3 (to be consistent with your example), sizeof a
will probably be 12 or 24.


>
会不正确?
>
would it be correct?



这取决于你打算如何使用。


在第一个例子中,是一个真正的指针。它可以出现在赋值运算符的左侧

侧。在第二个中,a是一个无法在那里显示
的数组。但是,在这两种情况下,a都可以出现在赋值运算符的

右边。


如果你想要的只是一个可以下标的变量名评估

到不同的字符串,然后第二个将工作。如果你删除

" static",它将成为堆栈分配的矩阵。 (技术上是一个自动的char *数组
)。当数组名称a时,出现在

表达式上下文中,而不是sizeof和&的操作数,它将

转换为[0]的地址,类型为指针类型

a [0]"。由于a是char *的数组,因此[0]是

数组中的第一个char *。类型指向[0]的类型的指针。只是指向

char *的指针也被称为char **。因此,您可以指定数组名称

" a"变量字符串在其他代码中。


另一方面,如果你需要一个可以重新分配的真指针

指向不同的字符串集,那么形式:


const astr1 [] =" Alpha";

...

const astrx [] = Omega;

const bstr1 [] =" Alef";

...

const bstrx [] =" Tav" ;;

const char * A [] = {astr1,...,astrx};

const char * B [] = {bstr1,...,bstrx };

const char ** a = A;

...

a = B;

和任何地方在你的代码中你可以说

strings = a;


-

删除电子邮件的del

It depends on how you plan to use a.

In the first example, is a true pointer. It can appear on the left
side of the assignment operator. In the second, a is an array which
cannot appear there. However, in both cases, a can appear on the
right of the assignment operator.

If all you want is a variable name that can be subscripted to evaluate
to different strings, then the second will work. If you delete the
"static", it will become a "stack allocated matrix" (technically an
automatic array of char*). When the array name "a" appears in an
expression context other than as the operand of sizeof and &, it will
be converted to the address of a[0] with type "pointer to type of
a[0]". Since a is an array of char*, a[0] is the first char* in the
array. The type "pointer to type of a[0]" is simply "pointer to
char*" also known as char**. Therefore you can assign the array name
"a" to the variable "strings" in the other code.

On the other hand, if you need a true pointer that can be reassigned
to point to different sets of strings, then something of the form:

const astr1[] = "Alpha";
...
const astrx[] = "Omega";
const bstr1[] = "Alef";
...
const bstrx[] = "Tav";
const char* A[] = {astr1,...,astrx};
const char* B[] = {bstr1,...,bstrx};
const char **a = A;
...
a = B;
and anywhere in your code you can say
strings = a;

--
Remove del for email


Barry Schwarz ha scritto:
Barry Schwarz ha scritto:

在这种情况下,sizeof a可能是4或8.
In this case, sizeof a will probably be 4 or 8.

>我需要将它初始化为字符串数组,类似于

静态const char ** a = {" Alpha"," Beta",Charlie"};

我知道字符串的数量及其最大长度,但我不能使用
堆栈分配矩阵出于兼容性原因与另一段
代码,它只是定义了一个字符串
>I need it to be initialized as an array of strings, something like

static const char **a = {"Alpha", "Beta", Charlie"};

I know the number of strings and their maximum length, but I can''t use a
stack-allocated matrix for compatibility reasons with another piece of
code, which simply defines a

char** strings


>必须将矩阵分配给哪个。
> to which the matrix has to be assigned to.



应该没有问题分配数组到这个指针。


There should be no problem "assigning the array" to this pointer.



实际上有。海湾合作委员会警告他们不兼容

Actually there is. GCC warns that they''re not compatible


另一方面,如果你需要一个可以重新分配的真正指针

指向对于不同的字符串集,然后是以下形式:


const astr1 [] =" Alpha";

...

const astrx [] =" Omega";

const bstr1 [] =" Alef";

...

const bstrx [] =" Tav";

const char * A [] = {astr1,...,astrx};

const char * B [] = {bstr1,...,bstrx};

const char ** a = A;

...

a = B;

你的代码中的任何地方都可以说

strings = a;
On the other hand, if you need a true pointer that can be reassigned
to point to different sets of strings, then something of the form:

const astr1[] = "Alpha";
...
const astrx[] = "Omega";
const bstr1[] = "Alef";
...
const bstrx[] = "Tav";
const char* A[] = {astr1,...,astrx};
const char* B[] = {bstr1,...,bstrx};
const char **a = A;
...
a = B;
and anywhere in your code you can say
strings = a;



这正是我的意思我要去做。


谢谢


AT

that''s precisely what I''m going to do.

Thanks

AT


这篇关于初始化作为指针指针实现的const矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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