Paminu said:
You're using printf() below. You need:
#include <stdio.h>
Otherwise, weird things may happen...
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)
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.