Length of a string?

P

Paminu

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);
}

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?
 
L

Lew Pitcher

Paminu said:
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);
}

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)
 
W

Walter Roberson

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);
}
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.)
 
V

Vlad Popescu

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);
}

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
 
P

Paminu

Walter said:
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);
}
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"!
 
V

Vladimir S. Oka

Paminu said:
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);
}

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 said:
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.
 
V

Vladimir S. Oka

Paminu said:
Walter said:
Paminu <[email protected]> said:
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);
}
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...
 
N

Nelu

Paminu said:
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);
}

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

Nelu

Paminu said:
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?
 
K

Keith Thompson

Paminu said:
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);
}

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

John Bode

Paminu said:
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);
}

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++)
{
...
}
 
K

Keith Thompson

John Bode said:
Paminu said:
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++) {
/* ... */
}
 
D

Dave Thompson

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
 
V

Vladimir S. Oka

Dave said:

Yes, of course. Thanks for spotting it.
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top