![](/img/trans.png)
[英]How to get the digits of a number without converting it to a string/ char array?
[英]How to get the number of columns of a char array?
有沒有一種方法可以獲取char array
的列數?
聲明如下: static const char *countries[][2] = { ... };
但是我想動態獲取它,所以自己不要輸入“ 2”
不,您必須將大小和數組一起傳遞。 可能存在依賴於系統/編譯器/庫的方式,但是依靠此類功能並不是一個好主意。 如果您使用的是C ++標准庫,則最好使用std::vector
,它攜帶更多的元數據,例如您感興趣的size()
。
當然可以。 可以使用sizeof
運算符輕松檢查靜態初始化的數組。 以下代碼段(用C表示)將打印與2D指針數組關聯的所有大小。
#include <stdio.h>
static const char *countries[][2] = { { "dfdf", "dss" }, { "ss"}, { "toto"}};
int main(void) {
printf("dim0 %zu, dim1 %zu, elements %zu\n",
sizeof countries/sizeof countries[0],
sizeof countries[0]/sizeof countries[0][0],
sizeof countries/sizeof countries[0][0]
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.