C语言 查找指针指向的字符串的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13551017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find the size of a string pointed by a pointer
提问by Manu
#include <stdio.h>
int main ()
{
char *ptr = "stackoverflow"
}
Is there any way to find the length of stackoverflow pointed by ptr, as sizeof ptr always gives 4
有没有办法找到ptr指向的stackoverflow的长度,因为sizeof ptr总是给出4
采纳答案by immayankmodi
sizeof()returns the size required by the type. Since the type you pass to sizeof in this case is a pointer, it will return size of the pointer.If you need the size of the data pointed by a pointer you will have to remember it by storing it explicitly.
sizeof()works at compile time. so,sizeof(ptr)willreturn 4 or 8 bytestypically. Instead usestrlen.
sizeof()返回类型所需的大小。由于在这种情况下传递给 sizeof 的类型是指针,因此它将返回指针的大小。如果您需要指针指向的数据的大小,则必须通过显式存储来记住它。
sizeof()在编译时工作。所以,sizeof(ptr)将return 4 or 8 bytes一般。而是使用strlen.
回答by simonc
Use strlento find the length of (number of characters in) a string
使用strlen查找字符串的长度(字符数)
const char *ptr = "stackoverflow";
size_t length = strlen(ptr);
Another minor point, note that ptris a string literal (a pointer to const memory which cannot be modified). Its better practice to declare it as const to show this.
另一个小问题,请注意它ptr是一个字符串文字(指向无法修改的常量内存的指针)。将其声明为 const 来显示这一点是更好的做法。
回答by effeffe
The strlen()function provided by string.hgives you how many "real characters" the string pointed by the argument contains. However, this length does not include the terminating null character '\0'; you have to consider it if you need the length to allocate memory.
strlen()提供的函数string.h为您提供参数指向的字符串包含多少个“真实字符”。但是,此长度不包括终止空字符'\0';如果您需要分配内存的长度,则必须考虑它。
That 4 bytes is the size of a pointer to char on your platform.
这 4 个字节是您平台上指向 char 的指针的大小。
回答by effeffe
#include<stdio.h>
main()
{
int mystrlen(char *);
char str[100];
char *p;
p=str;
printf("Enter the string..?\n");
scanf("%s",p);
int x=mystrlen(p);
printf("Length of string is=%d\n",x);
}
int mystrlen(char *p)
{
int c=0;
while(*p!='char *ptr = "stackoverflow"
size_t len = strlen(ptr);
')
{
c++;
*p++;
}
return(c);
}
simple code to understand
简单易懂的代码
回答by Rahul Tripathi
You can try using:
您可以尝试使用:
int strLen(char *s)
{
int *p = s;
while(*p !='#include <iostream>
#include <Windows.h>
int wmain()
{
// 1 byte per char, 65535 byte limit per C99 updated standard
// https://stackoverflow.com/a/5351964/3543437
const size_t ASCII_ARRAY_SAFE_SIZE_LIMIT = 65535;
// Theoretical UTF-8 upper byte limit of 6; can typically use 16383 for 4 bytes per char instead:
// https://stijndewitt.com/2014/08/09/max-bytes-in-a-utf-8-char/
const size_t UNICODE_ARRAY_SAFE_SIZE_LIMIT = 10922;
char ascii_array[] = "ACSCII stuff like ABCD1234.";
wchar_t unicode_array[] = L"Unicode stuff like → ∞ ∑ Σ? γνωρ?ζω τ?ν ?? ???.";
char * ascii_array_ptr = &ascii_array[0];
wchar_t * unicode_array_ptr = &unicode_array[0];
std::cout << "The string length of the char array is: " << strnlen_s(ascii_array_ptr, ASCII_ARRAY_SAFE_SIZE_LIMIT) << std::endl;
std::wcout << L"The string length of the wchar_t array is: " << wcsnlen_s(unicode_array_ptr, UNICODE_ARRAY_SAFE_SIZE_LIMIT) << std::endl;
return 0;
}
')
{
p++; /* increase the address until the end */
}
Return p – s; /* Subtract the two addresses, end - start */
}
回答by Kevinj22
Purely using pointers you can use pointer arithmetic:
纯粹使用指针,您可以使用指针算法:
The string length of the char array is: 27
The string length of the wchar_t array is: 47
回答by kayleeFrye_onDeck
Even though this is a generic C question, it gets pretty high hits when looking this question up for C++. Not only was I in C/C++ territory, I also had to be mindful of Microsoft's Security Development Lifecycle (SDL) Banned Function Callsfor a specific project which made strlena no-go due to,
尽管这是一个通用的 C 问题,但在为 C++ 查找此问题时,它的命中率很高。我不仅在 C/C++ 领域,我还必须注意 Microsoft 的安全开发生命周期 (SDL) 禁止特定项目的函数调用,该项目strlen由于以下原因而被禁止,
For critical applications, such as those accepting anonymous Internet connections,
strlenmust also be replaced...
对于关键应用程序,例如那些接受匿名 Internet 连接的应用程序,
strlen也必须更换...
Anyway, this answer is basically just a twist on the answers from the others but with approved Microsoft C++ alternative function calls and considerations for wide-character handling in respect to C99's updated limitof 65,535 bytes.
无论如何,这个答案基本上只是对其他答案的一个转折,但与C99 更新的 65,535 字节限制有关,已批准 Microsoft C++ 替代函数调用和宽字符处理的注意事项。
char *ptr = "stackoverflow";
length=strlen((const char *)ptr);
Output:
输出:
unsigned char *ptr;
ptr=(unsigned char *)calloc(50, sizeof(unsigned char));
scanf("%s",ptr );
length=strlen((const char *)ptr);
回答by Parham
if ptr length is an argument of a function it's reasonable to use pointers as a strings. we can get string length by following code:
如果 ptr length 是函数的参数,则使用指针作为字符串是合理的。我们可以通过以下代码获取字符串长度:
##代码##And for more explanation, if string is an input string by user with variable length, we can use following code:
并且为了更多的解释,如果字符串是用户输入的可变长度字符串,我们可以使用以下代码:
##代码##
