如何获取char *(char数组)的真实长度和总长度? [英] How to get the real and total length of char * (char array)?

查看:107
本文介绍了如何获取char *(char数组)的真实长度和总长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 char [],我可以通过以下方式轻松获取其长度:

For a char [], I can easily get its length by:

char a[] = "aaaaa";
int length = sizeof(a)/sizeof(char); // length=6

但是,我不能这样做来获得 char * 的长度:

However, I cannot do like this to get the length of a char * by:

char *a = new char[10];
int length = sizeof(a)/sizeof(char);

因为,我知道,a 这里是一个指针,这样 length 在这里将始终是 4(或其他不同的系统).

because, I know, a here is a pointer, such that length here will be always be 4 (or something other in different systems).

我的问题是之后如何获得 char * 的长度?我知道有人可能会质疑我你已经知道它的 10 因为你刚刚创建了它.我想知道这一点,因为获取其长度的这一步可能距离它的创建还有很长的路要走,我不想再回来检查这个数字.而且,我也想知道它的真实长度.

My question is that how can I get the length of a char * afterwards? I know someone may challenge me that you already know its 10 because you just created it. I want to know this because this step of getting its length may come long long way from its creation and I don't want to come long long way back to check this number. Moreover, I also want to know its real length.

更具体的

  • 我怎样才能得到它真正的length=5?
  • 我怎样才能得到它的总length=10?

以下示例:

char *a = new char[10]; 
strcpy(a, "hello");

推荐答案

你不能.无论如何,不​​是 100% 准确.指针没有长度/大小,只有它自己的.它所做的只是指向内存中保存字符的特定位置.如果该字符是字符串的一部分,那么您可以使用 strlen 来确定当前指向的字符后面是什么字符,但这并不意味着您的 array箱子那么大.
基本上:

You can't. Not with 100% accuracy, anyway. The pointer has no length/size but its own. All it does is point to a particular place in memory that holds a char. If that char is part of a string, then you can use strlen to determine what chars follow the one currently being pointed to, but that doesn't mean the array in your case is that big.
Basically:

指针 不是数组,所以它不需要> 知道数组的大小是多少.指针可以指向单个值,因此指针可以存在,甚至没有数组.它甚至不关心它所指向的内存位于何处(只读、堆或堆栈......无关紧要).指针除了自身之外没有其他长度.一个指针就是...
考虑一下:

A pointer is not an array, so it doesn't need to know what the size of the array is. A pointer can point to a single value, so a pointer can exist without there even being an array. It doesn't even care where the memory it points to is situated (Read only, heap or stack... doesn't matter). A pointer doesn't have a length other than itself. A pointer just is...
Consider this:

char beep = '\a';
void alert_user(const char *msg, char *signal); //for some reason
alert_user("Hear my super-awsome noise!", &beep); //passing pointer to single char!

void alert_user(const char *msg, char *signal)
{
    printf("%s%c\n", msg, *signal);
}

指针可以是单个字符,也可以是数组的开头、结尾或中间...
将字符视为结构.您有时会在堆上分配单个结构.这也创建了一个没有数组的指针.

A pointer can be a single char, as well as the beginning, end or middle of an array...
Think of chars as structs. You sometimes allocate a single struct on the heap. That, too, creates a pointer without an array.

仅使用指针来确定它指向的数组有多大是不可能的.最接近它的是使用 calloc 并计算可以通过指针找到的连续 \0 字符的数量.当然,一旦您将内容分配/重新分配给该数组的键,这将不起作用,并且如果该数组的 外部 内存恰好保存 \0 也是.因此,使用这种方法是不可靠的、危险的并且通常是愚蠢的.别.做.

Using only a pointer, to determine how big an array it is pointing to is impossible. The closest you can get to it is using calloc and counting the number of consecutive \0 chars you can find through the pointer. Of course, that doesn't work once you've assigned/reassigned stuff to that array's keys and it also fails if the memory just outside of the array happens to hold \0, too. So using this method is unreliable, dangerous and just generally silly. Don't. Do. It.

另一个类比:
将指针视为路标,它指向Town X.标志不知道那个城镇是什么样子,也不知道或关心(或可以关心)住在那里的人.它的工作是告诉您在哪里可以找到Town X.它只能告诉你那个城镇有多远,而不能告诉你它有多大.该信息被认为与道路标志无关.这是您只能通过查看城镇本身才能找到的东西,而不是指向指向其方向的路标

Another analogy:
Think of a pointer as a road sign, it points to Town X. The sign doesn't know what that town looks like, and it doesn't know or care (or can care) who lives there. It's job is to tell you where to find Town X. It can only tell you how far that town is, but not how big it is. That information is deemed irrelevant for road-signs. That's something that you can only find out by looking at the town itself, not at the road-signs that are pointing you in its direction

所以,使用指针你唯一能做的就是:

So, using a pointer the only thing you can do is:

char a_str[] = "hello";//{h,e,l,l,o,\0}
char *arr_ptr = &a_str[0];
printf("Get length of string -> %d\n", strlen(arr_ptr));

当然,这仅在数组/字符串以 \0 结尾时才有效.

But this, of course, only works if the array/string is \0-terminated.

顺便说一句:

int length = sizeof(a)/sizeof(char);//sizeof char is guaranteed 1, so sizeof(a) is enough

实际上是将size_t(sizeof的返回类型)分配给一个int,最好写成:

is actually assigning size_t (the return type of sizeof) to an int, best write:

size_t length = sizeof(a)/sizeof(*a);//best use ptr's type -> good habit

由于size_t 是无符号类型,如果sizeof 返回更大的值,length 的值可能是你意想不到的...

Since size_t is an unsigned type, if sizeof returns bigger values, the value of length might be something you didn't expect...

这篇关于如何获取char *(char数组)的真实长度和总长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆