字符串的长度? [英] Length of a string?

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

问题描述

我有这个功能:


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指向。


关于如何确定指针指向的字符串长度的任何提示

指向?

解决方案

在文章< ds ********** @ news.net.uni-c.dk> ;,Paminu< sd ** @ asd.com>写道:

我有这个函数:
int test(void * blop)
{
char * sp;
sp = blop;
int i;
for(i = 0; i< LENGTH.sp; i ++)
{
printf("%c\ n",sp [i]);
}
返回0;
}
但我似乎无法找到任何计算弹簧sp指向的长度的函数。
关于如何确定指针指向的字符串长度的任何提示?




你没有向我们展示任何结构定义名为LENGTH,并且即使存在一个名为
,其成员名为sp。很少有任何东西

来处理名为sp的本地指针。


因此我怀疑你正在使用C ++而不是C;

如果为true则comp.lang.c ++是适当的新闻组。


在C中,字符串全部以二进制0结尾,按照定义

(如果它没有二进制0那么它就不是字符串。)

C库函数strlen()将搜索二进制0和

返回字符数 - 之前 - 该点 -

字符数*不包括*最终0本身。 (因此,如果

你知道一个字符串的strlen(),那么你需要多一个字符

,以便存储字符串的副本,因为你

还必须存储二进制0.)

-

法律 - 它是商品

- Andrew Ryan(环球邮报,2005/11/26)


2006-02-06,Paminu< sd ** @ asd.com> ;写道:

我有这个功能:

int test(void * blop)
{
char * sp;
sp = blop ;

int i;
for(i = 0; i< LENGTH.sp; i ++)
{/> printf("%c\ n) ,sp [i]);
}

返回0;

}

但我似乎无法找到任何用于计算弹簧sp指向的长度的函数。

有关如何确定指针指向的字符串长度的任何提示?



从string.h尝试strlen()。


HTH


-

一个年轻的想法是一件美丽而脆弱的事情。攻击人,而不是想法。


Walter Roberson写道:

文章< ds ******** **@news.net.uni-c.dk> ;, Paminu< sd ** @ asd.com>写道:

我有这个功能:


int test(void * blop)
{
char * sp ;
sp = blop;
int i;
for(i = 0; i< LENGTH.sp; i ++)
{
printf("%c) \ n",sp [i]);
}
返回0;
}


但我不能似乎找到任何计算弹簧sp指向的长度的函数。


关于如何确定字符串长度的任何提示指针指向?



你没有向我们展示任何名为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?

解决方案

In article <ds**********@news.net.uni-c.dk>, Paminu <sd**@asd.com> wrote:

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)


On 2006-02-06, Paminu <sd**@asd.com> wrote:

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?



Try strlen() from string.h.

HTH

--
A young idea is a beautiful and fragile thing. Attack people, not ideas.


Walter Roberson wrote:

In article <ds**********@news.net.uni-c.dk>, Paminu <sd**@asd.com> wrote:

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.


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"!


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

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