簡體   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