小编Far*_*uti的帖子

合法的数组赋值.可能吗?

在阅读了关于K&R书中结构的章节后,我决定做一些测试来更好地理解它们,所以我写了这段代码:

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

struct test func(char *c);

struct test
{
    int i ;
    int j ;
    char x[20];
};

main(void)
{
    char c[20];
    struct  {int i ; int j ; char x[20];}  a = {5 , 7 , "someString"} , b; 
    c = func("Another string").x;
    printf("%s\n" , c);
}

struct test func(char *c)
{
    struct test temp;
    strcpy(temp.x , c);
    return temp;    
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:为什么c = func("Another string").x;工作(我知道这是非法的,但为什么它有效)?起初我用它写了strcpy()(因为这似乎是最合乎逻辑的事情)但我一直有这个错误:

structest.c: In function ‘main’:
structest.c:16:2: error: invalid …
Run Code Online (Sandbox Code Playgroud)

c arrays gcc c89 lvalue

7
推荐指数
1
解决办法
6031
查看次数

为什么迭代器不强制执行hasnext调用?

如果hasNext和Next像这样工作,这似乎是一个很好的方法:

boolean hasNextCalled = false;

boolean hasNext() {
  hasNextCalled  = true
}

next() {
  assert hasNextCalled
}
Run Code Online (Sandbox Code Playgroud)

这样我们就不会在我们得到NoSuchElementException()的情况下登陆.hasNext()调用没有强制执行的任何实际原因?

java iterator

0
推荐指数
1
解决办法
323
查看次数

写入1024字节时,文本文件变成二进制文件(linux)

我正在使用openmake函数,所以我尝试将1024字节写入文件,代码如下:

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

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

main(void)
{
        int fd;
        fd = open("hellow" , O_RDWR);
        write(fd , "hello" , 1024);
}
Run Code Online (Sandbox Code Playgroud)

当我linuxgcc它编译它编译时很好,但是当我尝试hellow在终端中打开时less,我得到了这个警告: "hellow" may be a binary file. See it anyway? 当我回答是的时候,我得到了我打算用随机符号整页编写的行.所以我的问题是,有没有人知道什么原因hellow被认为是一个二进制文件,为什么这些随机字符被写在预定的行之后.

c linux executable file

0
推荐指数
1
解决办法
570
查看次数

标签 统计

c ×2

arrays ×1

c89 ×1

executable ×1

file ×1

gcc ×1

iterator ×1

java ×1

linux ×1

lvalue ×1