470,810 Members | 1,251 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

Length of a string?

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?
Feb 6 '06 #1
13 2159
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)
Feb 6 '06 #2
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.
Feb 6 '06 #3
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"!
Feb 6 '06 #4
Paminu 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.
You didn't look very hard, did you.
Any hints on how to dertermine the length of a string that a pointer is
pointing to?


Try
strlen(sp)

--

Lew Pitcher, IT Specialist, Corporate Technical Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
Feb 6 '06 #5
Paminu wrote:
I have this function:
You're using printf() below. You need:

#include <stdio.h>

Otherwise, weird things may happen...
int test(void *blop)
If `blop` is going to be used as a `char*`, as implied below, you might
as well have:

int test(char *blop)

If you're not going to change string pointed to by `blop`, this is even
better:

int test(const char *blop)
{
char *sp;
sp = blop;
In your function, such as it is, you don't need this at all. You could
use `blop` instead of `sp`.


int i;
for (i=0; i < LENGTH.sp; i++) ^^^^^^^^^

This does not make sense. A pointer is not a member of some structure,
as this implies. I guess you wanted strlen(sp) instead.
{
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.
This is most likely because you haven't looked. Function strlen() is
standard, and readily available in any conforming compiler (remember to
incluse <string.h>).
Any hints on how to dertermine the length of a string that a pointer
is pointing to?


Use strlen(). BTW, you know that in C, strings are terminated by a zero
character ('\0').

Here's a variant of what you've been trying to do:

/* for printf() */
#include <stdio.h>

int test(const char *s)
{
int len = 0;

while (s)
{
/*
incrementing `s` is intentionally in
separate statement, for clarity
*/
printf("%c\n",*s);
++s;
++len;
}

return len;
}

As a bonus, it gives you the length of the string. NB, the function
assumes that `s` contains a valid pointer (possibly NULL).

PS (and OT)
I guess there are languages where strings are structures (objects), but
there you'd get to their lenght by writing s.LENGTH not LENGTH.s. You
probably need to go back to some good textbook.

--
BR, Vladimir

"You can write a small letter to Grandma in the filename."
-- Forbes Burkowski, Computer Science 454

Feb 6 '06 #6
Paminu wrote:
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"!


We knew that.

And how does this relate to your OP? Or Walters, for that matter...

--
BR, Vladimir

If God had not given us sticky tape, it would have been necessary to
invent it.

Feb 6 '06 #7
Paminu 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 can use strlen in <string.h> to find the length of a string.
As it would be easier to just ask for a function that returns the
length of a string, I assume you posted the code so that we can
show you the function in the code. I can only assume that LENGTH.sp
(which could be a macro) has something to do with it but the code you
posted is not enough.
Also, doing something like:
size_t i; /*strlen returns size_t not int*/
char *c=sp;
for(i=0;i<strlen(c);i++) {
....
}
is not the best way to accomplish something, I would think that
LENGTH.sp is not just:
#define LENGTH.sp strlen(sp)
So, show us the included headers and other things related to
LENGTH.sp.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Feb 6 '06 #8
Paminu wrote:
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;

Why do you cast sp with (char *) since you already declared it
as char *sp?

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Feb 6 '06 #9
Paminu <sd**@asd.com> writes:
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?


The nature of your question, and of the code you posted, seem to imply
that you're just starting to learn C. At this point, you'd probably
be better off working through a tutorial or a good book than asking
elementary questions here.

K&R2 (Kernighan & Ritchie, _The C Programming Language_, 2nd Edition)
is widely considered to be the best C textbook, but it tends to assume
*some* previous programming experience. Section 18 of the comp.lang.c
FAQ, <http://www.c-faq.com/resources/index.html>, has some good
pointers.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 6 '06 #10

Paminu 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.


#include <string.h>
....
for (i = 0; i < strlen(sp); i++)
{
...
}

Feb 6 '06 #11
"John Bode" <jo*******@my-deja.com> writes:
Paminu wrote:
I have this function:
[snip]
But I can't seem to find any functions that calculate the length og the
spring sp is pointing to.


#include <string.h>
...
for (i = 0; i < strlen(sp); i++)
{
...
}


That's quite inefficient. It computes strlen(sp) once for each
execution of the loop.

Try this:

/* Untested code off the top of my head. */
int len = strlen(sp);
for (i = 0; i < len; i++) {
/* ... */
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 6 '06 #12
On Mon, 6 Feb 2006 21:21:04 +0000 (UTC), "Vladimir S. Oka"
<no****@btopenworld.com> wrote:
Here's a variant of what you've been trying to do:

/* for printf() */
#include <stdio.h>

int test(const char *s)
{
int len = 0;

while (s)
YM *s .
{
/*
incrementing `s` is intentionally in
separate statement, for clarity
*/
printf("%c\n",*s);
++s;
++len;
}

return len;
}

As a bonus, it gives you the length of the string. NB, the function
assumes that `s` contains a valid pointer (possibly NULL).

PS (and OT)
I guess there are languages where strings are structures (objects), but
there you'd get to their lenght by writing s.LENGTH not LENGTH.s. You
probably need to go back to some good textbook.


There is also at least one language (Fortran) where dots are part of
the operator syntax, and % is used for structure member selection.
They could have (but didn't) use .LENGTH.s for string length.

- David.Thompson1 at worldnet.att.net
Feb 13 '06 #13
Dave Thompson wrote:
On Mon, 6 Feb 2006 21:21:04 +0000 (UTC), "Vladimir S. Oka"
<no****@btopenworld.com> wrote:
Here's a variant of what you've been trying to do:

/* for printf() */
#include <stdio.h>

int test(const char *s)
{
int len = 0;

while (s)


YM *s .


Yes, of course. Thanks for spotting it.
{
/*
incrementing `s` is intentionally in
separate statement, for clarity
*/
printf("%c\n",*s);
++s;
++len;
}

return len;
}

As a bonus, it gives you the length of the string. NB, the function
assumes that `s` contains a valid pointer (possibly NULL).

PS (and OT)
I guess there are languages where strings are structures (objects),
but there you'd get to their lenght by writing s.LENGTH not LENGTH.s.
You probably need to go back to some good textbook.


There is also at least one language (Fortran) where dots are part of
the operator syntax, and % is used for structure member selection.
They could have (but didn't) use .LENGTH.s for string length.


Ah, sweet memories... Haven't used Fortran for ages...

--
BR, Vladimir

BELA LUGOSI is my co-pilot ...

Feb 13 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by gane kol | last post: by
7 posts views Thread by Matt | last post: by
10 posts views Thread by Mavenos | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.