是否可以在C中创建匿名的ad-hoc数组?
例如,假设我有一个调用的函数processArray(int[] array)
,它接受一个int数组作为参数,我可以通过以下方式传递一个匿名数组:
int main(){
processArray( (int[]){0, 1, 2, 3} ); //can I create this type of array?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
或者我是否必须先声明数组(使用指针),然后将其指针传递给processArray()?例如:
int main(){
int[] myArray = {0, 1, 2, 3};
processArray(myArray);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我一直在查看文档SocketServer
.我从文档中复制了TCP服务器代码,运行正常.但是,我注意到每当我在终端中按程序ctrl-c'ed,然后尝试再次运行它时,我会收到以下错误:
socket.error: [Errno 98] Address already in use
Run Code Online (Sandbox Code Playgroud)
我通过阅读本文和本文来研究如何解决问题.我在代码中添加了以下行以尝试允许重用地址:
server.allow_reuse_address = True
Run Code Online (Sandbox Code Playgroud)
即使添加了上面的行,我仍然遇到同样的问题.我还添加了一个try
和except
我的server.serve_forever()
函数,捕获KeyboardInterrupt异常并调用,server.shutdown()
并server.socket.close()
希望释放该地址.
这是我的TCP服务器代码的完整范围(注意:我排除了MyTCPHandler类):
if __name__ == "__main__":
HOST, PORT = '', 9999
# Create the server, binding to localhost on port 9999
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
server.allow_reuse_address = True
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
try:
server.serve_forever()
except KeyboardInterrupt:
server.shutdown()
server.socket.close()
Run Code Online (Sandbox Code Playgroud)
我仍然在运行上面的代码时出错,必须等待一分钟,直到地址最终被释放.当我不断调试和更改代码时,这很令人沮丧. …
我正在使用Python Parsimonious Parser尝试为我正在设计的一种简单语言构建解释器。我观看了本教程视频,该视频非常有帮助,现在我正在慢慢修改代码以符合我自己的规则。我受困于最初定义为的分配规则:
def assignment(self, node, children):
'assignment = lvalue "=" expr'
lvalue, _, expr = children
self.env[lvalue] = expr
return expr
Run Code Online (Sandbox Code Playgroud)
我使用以下语法对规则进行了少许修改:
def assignment(self, node, children):
'assignment = "SET" lvalue "," expr'
_, lvalue, _, expr = children
self.env[lvalue] = expr
return expr
Run Code Online (Sandbox Code Playgroud)
我希望解析器进行评估SET a, 7
,例如,与解析器相同,a = 7
并将值绑定7
到name a
。但是,当我尝试解析它时,我从Parsimonious库中得到了以下错误:
parsimonious.exceptions.IncompleteParseError: Rule 'program' matched in its
entirety, but it didn't consume all the text. The non-matching portion …
Run Code Online (Sandbox Code Playgroud) 我正在为我的 rails 应用程序使用 docker-compose。
我最近更新了我的 master 分支,将 rails 版本更新为 5.2.3 —— 我通过 docker-compose 运行了 bundle install:
docker-compose run web bundle install
看起来它运行良好,但是当我尝试运行 rspec 时出现此错误:
Could not find activesupport-5.2.3 in any of the sources
Run `bundle install` to install missing gems.
Run Code Online (Sandbox Code Playgroud)
我尝试运行bundle update activesupport
- 并得到这个:
Bundler attempted to update activesupport but its version stayed the same
Bundle updated!
Run Code Online (Sandbox Code Playgroud)
所以我尝试手动安装 gem:
docker-compose run web gem install activesupport
Fetching activesupport-5.2.3.gem
Successfully installed activesupport-5.2.3
1 gem installed
Run Code Online (Sandbox Code Playgroud)
然后我尝试再次运行 rspec,同样的事情:
$ docker-compose run …
Run Code Online (Sandbox Code Playgroud) 我不知道如何解析 getline() 中的行。我想看看该行中的每个字符。
因此,如果有人在标准输入中输入:“Hello”,我希望能够以这种方式访问 char 数组:
line[0] = 'H'
line[1] = 'e'
line[2] = 'l'
line[3] = 'l'
line[4] = 'o'
line[5] = '/0';
Run Code Online (Sandbox Code Playgroud)
我看过 getchar(),但我想尝试使用 getline(),因为我觉得它更方便。我也研究过 scanf(),但它会跳过空格,并且不能让我像 getchar() 或 getline() 那样很好地解析输入。
下面是尝试通过 stdin 获取行的第一个字符的简单代码,但会导致段错误:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int len;
int nbytes = 100;
char *line = (char *) malloc (nbytes + 1);
while(getline(&line, &nbytes, stdin) > 0){
printf("first char: %s", line[0]); //try and get the first char from input
/**
* other code that would …
Run Code Online (Sandbox Code Playgroud) c ×2
python ×2
arrays ×1
bundler ×1
docker ×1
getline ×1
parsimonious ×1
parsing ×1
peg ×1
python-2.7 ×1
rspec ×1
socketserver ×1