[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

C Program not working..



 Hi all

 Feeling absolutely stupid for posting a C problem but I am stuck Please
help me if you can..

 Following program dumps the core. I do not know why... Also I get warning
while compiling that 'gets' is a dangerous function..

 Please help..


 TIA..
 
  Bye
   Shridhar


****Program  
#include <stdio.h>

main()
 {
  char a[100];
  int flag=0,i,j,ftemp=0,fieldno=2;
  char *ptr,*line;

   printf("\nEnter a string.. ");
   gets(a);

   line=a;

  ptr=(char *)malloc(100*sizeof(char));

  if(ptr)
  {
    i=0;  
   while(ftemp<fieldno)    /*skip earlier fields/words */
    {
     for(;line[i]!='\0'||line[i]==" "||line[i]=="\t";i++);
     ftemp++;
     if(line[i-1]=="\0"){ptr=NULL;break;}
     for(;line[i]!='\0'||line[i]!=" "||line[i]!="\t";i++);  /* eat up non
space characters*/
    }
    j=i-1; 
    for(;line[i]!='\0'||line[i]!=" "||line[i]!="\t";i++);   /*go to the
end of the word*/
  
    if(ptr)
    {
     strncpy(ptr,line[j],i-j-1);   /*copy the word*/
     ptr[i-j]=0;
     fprintf(stderr,"The field is %s",ptr);
   }

 }
 }