问题描述
我有这个功能:
int test(void * blop)
{
char * sp;
sp = blop;
int i;
for(i = 0; i< LENGTH.sp; i ++)
{
printf("%c\ n",sp [i]);
}
返回0;
}
但我似乎无法找到任何计算长度的函数
spring sp指向。
关于如何确定指针指向的字符串长度的任何提示
指向?
你没有向我们展示任何结构定义名为LENGTH,并且即使存在一个名为
,其成员名为sp。很少有任何东西
来处理名为sp的本地指针。
因此我怀疑你正在使用C ++而不是C;
如果为true则comp.lang.c ++是适当的新闻组。
在C中,字符串全部以二进制0结尾,按照定义
(如果它没有二进制0那么它就不是字符串。)
C库函数strlen()将搜索二进制0和
返回字符数 - 之前 - 该点 -
字符数*不包括*最终0本身。 (因此,如果
你知道一个字符串的strlen(),那么你需要多一个字符
,以便存储字符串的副本,因为你
还必须存储二进制0.)
-
法律 - 它是商品
- Andrew Ryan(环球邮报,2005/11/26)
你没有向我们展示任何名为LENGTH的结构定义,即使存在一个名为LENGTH的结构定义,其成员名为sp。很少有任何与名为sp的本地指针有关的东西。
因此我怀疑你正在使用C ++而不是C;
如果是真的那么comp .lang.c ++是合适的新闻组。
我正在使用C.它可以通过
打字来打印整个字符串:
int test(void * blop)
{
char * sp;
sp = blop;
printf("%s \ n",(char *)sp);
返回4;
}
当我从主叫它时:
int main(无效)
{
int a;
test(" abc");
返回0;
}
它打印abc!
I have this function:
int test(void *blop)
{
char *sp;
sp = blop;
int i;
for (i=0; i < LENGTH.sp; i++)
{
printf("%c\n", sp[i]);
}
return 0;
}
But I can''t seem to find any functions that calculate the length og the
spring sp is pointing to.
Any hints on how to dertermine the length of a string that a pointer is
pointing to?
You have not shown us any structure definition named LENGTH, and
even if one existed, its member named "sp" would rarely have anything
to do with the local pointer named "sp".
My suspicion is thus that you are working with C++ instead of C;
if true then comp.lang.c++ is the appropriate newsgroup.
In C, strings are all terminated with a binary 0, by definition
(if it does not have the binary 0 then it isn''t a "string".)
The C library function strlen() will search for the binary 0 and
return the number of characters -before- that point -- the
number of characters *excluding* the final 0 itself. (Thus if
you know the strlen() of a string, then you need one more character
than that in order to store a copy of the string, because you
must also store the binary 0.)
--
"law -- it''s a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Try strlen() from string.h.
HTH
--
A young idea is a beautiful and fragile thing. Attack people, not ideas.
You have not shown us any structure definition named LENGTH, and
even if one existed, its member named "sp" would rarely have anything
to do with the local pointer named "sp".
My suspicion is thus that you are working with C++ instead of C;
if true then comp.lang.c++ is the appropriate newsgroup.
I am working with C. Its possible to get the whole string printed just by
typing:
int test(void *blop)
{
char *sp;
sp = blop;
printf("%s\n",(char *) sp);
return 4;
}
when I call it from main:
int main(void)
{
int a;
test("abc");
return 0;
}
it prints "abc"!
这篇关于字符串的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!