Mail Archives: djgpp/1999/04/27/19:41:21
> Well, consider this :
>
> char *string;
>
> void main (void)
> {
> int i;
> for (i=; i=lengthof(string); i++)
> {
> dosomething with string[i];
> }
>
> I can't do that, the compiler gives me an error.
>
> How should I declare the string?
>
First of all: you have a real string or a pointer to it?
If you referred a string declared somewhere in your code, you must declare:
extern char *string;
or
extern char string[];
If you only declare:
char *string;
you have declared a pointer, but you must initialize it with the address of a
string.
Then, first declare your string: char my_string[20]; or declare and
initialize it: char my_string[] = "hello, World"; Remember: the string is
terminated with a character '\0'. You must provide space for this in your
declaration.
If you want, you can declare a pointer to it:
char *string;
and initialize it with:
string = my_string;
or (it is the same):
string = &my_string[0];
Look this:
char string[] = "Hello, World!"
main()
{
char *pointer;
int i;
for(i=0; i<strlen(string); i++) {
putchar(string[i]);
}
/* or with a pointer */
/* you can loop checking the end of the string ('\0') */
pointer = string;
while(*pointer != '\0') {
putchar (*pointer);
pointer++;
}
}
I hope this help you
bye
Maxximo
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
- Raw text -