如何在不使用 for 循环的情况下获取 char * 变量的大小?
How can i get the size of my char * variable without using a for loop?
char *foo(char *dest, const char *src) {
size_t i;
for (i = 0; dest[i] != '[=10=]'; i++);
在这里,我正在迭代以获得目标的大小。
在这种情况下,我将 "hello " 输入到 dest,其大小为 6。当我尝试使用 sizeof(dest) 时,我得到 4 作为返回值。我希望能够在不使用此 for 循环的情况下获取 dest 内部内容的大小。
char *foo(char *dest, const char *src) {
while (*dest != '[=11=]') dest++; /* increment the length of dest's pointer*/
编辑::
我想花点时间证明我能够直接找到长度。
这都是 strcat 程序的一部分。要求是 不 使用 [ ] 括号访问或在内存中移动。
char *strcat(char *dest, const char *src) {
while (*dest != '[=12=]') dest++; /* increment the length of dest's pointer*/
while (*src != '[=12=]') /* we will be incrementing up through src*/
*dest++ = *src++; /* while this is happening we are appending
* letter by letter onto the variable dest
*/
*(dest++) = ' '; /* increment up one in memory and add a space */
*(dest++) = '[=12=]'; /* increment up one in memory and add a null
* termination at the end of our variable dest
*/
return dest; /* return the final output */
}
您正在寻找 strlen()
。但请注意,它可能是用相同的循环实现的。
对于以空字符结尾的字符串,您必须遍历每个字符才能计算出长度。即使您使用 strlen() ,它也会执行您的循环所做的事情。
因为你的函数中dest
的类型是char const*
,sizeof(dest)
和sizeof(char const*)
是一样的,也就是指针的大小。当您使用 sizeof(dest)
时得到 4 的事实表明您平台中指针的 sizeof
是 4.
获取字符串长度的唯一方法是对字符进行计数,直到遇到空字符。这很可能也是 strlen
所做的。
在 C 中,字符串存储为以 [=11=]
结尾的字符数组。
如果你想得到一个数组中的字符数,你必须遍历这个数组,你无法绕过它。
但是,您可以将大小存储在结构中,
typedef struct
{
int size;
char *data;
}String;
然后,您必须创建包装函数来写入此 String
、读取此 String
并更新数据。
如果您有很多大小的读取而没有很多更新或写入(或常量的零写入),这将很有用。
但一般来说,for 循环是更好的解决方案。
是啊!!你可以在不使用 for 循环的情况下获得大小。介绍图书馆和
strlen(dest)+1 将是您 array.cout<< dest;肯定会给出您的数组,但 sizeof(dest) 不会给出数组的大小。我也很困惑为什么会这样。
char *foo(char *dest, const char *src) {
size_t i;
for (i = 0; dest[i] != '[=10=]'; i++);
在这里,我正在迭代以获得目标的大小。 在这种情况下,我将 "hello " 输入到 dest,其大小为 6。当我尝试使用 sizeof(dest) 时,我得到 4 作为返回值。我希望能够在不使用此 for 循环的情况下获取 dest 内部内容的大小。
char *foo(char *dest, const char *src) {
while (*dest != '[=11=]') dest++; /* increment the length of dest's pointer*/
编辑:: 我想花点时间证明我能够直接找到长度。
这都是 strcat 程序的一部分。要求是 不 使用 [ ] 括号访问或在内存中移动。
char *strcat(char *dest, const char *src) {
while (*dest != '[=12=]') dest++; /* increment the length of dest's pointer*/
while (*src != '[=12=]') /* we will be incrementing up through src*/
*dest++ = *src++; /* while this is happening we are appending
* letter by letter onto the variable dest
*/
*(dest++) = ' '; /* increment up one in memory and add a space */
*(dest++) = '[=12=]'; /* increment up one in memory and add a null
* termination at the end of our variable dest
*/
return dest; /* return the final output */
}
您正在寻找 strlen()
。但请注意,它可能是用相同的循环实现的。
对于以空字符结尾的字符串,您必须遍历每个字符才能计算出长度。即使您使用 strlen() ,它也会执行您的循环所做的事情。
因为你的函数中dest
的类型是char const*
,sizeof(dest)
和sizeof(char const*)
是一样的,也就是指针的大小。当您使用 sizeof(dest)
时得到 4 的事实表明您平台中指针的 sizeof
是 4.
获取字符串长度的唯一方法是对字符进行计数,直到遇到空字符。这很可能也是 strlen
所做的。
在 C 中,字符串存储为以 [=11=]
结尾的字符数组。
如果你想得到一个数组中的字符数,你必须遍历这个数组,你无法绕过它。
但是,您可以将大小存储在结构中,
typedef struct
{
int size;
char *data;
}String;
然后,您必须创建包装函数来写入此 String
、读取此 String
并更新数据。
如果您有很多大小的读取而没有很多更新或写入(或常量的零写入),这将很有用。
但一般来说,for 循环是更好的解决方案。
是啊!!你可以在不使用 for 循环的情况下获得大小。介绍图书馆和 strlen(dest)+1 将是您 array.cout<< dest;肯定会给出您的数组,但 sizeof(dest) 不会给出数组的大小。我也很困惑为什么会这样。