使用%p转换说明符打印空指针是不确定的行为?
#include <stdio.h>
int main(void) {
void *p = NULL;
printf("%p", p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个问题适用于C标准,而不适用于C实现.
我想我在过去的20个小时内尽我所能,但似乎没有任何效果.我的应用程序正在运行和工作 - 就像它应该 - 我唯一的问题是我无法.app从它创建一个包.我都尝试Py2App和cx_Freeze,但他们的非正常工作.由于多平台支持,我会坚持使用后者 - 如果可能的话.
该setup.py如下所示:
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
OPTIONS = {'build_exe': {'includes': ['sip',
'PyQt5',
'PyQt5.QtCore',
'PyQt5.QtGui',
'PyQt5.QtWidgets',
'PyQt5.QtMultimediaWidgets',
'PyQt5.QtMultimedia',
'PyQt5.QtNetwork']}}
EXECUTABLES = [Executable('main.py', base=base)]
NAME = 'coublet'
VERSION = '0.5.70'
setup(name = NAME,
version = VERSION,
options = OPTIONS,
executables = EXECUTABLES)
Run Code Online (Sandbox Code Playgroud)
我有的错误信息是这样的:
objc[28404]: Class NotificationReceiver is implemented in both
/Users/.../build/coublet-0.5.70.app/Contents/MacOS/QtWidgets and
/usr/local/Cellar/qt5/5.3.1/lib/QtWidgets.framework/Versions/5/QtWidgets. One of …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个关于如何存储在对象内部的对象上调用的方法的pythonic解决方案.
因为在python中,如果我想捕获例如abs()方法,我将重载此运算符,如:
Catcher(object):
def __abs__(self):
self.function = abs
c = Catcher()
abs(c) # Now c.function stores 'abs' as it was called on c
Run Code Online (Sandbox Code Playgroud)
例如,如果我想要捕获一个具有其他属性的函数,pow()我将使用它:
Catcher(object):
def __pow__(self, value):
self.function = pow
self.value = value
c = Catcher()
c ** 2 # Now c.function stores 'pow', and c.value stores '2'
Run Code Online (Sandbox Code Playgroud)
现在,我正在寻找的是一个通用的解决方案,捕获和存储任何类型的函数Catcher,而不实现所有重载,以及其他情况.正如您所看到的,我还想存储值(可能在列表中,如果有多个值?),这些是方法的属性.
提前致谢!
假设我在 中定义了以下功能Cargo.toml:
[features]
my_feature = []
Run Code Online (Sandbox Code Playgroud)
以下代码位于src/lib.rs:
[features]
my_feature = []
Run Code Online (Sandbox Code Playgroud)
我如何强制功能字符串与显式定义和隐式可用功能的列表相匹配,以便Cargo.toml避免例如拼写错误?
我正在尝试使用字典在Python中创建一个简单的计算器.这是我的代码:
def default():
print "Incorrect input!"
def add(a, b):
print a+b
def sub(a, b):
print a-b
def mult(a, b):
print a*b
def div(a, b):
print a/b
line = raw_input("Input: ")
parts = line.split(" ")
part1 = float(parts[0])
op = parts[1];
part3 = float(parts[2])
dict = {
'+': add(part1, part3),
'-': sub(part1, part3),
'*': mult(part1, part3),
'/': div(part1, part3)
}
try:
dict[op]
except KeyError:
default()
Run Code Online (Sandbox Code Playgroud)
但所有功能都被激活了.有什么问题?
以下代码生成2个警告,这些警告在问题标题中描述.
#include <stdio.h>
static void _print_f(float *f){printf("float : %f\n", *f);}
static void _print_i(int *i) {printf("int : %d\n", *i);}
#define print(num) _Generic((num), \
int* : _print_i(num), \
float* : _print_f(num))
int main(void)
{
print((&(int){10}));
print((&(float){10.f}));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
int : 10
float : 10.000000
Run Code Online (Sandbox Code Playgroud)
我知道,这个宏可以写成如下:
#define print(num) _Generic((num), \
int* : _print_i, \
float* : _print_f)(num)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,不会有任何警告,但我的例子是我写的一个虚拟片段来证明问题.在我的真实代码库中,我选择了以前的解决方案,因为其他一些"默认"但类型特定的参数需要传递给选定的函数.
所以问题是:即使宏正在按预期工作,输出正是我所期望的,为什么会产生警告?
旗帜与环境:
/* Mac OS X 10.9.4
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) */
cc -Wall -v -g -std=c11 …Run Code Online (Sandbox Code Playgroud) 如何按照我设置的原始顺序打印出我的字典?
如果我有这样的字典:
smallestCars = {'Civic96': 12.5, 'Camry98':13.2, 'Sentra98': 13.8}
Run Code Online (Sandbox Code Playgroud)
我这样做:
for cars in smallestCars:
print cars
Run Code Online (Sandbox Code Playgroud)
它输出:
Sentra98
Civic96
Camry98
Run Code Online (Sandbox Code Playgroud)
但我想要的是这个:
Civic96
Camry98
Sentra98
Run Code Online (Sandbox Code Playgroud)
有没有办法按顺序打印原始字典而不将其转换为列表?
这是我死的简单虚拟代码:
#include <errno.h>
int main(void)
{
errno_t e;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这令人惊讶地引发了这个错误:
main.c:5:5: error: use of undeclared identifier 'errno_t'
errno_t x;
^
Run Code Online (Sandbox Code Playgroud)
我开始遵循这些跟踪:当编译器看到<...>包含时,它将首先查看/usr/include我找到errno.h文件的位置.实际上它除了许可评论之外还有一行,它是:
#include <sys/errno.h>
Run Code Online (Sandbox Code Playgroud)
现在,/usr/include/sys在errno.h我发现以下几行:
#include <sys/cdefs.h>
#if defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ >= 1
#include <sys/_types/_errno_t.h>
#endif
Run Code Online (Sandbox Code Playgroud)
而在/usr/include/_types中_errno_t.h,我发现这一点:
typedef int errno_t;
Run Code Online (Sandbox Code Playgroud)
所以看起来,它就在那里,它是整数类型的别名,也是它的一部分errno.h- 就像它应该的那样.
那为什么不包括在内呢?为什么编译器会引发未声明的标识符错误?
提前致谢!
相关信息:
Compiler:
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)`
Compiler flags:
-std=c11 -I/usr/include/sys -I/usr/local/include
Run Code Online (Sandbox Code Playgroud)
宏变量 …
我正在针对我的itoa()函数运行一些测试用例但是继续进行
did not allocate memory for the int min value
Run Code Online (Sandbox Code Playgroud)
我正在检查,但这是我在这里缺少的东西,它是什么?
char *ft_itoa(int x) {
char *s;
size_t len;
long int n;
n = x;
if (x == -2147483648)
return (ft_strdup("-2147483648"));
len = ft_intlen(n) + 1;
if (!(s = (char*)malloc(sizeof(char) * len)))
return (NULL);
if (n == 0)
s[0] = '0';
if (n < 0) {
s[0] = '-';
n = -n;
}
s[len - 1] = '\0';
while (n) {
len--;
s[len - 1] = (n % 10) + …Run Code Online (Sandbox Code Playgroud) 我创建了一个轻量级图形库,它有3个对象(Vertex,Edge,Graph)和1个函数(topo_sort),它们看起来像:
class DAGError(Exception): pass
def topo_sort(graph):
sorted_list = []
def visit(vertex):
nonlocal sorted_list
if vertex.idle:
raise DAGError('Graph has at least one cycle.')
if not vertex.done:
vertex.idle = True
for neighbor in vertex.vertices():
visit(neighbor)
vertex.done = True
vertex.idle = False
sorted_list.insert(0, vertex)
queue = [vertex for vertex in graph.vertices() if not vertex.done]
while queue:
visit(queue.pop(0))
return iter(sorted_list)
Run Code Online (Sandbox Code Playgroud)
如果我有一个平坦的DAG,这工作正常.但我想要实现的是将子图(或嵌套图)添加到我的主图中,正如您在此图中所示我绘制的:
嵌套/子图插图http://f.cl.ly/items/2o0s1c2W1o2C0i0g0l0r/subgraph_illustration3.png
这仍然是一个DAG,所以如果我对此运行我的函数,正常 topo_sort输出将是这样的:
V0, V3, V1, V5, V4, V8, V7, V12, V11, V13, V14, V2, V6, V10, V9, V15, …Run Code Online (Sandbox Code Playgroud)