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

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

问题描述

对于的char [] ,我可以很容易地获取其长度:

 的char a [] =AAAAA
INT长度= sizeof的(A)/ sizeof的(焦); //长度= 6

不过,我不能做这样获得的char *的长度

 的char * A =新的char [10];
INT长度= sizeof的(A)/ sizeof的(焦);

因为,我知道, A 这里是一个指针,这样长度这里将永远是 4 (或其他在不同的系统的东西)。

我的问题是,我怎么能得到一个的char * 之后的长度?的我知道有人会质疑我,你已经知道它的 10 ,因为你刚才创建它。我想知道这是因为获得它的长度这一步可能来自它的创作很长很长的路要走,我不想来长很长的路回查该号码。此外,我也想知道它的实际长度。

要更具体地


  • 我怎样才能得到它的实长度= 5

  • 我怎样才能得到它的总长度= 10

在下面的例子:

 的char * A =新的char [10];
的strcpy(一,你好);


解决方案

您不能。不是100%的准确率,反正。指针的没有长度/大小,但其自身的。它所做的就是指向内存中的特定位置保存一个字符。如果char是一个字符串的一部分,那么你可以使用的strlen 来确定哪些字符遵循目前正在指着之一,但这并不意味着的阵列的你的情况是那么大。

基本上是:

A 指针不是 阵列,所以也没有的需要知道数组的大小是什么。指针可以指向单一值,因此,一个指针可以在不存在甚至是一个数组存在。它甚至不关心它指的是位于内存(只读,堆或叠......没关系)。指针没有自身以外的长度。指针正好是...

试想一下:

 字符哔='\\一';
无效alert_user(为const char *味精,字符*信号); //由于某种原因
alert_user(听到我的超级棒极了噪音!,&安培;蜂鸣声); //传递指针单个字符!
//
无效alert_user(为const char *味精,字符*信号)
{
    的printf(%S%C \\ N,味精,*信号);
}

一个指针可以是单个字符藏汉作为数组
的...的开始,结束或中间
想想字符作为结构的。有时你在堆上分配一个单一的结构。这一点也创建了一个没有指针数组。

仅使用一指针,以确定它指向的数组多大是不可能的。你可以得到它最接近的是使用释放calloc 和计数连续\\ 0字符可以通过指针找到的数量。当然,一旦你分配不工作/重新分配的东西到该阵列的钥匙,如果内存仅仅之外的数组恰好持有 \\ 0它也失败,太。因此,使用这种方法是不可靠的,危险的,只是一般傻了。别。做。它

另一个比喻:

想想一个指针作为路标,它指向的镇X 。符号不知道城里是什么样子,它不知道或不关心(或者关心)谁住在这里。它的工作就是告诉你在哪里找到的镇X 。它只能告诉你,镇有多远,但不知道如何大它是。这些信息被视为无关道路标志。在这个时候,你只能通过看城市本身,而不是那些指点你在它的方向的道路标志找出

所以,使用指针,你可以做的唯一事情是:

 字符a_str [] =你好; // {H,E,L,L,O,\\ 0}
字符* arr_ptr =安培; a_str [0];
的printf(获取一个字符串的长度 - >%d个\\ N的strlen(arr_ptr));

但是,这当然只适用如果阵列/字符串\\ 0终结

作为asside:

// sizeof的炭保证1,这样的sizeof(a)是不够的;

  INT长度= sizeof的(A)/ sizeof的(炭)

实际上是分配为size_t (sizeof的 的返回类型)到 INT ,最好写:

 为size_t长度= sizeof的(A)/ sizeof的(*一); //最佳使用PTR的类型 - >好习惯

由于为size_t 是一个无符号的类型,如果的sizeof 返回较大值,值长度可能是你的东西,没想到...

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

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

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);

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

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.

To be more specific

  • how can I get its real length=5?
  • how can I get its total length=10?

for the following example:

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

解决方案

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 aswell 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.

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.

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));

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

As an asside:

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

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

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

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

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