Very elementary question concerning indirection

Zev Sero zvs at bby.oz.au
Thu Feb 15 17:41:03 AEST 1990


In article <7998 at hubcap.clemson.edu> kaires at hubcap.clemson.edu (Robert G Kaires) writes:

   main()
   {
      char *pointer;
      char string[300];
	 gets(string);
	 printf( "%c\n",*(strchr(string,40)) );   <---- line in question
	 pointer = strchr(string,40);
	 printf("%c\n",*pointer);
   }

   "partest.c", line 6: illegal indirection
   "partest.c", line 7: warning: illegal combination of pointer and
   integer, op =

The first thing to do is to declare strchr as returning char *.
#include <string.h>


The second point to consider is what happens if there is no '(' in the
input line.  In this case, strchr will return NULL, which will crash
when you dereference it. What you might want to do is :
	pointer = strchr (string, '(');
	if (pointer)
	    printf ("%c\n", *pointer);
I presume you want to do more with it than that, or you could simply 
	putchar ('(');


Also, be aware that gets() is dangerous.  If you give it more than 299
characters, it will crash.  Use
	fgets (string, 299, stdin);
	string[strlen (string) - 1] = '\0';
This will get you up to 298 characters, without the '\n' at the end.
--
	                                Zev Sero  -  zvs at bby.oz.au
Megalomaniacs are simply people who know damn well they can run the universe
better then God or the present governors.	- Abner Doon (Orson S. Card)



More information about the Comp.lang.c mailing list