繁体   English   中英

根据C中的索引构造字符串

[英]construct string from indices in C

当我构造这样的字符串时:

char string[1] = {'a'};
printf("%s", string)

它返回a4 为什么末尾有四个? 我该如何摆脱呢?

我之所以选择这种方法,是因为我需要从字符索引中创建一个字符串,例如char array[4] = {string[i],string[j],string[k]};

您的字符串应以终止字符'\\ 0'结尾。

char string[2] = {'a','\0'};

要么:

char string[] = "a";

C语言中的“字符串”本质上是以\\ 0字符结尾的字符数组(空终止)。
因此,如果您想要一个字符数组,那么您所做的就可以了,但是它不是一个“字符串”。 不要尝试这样打印。
如果您还想打印或将其视为“字符串”,则将其长度增加1,并在末尾添加一个'\\0'字符。

转换规范%s用于输出字符串,该字符串是一个以零个字符结尾的字符序列。

数组以这种方式声明

char string[1] = {'a'};

不包含字符串。

因此,要输出其元素,您需要指定要输出的确切字符数。 例如

printf("%*.*s", 1, 1, string);

否则,在数组中为终止的零保留一个以上的元素,并使用转换规范%s 例如

char string[2] = {'a'};
printf( "%s", string );

使用 ctypes 从 C 结构构造 numpy 数组。 值错误:' <p' is not a valid pep 3118 buffer format string< div><div id="text_translate"><p> 考虑以下代码:</p><p> by_ref.h</p><pre> typedef struct OutPutImage{ double *** output_img; int nb_images; int nb_cols; int nb_rows; }opi_; void test_output_images(struct OutPutImage * out, int nb_images, int nb_cols, int nb_rows);</pre><p> by_ref.c</p><pre> #include &lt;stdlib.h&gt; #include "by_ref.h" void test_output_images(struct OutPutImage* out, int nb_images, int nb_cols, int nb_rows){ out-&gt;nb_images = nb_images; out-&gt;nb_cols = nb_cols; out-&gt;nb_rows = nb_rows; out-&gt;output_img = (double***)malloc((nb_images) * sizeof(double**)); for(int i = 0; i &lt; nb_images; i++){ out-&gt;output_img[i] = (double**)malloc((nb_cols) * sizeof(double*)); for(int j = 0; j &lt; nb_cols; j++){ out-&gt;output_img[i][j] = (double*)malloc((nb_rows) * sizeof(double)); for(int k = 0; k &lt; nb_rows; k++){ out-&gt;output_img[i][j][k] = 0; } } } }</pre><p> 和</p><p> by_ref.py</p><pre> import ctypes import numpy.ctypeslib as npct import numpy as np class OutPutImage(ctypes.Structure): _fields_ = [('output_img', npct.ndpointer(dtype=np.double, ndim=3)), ('nb_images', ctypes.c_int), ('nb_cols', ctypes.c_int), ('nb_rows', ctypes.c_int)] _libc = ctypes.CDLL("./by_ref.so") def __init__(self, nb_images=None, nb_cols=None, nb_rows=None): self.nb_images = nb_images self.nb_cols = nb_cols self.nb_rows = nb_rows if __name__ == '__main__': libc_adm = ctypes.CDLL("./by_ref.so") libc_adm.test_output_images.restype = ctypes.c_int libc_adm.test_output_images.argtypes = [ctypes.POINTER(OutPutImage), ctypes.c_int, ctypes.c_int, ctypes.c_int] output_image = OutPutImage(1, 2, 3) libc_adm.test_output_images(ctypes.byref(output_image), 4, 5, 6) print(np.array(output_image.output_img, dtype=np.float)) # error ocures here</pre><p> 当我使用以下 Makefile 制作并运行此代码时</p><pre>by_ref: by_ref.so python by_ref.py by_ref.so: by_ref.o gcc -shared -o by_ref.so by_ref.o by_ref.o: by_ref.c gcc -c -Wall -fpic by_ref.c -o by_ref.o</pre><p> 我得到错误:</p><pre> Traceback (most recent call last): File "by_ref.py", line 46, in &lt;module&gt; print(np.array(output_image.output_img, dtype=np.float)) ValueError: '&lt;P' is not a valid PEP 3118 buffer format string make: *** [Makefile:2: by_ref] Error 1</pre><p> 我确信test_output_images正在做它应该做的事情。 但是我无法从结构中的数据构建 numpy 数组。 我该如何做到这一点? 另外,我什么时候释放 memory? 谢谢。</p><p> <strong>编辑:</strong></p><p> 如果我使用np.ctypeslib.as_array(output_image.output_img)我得到同样的错误:</p><pre> ValueError: '&lt;P' is not a valid PEP 3118 buffer format string</pre><p> <strong>更新:</strong></p><p> 如果我使用x = np.ctypeslib.as_array(( ctypes.c_double*array_length ).from_address( ctypes.addressof(output_image.output_img) )) ,其中array_length=nb_images*nb_cols*nb_rows ,那么我避免了上述错误,但是新数组 x 包含垃圾,无法重塑。</p></div></p'>

[英]Construct a numpy array from a C structure using ctypes. ValueError: '<P' is not a valid PEP 3118 buffer format string

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用C构造String 如何从C程序为Unix system()调用构造字符串 C ++从指针构造 从地址在C中构造一个指针 使用C预处理器为scanf构造字符串文字? 从 C 中的二进制“组件”构造一个 int 根据CSV数据构造一个C数组 使用 ctypes 从 C 结构构造 numpy 数组。 值错误:' <p' is not a valid pep 3118 buffer format string< div><div id="text_translate"><p> 考虑以下代码:</p><p> by_ref.h</p><pre> typedef struct OutPutImage{ double *** output_img; int nb_images; int nb_cols; int nb_rows; }opi_; void test_output_images(struct OutPutImage * out, int nb_images, int nb_cols, int nb_rows);</pre><p> by_ref.c</p><pre> #include &lt;stdlib.h&gt; #include "by_ref.h" void test_output_images(struct OutPutImage* out, int nb_images, int nb_cols, int nb_rows){ out-&gt;nb_images = nb_images; out-&gt;nb_cols = nb_cols; out-&gt;nb_rows = nb_rows; out-&gt;output_img = (double***)malloc((nb_images) * sizeof(double**)); for(int i = 0; i &lt; nb_images; i++){ out-&gt;output_img[i] = (double**)malloc((nb_cols) * sizeof(double*)); for(int j = 0; j &lt; nb_cols; j++){ out-&gt;output_img[i][j] = (double*)malloc((nb_rows) * sizeof(double)); for(int k = 0; k &lt; nb_rows; k++){ out-&gt;output_img[i][j][k] = 0; } } } }</pre><p> 和</p><p> by_ref.py</p><pre> import ctypes import numpy.ctypeslib as npct import numpy as np class OutPutImage(ctypes.Structure): _fields_ = [('output_img', npct.ndpointer(dtype=np.double, ndim=3)), ('nb_images', ctypes.c_int), ('nb_cols', ctypes.c_int), ('nb_rows', ctypes.c_int)] _libc = ctypes.CDLL("./by_ref.so") def __init__(self, nb_images=None, nb_cols=None, nb_rows=None): self.nb_images = nb_images self.nb_cols = nb_cols self.nb_rows = nb_rows if __name__ == '__main__': libc_adm = ctypes.CDLL("./by_ref.so") libc_adm.test_output_images.restype = ctypes.c_int libc_adm.test_output_images.argtypes = [ctypes.POINTER(OutPutImage), ctypes.c_int, ctypes.c_int, ctypes.c_int] output_image = OutPutImage(1, 2, 3) libc_adm.test_output_images(ctypes.byref(output_image), 4, 5, 6) print(np.array(output_image.output_img, dtype=np.float)) # error ocures here</pre><p> 当我使用以下 Makefile 制作并运行此代码时</p><pre>by_ref: by_ref.so python by_ref.py by_ref.so: by_ref.o gcc -shared -o by_ref.so by_ref.o by_ref.o: by_ref.c gcc -c -Wall -fpic by_ref.c -o by_ref.o</pre><p> 我得到错误:</p><pre> Traceback (most recent call last): File "by_ref.py", line 46, in &lt;module&gt; print(np.array(output_image.output_img, dtype=np.float)) ValueError: '&lt;P' is not a valid PEP 3118 buffer format string make: *** [Makefile:2: by_ref] Error 1</pre><p> 我确信test_output_images正在做它应该做的事情。 但是我无法从结构中的数据构建 numpy 数组。 我该如何做到这一点? 另外,我什么时候释放 memory? 谢谢。</p><p> <strong>编辑:</strong></p><p> 如果我使用np.ctypeslib.as_array(output_image.output_img)我得到同样的错误:</p><pre> ValueError: '&lt;P' is not a valid PEP 3118 buffer format string</pre><p> <strong>更新:</strong></p><p> 如果我使用x = np.ctypeslib.as_array(( ctypes.c_double*array_length ).from_address( ctypes.addressof(output_image.output_img) )) ,其中array_length=nb_images*nb_cols*nb_rows ,那么我避免了上述错误,但是新数组 x 包含垃圾,无法重塑。</p></div></p'> 使用C宏构造字符串/字符转义序列 如何使用变量或重复的占位符在C中构造格式字符串?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM