这是有效的计划吗? [英] Is this valid program

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

问题描述

#include< stdio.h>

#include< string.h>

#include< stdlib.h>

int main()

{

typedef union

{

int i;

char * s;

} union_t;

typedef struct

{

union_t union1;

int j;

} struct_t;

struct_t struct1;

struct1.union1.s = malloc(5);

strcpy(struct1.union1.s," ASHW");

struct1.union1.s [5] =''\''';

printf("%s \ n",struct1.union1.s);

返回0;

}


我得到了上述程序的预期输出(即ASHW)。


但是当使用测试工具测试类似的功能时

称为RTRT,我收到以下错误。

"赋值中不兼容的类型

上面的错误是针对在union中声明的

指针分配内存的语句。


上述程序是使用GCC在Redhat Linux上编译的。工具

(用于测试函数)也使用相同的编译器。


问题是,我们可以为在a内声明的指针分配内存吗?

联盟?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
typedef union
{
int i;
char *s;
}union_t;
typedef struct
{
union_t union1;
int j;
}struct_t;
struct_t struct1;
struct1.union1.s = malloc(5);
strcpy(struct1.union1.s,"ASHW");
struct1.union1.s[5]=''\0'';
printf("%s\n",struct1.union1.s);
return 0;
}

I am getting the expected output(i.e. ASHW) for the above program.

However when a similar kind of function is tested using a testing tool
called RTRT, I am getting the following error.
" incompatible types in assignment "
The above error is for the statement where memory is allocated for a
pointer declared inside union.

The above program was compiled on Redhat Linux using GCC. The tool
(used for tesing the function) also is using the same compiler.

The question is, Can we allocate memory for a pointer declared inside a
union?

推荐答案

malloc的返回类型是(void *)

这就是为什么你的测试工具可能会出错。

u可以显式地转换它(char *)malloc(..)然后结账。

将内存分配给union

的指针是没有错的,因为只有指针的值发生了变化,

union的大小才会相同。 />
the return type of malloc is (void *)
that''s why ur testing tool may be giving an error.
u can explicitly cast it (char *) malloc(..) and then check out.
there is nothing wrong in assigning memory to a pointer of union
because only the value of the pointer is getting changed, the size of
union will be same.


在正在测试的函数中,完成显式类型转换。在

示例程序中我没有显示它。

In the function that is under test, explicit type casting is done. In
the example program I have not shown it.


va ****** @ rediffmail.com 写道:
#include< stdio.h>
#include< string。 h>
#include< stdlib.h>

int main()


int main(无效)

{
typedef union
{i;
char * s;
} union_t;

typedef struct
{
union_t union1;
int j;
} struct_t;

struct_t struct1;

struct1.union1.s = malloc(5) ;


你想在这里检查malloc的返回值。

strcpy(struct1.union1.s," ASHW");
struct1 .union1.s [5] = '' \0 '';


未定义的行为。你写给你不拥有的记忆,并且完全不必要地写了
所以 - strcpy已经附加了

终止''\ 0''。

printf("%s \ n",struct1.union1.s);
返回0;
}

我得到预期的输出(即ASHW )对于上述计划。


运气不好。

但是当使用名为RTRT的测试工具测试类似的功能时,我收到以下错误。
赋值中不兼容的类型
以上错误是针对在union中声明的指针分配内存的语句。

上述程序是在Redhat Linux上使用GCC编译的。工具
(用于测试函数)也使用相同的编译器。

问题是,我们可以为在
联合内声明的指针分配内存吗?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
int main(void)
{
typedef union
{
int i;
char *s;
}union_t;
typedef struct
{
union_t union1;
int j;
}struct_t;
struct_t struct1;
struct1.union1.s = malloc(5);
You want to check the return value of malloc here.
strcpy(struct1.union1.s,"ASHW");
struct1.union1.s[5]=''\0'';
Undefined behaviour. You write to memory you don''t own, and
completely unnecessarily so - strcpy already appended a
terminating ''\0''.
printf("%s\n",struct1.union1.s);
return 0;
}

I am getting the expected output(i.e. ASHW) for the above program.
Just bad luck.
However when a similar kind of function is tested using a testing tool
called RTRT, I am getting the following error.
" incompatible types in assignment "
The above error is for the statement where memory is allocated for a
pointer declared inside union.

The above program was compiled on Redhat Linux using GCC. The tool
(used for tesing the function) also is using the same compiler.

The question is, Can we allocate memory for a pointer declared inside a
union?




当然。你的RTRT工具坏了,丢掉它。


祝你好运。

-

Irrwahn Grausewitz(ir **** ***@freenet.de)

欢迎来到clc: http://www.ungerhu.com/jxh/clc.welcome.txt

clc faq-list: http://www.faqs.org/faqs/C-faq/faq/

clc常见答案: http://benpfaff.org/writings/clc



Of course. Your RTRT tool is broken, ditch it.

Best regards.
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc


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

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