delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/04/27/19:41:21

From: Maxximo <mssmsoft AT my-dejanews DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: I need help adressing characters in a string
Date: Tue, 27 Apr 1999 11:05:57 GMT
Organization: Deja News - The Leader in Internet Discussion
Lines: 78
Message-ID: <7g45mj$fih$1@nnrp1.dejanews.com>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 990426134857 DOT 22711F-100000 AT is> <7g439h$dk3$1 AT nnrp1 DOT dejanews DOT com>
NNTP-Posting-Host: 138.132.53.11
X-Article-Creation-Date: Tue Apr 27 11:05:57 1999 GMT
X-Http-User-Agent: Mozilla/3.04Gold (X11; I; OSF1 V4.0 alpha)
X-Http-Proxy: 1.0 x11.dejanews.com:80 (Squid/1.1.22) for client 138.132.53.11
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019