这有效吗? [英] Is this valid?

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

问题描述

以下代码是否有效?


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


typedef struct

{

int a;

char s [];

} test_struct;


int main(无效)

{

test_struct * t;

t = malloc(sizeof * t + 4);

strcpy(t-> s," oye");

printf("%s \ n",t-> s);

返回0;

}

Is the following code valid?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
int a;
char s[];
} test_struct;

int main(void)
{
test_struct *t;
t = malloc( sizeof *t + 4);
strcpy(t->s,"oye");
printf("%s\n",t->s);
return 0;
}

推荐答案

对于C90:否。

For C90: no.


2005年6月10日星期五12:34:08 - 0400,AC写道:
On Fri, 10 Jun 2005 12:34:08 -0400, AC wrote:
以下代码是否有效?

#include< stdio.h>
#include< stdlib.h> ;
#include< string.h>

typedef struct
{
int a;
char s [];
} test_struct ;

int main(void)
{test_struct * t;
t = malloc(sizeof * t + 4);
strcpy(t-> s," oye");
printf("%s \ nn",t-> s);
返回0 ;
}
Is the following code valid?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
int a;
char s[];
} test_struct;

int main(void)
{
test_struct *t;
t = malloc( sizeof *t + 4);
strcpy(t->s,"oye");
printf("%s\n",t->s);
return 0;
}




我没有看到任何问题。灵活的阵列功能在C99之前不是标准化的,尽管许多实现在此之前以某种形式支持它(但这当然不是便携式的)。 br />

Robert Gamble



I don''t see any problems with it. The flexible array feature is not
standardized until C99 although many implementations supported it in some
form prior to that (but that would, of course, not be portable).

Robert Gamble


文章< d8 ********** @ license1.unx.sas。 com>,AC< te ** @ test.test>写道:
In article <d8**********@license1.unx.sas.com>, AC <te**@test.test> wrote:
以下代码是否有效?

typedef struct
{
int a;
char s [];
} test_struct;
Is the following code valid?

typedef struct
{
int a;
char s[];
} test_struct;




如果你将s的声明更改为char s [1],

技巧将更频繁地工作,即几乎总是。


虽然技术上超出了c89的范围。


搜索struct hack使用您最喜欢的搜索引擎

如果您想了解更多信息。

-

7842 ++



If you change the declaration of s to char s[1] the
trick will work more often, i.e. nearly always.

It is technically outside the bounds of c89 though.

Search for "struct hack" using your favorite search engine
if you would like more information.
--
7842++


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

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