472,958 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 2366
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: John Smith | last post by:
Hi, I'm writing a library in C++ which is supposed to be used by people using C. One function I have must return a string to users which is arbitrary length. The user must be able to use this...
5
by: MLH | last post by:
I'm working with lots of long strings now, it seems. I have to import them & parse them constantly. The A97 memo field type supports only 32768 chars. What happens when this is processed... Dim...
3
by: Jimski | last post by:
Hello all, I am having a problem where I get an error message when I call FlushFinalBlock when decrypting my encrypted text. I am using the Rijndael algorithm. The error message is "Length...
1
by: gane kol | last post by:
Hi I am using DES algorithm. I am getting an error message in a few cases of the querystring Error Message : Length of the data to decrypt is invalid. Error Method : System.String...
7
by: Matt | last post by:
Have below code AcctNbr give me result of 30. That is the database column length, Column stores 10 why is giving me 30 ? while(objRead.Read()) { AcctNbr = (string)objRead.ToString().Length;...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
10
by: Mavenos | last post by:
Hi Web Masters, Just wondering wether you can help us to come up with some tokenize script. My problem is wanted to display a LONG content into a short para (by giving minimum letter lenght)...
1
by: Sathyaish | last post by:
I have the following scenario: Algorithm: 3DES Cipher Mode: CBC Key Size: 128-bit Block Size: 64 bit IV: 0x0000000000000000 (an eight byte array of zeros) The results I get using .NET with...
6
by: kellygreer1 | last post by:
What is a good one line method for doing a "length safe" String.Substring? The VB classes offer up the old Left function so that string s = Microsoft.VisualBasic.Left("kelly",200) // s will =...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.