如何找到C中的字符数组的长度 [英] How to find the length of an char array in c

查看:247
本文介绍了如何找到C中的字符数组的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到这个长度:

char *s[]={"s","a","b"};

这应该算4使用/ 0,但strlen的或者sizeof(S)/的sizeof(char)的给了我错误的答案..
我怎样才能找到它?

it should count 4 with the /0 but the strlen or sizeof(s)/sizeof(char) gives me wrong answers.. How can i find it?

推荐答案

您正在的char * 的数组,而不是的字符。这就是为什么的strlen 将不起作用。使用

You are making an array of char* and not of char. That's why strlen won't work. Use

sizeof(s) / sizeof(char*) //should give 3

如果你想要一个字符串使用

If you want a single string use

char s[] = "sab";

这篇关于如何找到C中的字符数组的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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