在阅读了关于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) 如果hasNext和Next像这样工作,这似乎是一个很好的方法:
boolean hasNextCalled = false;
boolean hasNext() {
hasNextCalled = true
}
next() {
assert hasNextCalled
}
Run Code Online (Sandbox Code Playgroud)
这样我们就不会在我们得到NoSuchElementException()的情况下登陆.hasNext()调用没有强制执行的任何实际原因?
我正在使用open和make函数,所以我尝试将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)
当我linux用gcc它编译它编译时很好,但是当我尝试hellow在终端中打开时less,我得到了这个警告:
"hellow" may be a binary file. See it anyway?
当我回答是的时候,我得到了我打算用随机符号整页编写的行.所以我的问题是,有没有人知道什么原因hellow被认为是一个二进制文件,为什么这些随机字符被写在预定的行之后.