Segmentation fault on my program

This is a discussion on Segmentation fault on my program within the C Programming forums, part of the General Programming Boards category; I tried to run my unfinishem program and I get a segmentation fault. It compiles without errors and warnings. Code: ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    67

    Segmentation fault on my program

    I tried to run my unfinishem program and I get a segmentation fault. It compiles without errors and warnings.

    Code:
    //Reverse Polish Notation Calc
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<string.h>
    void help();
    
    int main(int argc, char *argv[]){
    int i;
    int u=0;
    int j=0;
    int z=0;
    int st_stevk=0;
    float sklad[20];
    float plus[20];
    
          if(argc<4)
            {
              help();
    	  exit(1);
    	 }
    	 
    	 
      for(i=1;i<argc;i++)
      
       {
        if(isdigit(argv[i]))
            {
    	  sklad[j]=atof(argv[i]);
    	  j++;
    	  st_stevk++;
        }
    	  if((st_stevk>=2)&&(isalpha(argv[i])))
    	   {
    	     z=j-st_stevk;
    	     switch(*argv[i]){ //ascii kode od operatorjev
    	     
    	     case '+': for(u=0;u<st_stevk;u++)
    	                {
    			   plus[u]=sklad[z];
    			   z++;
    			  
    			 };
    	    //case '-':  for(i=
    			 
    	 }
    	  st_stevk=0;
    	
        }
    
    }
     return 0;
     
    }
     void help() {
                      printf("\n");
       char hilfe[]="Vpisati moras vsaj tri argumente!\n" 
                    "Primer: ./sedma 3 4 +, pri tem pa\n" 
    	        "mora biti operator vedno za zadnjim\n"
    	        "operandom, ki ga operacija zajema\n";
    	          printf("%s\n",hilfe);
    	    
    	    }
    Im trying to make a RPN calculator. That means that first the operands are inputed(im my prog as cmd arguments) and then the operator.
    Example:
    3 5 + would print 8 (3+5).
    The reason Im complicating is because I would like to make this to work for more operands and operators:
    Example :
    input: 2 3 + returns 5
    5 4 + returns 9
    * returns 45

    Anyway, can someone please explain what causes segmentation fault.

    THanks!
    Last edited by blackswan; 05-11-2005 at 02:38 PM.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    On HPUX compile as ANSI C this does not segfault-
    segfaulting is trying to access memory your processes does not own.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<string.h>
    void help(void);
    
    int main(int argc, char *argv[])
    {
        int i;
        int j=0;
        int z=0;
        int st_stevk=0;
        float sklad[20]={0.};
        float plus[20]={0.};
    
        if(argc<4)
        {
             help();
    	     exit(EXIT_FAILURE);
    	}
    	 
    	 
       for(i=0;i<argc;i++)  
       {    
          if(isdigit(*argv[i]))
          {
    	     sklad[j]=atof(argv[i]);	  
    	     j++;
    	     st_stevk++;
    	     continue;
    	  }
    	  
          if((st_stevk>=2)&&(isalpha(*argv[i])) )
    	  {
    	     z=j-st_stevk;
    	     switch(*argv[i])
    	     { //ascii kode od operatorjev
    	     
    	         case '+': 
    	                for(i=0;i<st_stevk;i++)
    	                {
    			           plus[i]=sklad[z];
    			           z++;			  
    			        }	   
    			        break;
    		     default:
    		            break;
    	       		 
    	     }
    	     st_stevk=0;
          }
       }
       return 0;
    }
    void help(void) 
    {
        printf("\n");
        printf("%s","Vpisati moras vsaj tri argumente!\nPrimer: ./sedma 3 4 +, pri tem pa\n");
    	printf("%s", "mora biti operator vedno za zadnjim\n");
    	printf("%s","operandom, ki ga operacija zajema\n");
    }

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Thanks man! U helped a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange segmentation fault
    By Ron in forum C Programming
    Replies: 24
    Last Post: 06-15-2008, 02:10 PM
  2. Segmentation Fault Problem: Urgent Help
    By bodydrop in forum C Programming
    Replies: 3
    Last Post: 05-05-2006, 08:02 PM
  3. Segmentation Fault??
    By SteveP4444 in forum C Programming
    Replies: 5
    Last Post: 04-01-2006, 01:24 AM
  4. Segmentation Fault Error
    By jcramer in forum C Programming
    Replies: 2
    Last Post: 11-23-2003, 01:16 PM
  5. debugging: sigsegv, segmentation fault???
    By Gonzo in forum C Programming
    Replies: 9
    Last Post: 09-16-2003, 06:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21