[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: oh no! more problems



On Sun, 13 Jun 1999, Rob Kaper wrote:

> On Sun, Jun 13, 1999 at 06:32:16PM +0200, Felix Kollmann wrote:
> > 	if (&zeichen== ".") {printf ("___.___\n");}
> > and also:
> > 	if (zeichen== (char) ".") {printf ("___.___\n");}
> C doesn't work like that (at least not with strings).
> 
> Try the strcmp function. It returns true when strings are different, and
> false when they are the same.
> 
> if (strcmp(str, "Rob")) printf ("Your name is not Rob\n");
> else printf("You are one cool dude!\n");
> 

Or, if you only want to compare one character of a string:



	char *zeichen="text";
	int character_index = 1;

	/* get 1 character of a string */

	printf("character %i of the string in zeichen is %c\n",
character_index, zeichen[ character_index ] );

	/* compare a character in 'text' with other character */

	char c = 'e';
	if ( text[ character_index ] == c )
		; // do something

	if ( text[ character_index ] == 'e' )
		; // do something
	

	/*
		In general,

		even if you write char *str = "s", you still get a string,
although it's only 1 character long. a string is a pointer, not a value, so to
compare it you need to do more than use '==' (that'd compare the pointer, not
the content, which is what you want.. To get a certain character of a string
(character from an array of characters), use array indexing ( zeichen[ 2 ] )
or this: *( zeichen + 2 ).
	*/




Tomas Andrle / red_hatred
tomaasz@penguinpowered.com