我有一个库,当库被编译为独立的二进制文件时,它有自己的命令行参数解析机制,这里是代码:
import math
# Input argument is the radius
circle_radius = 2.
# This is specified as 1, but doesn't have to be
tile_size = 1.
# Make a square box covering a quarter of the circle
tile_length_1d = int(math.ceil(circle_radius / tile_size ))
# How many tiles do you need?
num_complete_tiles = 0
num_partial_tiles = 0
# Now loop over all tile_length_1d x tile_length_1d tiles and check if they
# are needed
for i in range(tile_length_1d):
for j in range(tile_length_1d):
# Does corner of tile intersect circle?
intersect_len = ((i * tile_size)**2 + (j * tile_size)**2)**0.5
# Does *far* corner intersect (ie. is the whole tile in the circle)
far_intersect_len = (((i+1) * tile_size)**2 + ((j+1) * tile_size)**2)**0.5
if intersect_len > circle_radius:
# Don't need tile, continue
continue
elif far_intersect_len > circle_radius:
# Partial tile
num_partial_tiles += 1
else:
# Keep tile. Maybe you want to store this or something
# instead of just adding 1 to count?
num_complete_tiles += 1
# Multiple by 4 for symmetry
num_complete_tiles = num_complete_tiles * 4
num_partial_tiles = num_partial_tiles * 4
print "You need %d complete tiles" %(num_complete_tiles)
print "You need %d partial tiles" %(num_partial_tiles)
现在我将这个库静态链接到另一个具有自己的命令行解析机制和参数的库。我想在运行时使用一些参数初始化第一个库,例如我将这些参数传递给mimic命令行:
int main( int argc, char **argv )
{
int result;
argc = wool_init( argc, argv );
....
}
int wool_init( int argc, char **argv )
{
us_elapsed();
argc = decode_options( argc, argv );
.....
}
但我收到了以下错误。
/* initializing wool run-time environment */
char **woolArg;
*woolArg[0] = "-p 3";
wool_init(1, woolArg);
有人可以帮忙吗?
答案 0 :(得分:6)
让我们算上星星。
char **woolArg;
宣言中有两颗星。
woolArg[0]
这耗尽了一颗星。我们留下了char*
。
*woolArg[0]
这耗尽了另一颗星。我们留下了char
。
*woolArg[0] = "-p 3";
作业右侧有char*
。 char
和char*
是两个非常不同的东西。您可能想尝试
woolArg[0] = "-p 3";
...除了它不起作用,因为woolArg
未初始化,因此您无法触及woolArg[0]
。但是,不要试图解决这个问题,而只是陷入下一个问题,然后是下一个问题,然后学习简单正确的形式:
char* woolArg[] = { "library", "-p", "3", NULL };
第一个字符串是任意的。如果可以访问,可以在此使用argv[0]
。
答案 1 :(得分:2)
woolArg = malloc(sizeof(char *) * 3/*number of args*/);
woolArg[0] = /*program name*/;
woolArg[1] = "-p";
woolArg[2] = "3";
答案 2 :(得分:2)
让我们把内存管理问题放在一边,其他答案已经解决了这个问题。我们来解释一下错误信息。你的陈述是:
*woolArg[0] = "-p 3";
有哪些类型?
woolArg
是char **
*woolArg
是char *
*woolArg[0]
是char
"-p 3"
is a char[]
但是沦为char *
因此,作业的左侧需要char
,但右侧会提供char *
。因此,您隐式将指针转换为整数的警告/错误。
要解决此问题,您需要摆脱间接:
woolArg[0] = "-p 3";