小编Dar*_*gor的帖子

C中的函数指针,数组和左值

假设我们有以下函数(在C中):

int sum(int a, int b){  
   return a+b;
}
int diff(int a, int b){
    return a-b;
}
Run Code Online (Sandbox Code Playgroud)

所以我们知道我们可以通过以下方式声明一个函数指针数组:

int (*test[2]) (int a, int b);
test[0] = sum;
test[1] = diff;
Run Code Online (Sandbox Code Playgroud)

但以下也有效(但我们使用堆分配):

int (**test) (int a, int b) = malloc( 2*sizeof(*test));
test[0] = sum;
test[1] = diff;
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.现在让我们记住,我们可以做一个(动态分配的)两个整数的数组:

 int* test  = malloc( 2*sizeof(int));
Run Code Online (Sandbox Code Playgroud)

那么为什么我们不能将一个函数指针数组声明为

int (*test) (int a, int b) = malloc( 2*sizeof(*test)); ?
Run Code Online (Sandbox Code Playgroud)

是因为测试与*test**test(等等)相同,malloc( 2*sizeof(*test))是返回指向函数指针的指针,因此无法分配给它(*test)

如果这个假设是正确的,你能详细解释为什么我们得到编译错误

error: lvalue required as …
Run Code Online (Sandbox Code Playgroud)

c arrays pointers

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

将大量文件传递给stdin,提取第一列,然后将它们组合到一个新文件中

假设我们有这两个文件:

$ cat ABC.txt 
ABC DEF

$ cat PQR.txt 
PQR XTZ
Run Code Online (Sandbox Code Playgroud)

我们希望用每个文件的第1列形成一个文件.这可以通过以下方式实现:

$ paste -d ' ' <(cut -d ' ' -f 1 ABC.txt) <(cut -d ' ' -f 1 PQR.txt )
ABC PQR
Run Code Online (Sandbox Code Playgroud)

但是我想在输入中使用大量的文件,不仅是ABC.txt和PQR.TXT,还有很多.我们如何概括这种情况来传递集合中的每个文件以剪切然后将所有输出传递给粘贴(我知道这可以用awk做得更好,但我想知道如何使用这种方法解决这个问题).


编辑1

我发现了一种肮脏,肮脏的方式:

$ str='';  for i in *.txt; \
           do    str="${str} <(cut -d ' ' -f 1 ${i})"; \
           done ; \
  str="paste -d ' ' $str"; \
  eval $str
Run Code Online (Sandbox Code Playgroud)

但请,让我的灵魂释放一个不涉及计算机科学地狱的答案.

编辑2

如果这很重要,每个文件可以有n行.

bash shell stdin cut paste

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

Python类和全局与局部变量

我有一个问题,了解以下代码片段的结果发生了什么:

my_str = "outside func"
def func():
    my_str = "inside func"
    class C():
        print(my_str)
        print((lambda:my_str)())
        my_str = "inside C"
        print(my_str)
Run Code Online (Sandbox Code Playgroud)

输出是:

outside func
inside func
inside C
Run Code Online (Sandbox Code Playgroud)

另一段代码是:

my_str = "not in class"
class C:
    my_str = "in the class"
    print([my_str for i in (1,2)])
    print(list(my_str for i in (1,2)))
Run Code Online (Sandbox Code Playgroud)

输出是:

[‘in the class’, 'in the class’]
['not in class’, 'not in class’]
Run Code Online (Sandbox Code Playgroud)

问题是:

  1. 每个print()语句中发生了什么?
  2. 任何人都可以解释为什么 print()语句从不同的命名空间中获取字符串?

编辑1:

我认为这与这个问题不同,因为我谦卑地认为那里的答案并不能解释这种变化:

my_str = "outside func"
def func():
    my_str = …
Run Code Online (Sandbox Code Playgroud)

python namespaces class global-variables

6
推荐指数
1
解决办法
511
查看次数

在python中处理Telegram bot的多个问题

我正在使用Telegram bot API在Python中编写电报机器人.我面临着管理需要用户回答的问题的问题.当程序第一个用户响应之前等待一个用户的答案和另一个用户请求信息或询问另一个问题时,会出现问题.

Telegram API使用代码来处理请求.当您要求更新时,请包含代码.如果您发送的代码高于请求代码,则将其标记为已处理并且电报将其删除并且不再出现在更新中.此代码是顺序的,因此如果将update 3标记为已处理,则更新1和2也将被删除.

问题是为什么最好的phytonic /优雅方式来处理需要用户答案的​​多个请求?

python bots telegram python-telegram-bot

5
推荐指数
2
解决办法
7281
查看次数

奇怪的内联分配

我在Python(2和3)中遇到这种奇怪的行为:

>>> a = [1, 2]
>>> a[a.index(1)], a[a.index(2)] = 2, 1
Run Code Online (Sandbox Code Playgroud)

这导致:

>>> a
[1, 2]
Run Code Online (Sandbox Code Playgroud)

但如果你写

>>> a = [1, 2]
>>> a[a.index(1)], a[a.index(2)] = x, y
Run Code Online (Sandbox Code Playgroud)

其中x, y != 2, 1(可以是1, 1,2, 2,3, 5等),这将导致:

>>> a == [x, y]
True
Run Code Online (Sandbox Code Playgroud)

正如人们所料.为什么不a[a.index(1)], a[a.index(2)] = 2, 1产生结果a == [2, 1]

>>> a == [2, 1]
False
Run Code Online (Sandbox Code Playgroud)

python tuples variable-assignment

5
推荐指数
1
解决办法
256
查看次数

asyncio 的事件循环如何知道可等待资源何时准备就绪?

我正在学习Python asyncio 用于异步编程。我知道事件循环会监视 Future 对象,直到它们准备好,然后恢复适当的协程以在出现 wait 关键字的点继续执行。

当您使用类似的东西时,这是非常可以理解的asyncio.sleep,因为睡眠函数知道它需要多少时间,因此会知道事件循环,但是对于依赖于网络(例如)等待时间未知的东西会发生什么?

事件循环如何知道资源何时准备就绪,或者从某个源收集数据需要多少时间?

python asynchronous python-asyncio

4
推荐指数
1
解决办法
1132
查看次数