C语言 在 C 中查找字符数组的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4180818/
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
Finding the length of a Character Array in C
提问by not_l33t
What is a way in C that someone could find the length of a Character array?
在 C 中,有人可以找到 Character 数组的长度的方法是什么?
I will happily accept psuedo-code, but am not averse to someone writing it out if they'd like to :)
我很乐意接受伪代码,但如果有人愿意,我并不反对写出来:)
回答by Daniel A. White
Provided the char array is nullterminated,
如果字符数组null终止,
char chararray[10];
size_t len = strlen(chararray);
回答by James McNellis
If you have an array, then you can find the number of elements in the array by dividing the size of the array in bytes by the size of each element in bytes:
如果您有一个数组,那么您可以通过将数组的大小(以字节为单位)除以每个元素的大小(以字节为单位)来求出数组中的元素数:
char x[10];
int elements_in_x = sizeof(x) / sizeof(x[0]);
For the specific case of char, since sizeof(char) == 1, sizeof(x)will yield the same result.
对于 的特定情况char,因为sizeof(char) == 1,sizeof(x)将产生相同的结果。
If you only have a pointer to an array, then there's no way to find the number of elements in the pointed-to array. You have to keep track of that yourself. For example, given:
如果您只有一个指向数组的指针,则无法找到指向数组中元素的数量。你必须自己跟踪。例如,给定:
char x[10];
char* pointer_to_x = x;
there is no way to tell from just pointer_to_xthat it points to an array of 10 elements. You have to keep track of that information yourself.
仅凭pointer_to_x它指向一个包含 10 个元素的数组就无法判断了。您必须自己跟踪该信息。
There are numerous ways to do that: you can either store the number of elements in a variable or you can encode the contents of the array such that you can get its size somehow by analyzing its contents (this is effectively what null-terminated strings do: they place a '\0'character at the end of the string so that you know when the string ends).
有很多方法可以做到这一点:您可以将元素的数量存储在变量中,也可以对数组的内容进行编码,这样您就可以通过分析其内容以某种方式获得其大小(这实际上是空终止字符串所做的:他们'\0'在字符串的末尾放置一个字符,以便您知道字符串何时结束)。
回答by SteveMoros
Hi although the above answers are OK, here's my contribution to your question.
您好,虽然以上答案都可以,但这是我对您的问题的贡献。
//returns the size of a character array using a pointer to the first element of the character array
int size(char *ptr)
{
//variable used to access the subsequent array elements.
int offset = 0;
//variable that counts the number of elements in your array
int count = 0;
//While loop that tests whether the end of the array has been reached
while (*(ptr + offset) != 'char myArray[] = {'h', 'e', 'l', 'l', 'o'};
printf("The size of my character array is: %d\n", size(&myArray[0]));
')
{
//increment the count variable
++count;
//advance to the next element of the array
++offset;
}
//return the size of the array
return count;
}
In function main you call function size by passing the address of the first element of your array. For example:
在函数 main 中,您通过传递数组第一个元素的地址来调用函数大小。例如:
strlen(urarray);
All the best
祝一切顺利
回答by dco
You can use strlen
你可以使用 strlen
size_t my_strlen(const char *str)
{
size_t i;
for (i = 0; str[i]; i++);
return i;
}
You can code it yourself so you understand how it works
您可以自己编写代码,以便了解它是如何工作的
char urarray[255];
printf("%zu", sizeof(urarray));
if you want the size of the array then you use sizeof
如果你想要数组的大小,那么你使用 sizeof
unsigned int len=0;
while(*(msg+len) ) len++;
回答by onemasse
If you want the length of the character array use sizeof(array)/sizeof(array[0]), if you want the length of the string use strlen(array).
如果你想要字符数组的长度使用sizeof(array)/sizeof(array[0]),如果你想要字符串的长度使用strlen(array).
回答by Imre Kiss
There is also a compact form for that, if you do not want to rely on strlen. Assuming that the character array you are considering is "msg":
如果您不想依赖 strlen,还有一个紧凑的形式。假设您正在考虑的字符数组是“msg”:
char h[] = "hello";
printf("%d\n",sizeof(h)-1); //Output = 5
回答by Beyondo
using sizeof()
使用sizeof()
#include <string.h>
char h[] = "hello";
printf("%d\n",strlen(h)); //Output = 5
using string.h
使用string.h
int strsize(const char* str);
int main(){
char h[] = "hello";
printf("%d\n",strsize(h)); //Output = 5
return 0;
}
int strsize(const char* str){
return (*str) ? strsize(++str) + 1 : 0;
}
using function(
strlenimplementation)
##代码##使用函数(
strlen实现)

