473,394 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2390
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.