I'm new at C coming from java but I'm learning it in one of my college classes and so far it's a nightmare anytime I write a program with it :'(. I'm writing a Reverse polish notation calculator and I'm using gcc as my compiler. I want to be able to test it by doing this.


gcc - std=c99 -o test test.c
./test "10 7 +"

output>
strPointer 10 7 +
The result is : 17

(I'm having it display strPointer in the calculate() method to debug this issue)

but when I do the above(.test "10 7 +" )with my program below as is:

output>
strPointer 10 7 +

then it's done

if I don't use quotes, strPointer only displays 10

However....

if I uncomment this line at the very bottom

//fgets(argv[1],50,stdin);

this is what happens:

./test "10 7 +" (with or without quotes this time, it doesn't matter)


output>
(blank prompt so I input)

10 7 +

then it displays

strPointer 10 7 +

The result is: 17

which is what I want

HOWEVER if just do

./test (no argument this time)

output>

(blank prompt so I input)

10 7 +

then it displays


strPointer 10 7 +

Segmentation fault

so StrPointer is getting what it's suppose to get but the segmentation fault comes out
of nowhere. Earlier on it wouldn't even display that, it would just terminate


What's the deal?
All I want is to be able to do ./test 10 7 + (with or without quotes) after compilation

I can't seem to get this working using argv[1] but the program works with fgets().

I'm required to use argv[] to do the above so I can't just ignore it, what's wrong and
how can I fix it?

It seems like my calculate() method is having a hard time using argv[1]..why?

here's my code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>

typedef struct node
{
	int numdata;
	struct node * nextnode;
} node;

static node *top = NULL; //may not be static...not sure

node * peek(node * stack)
{
  stack = top;
  return stack;
}


bool Is_empty(node * stack)
{
   return peek(stack) == NULL;
}

node * push(node * stack, int number)
{
   static node * newItem;

   newItem = calloc(sizeof(node), 1);
   newItem -> numdata = number;
   newItem -> nextnode = stack;

   return newItem;
}

node * pop(node * stack)
{
   static node * temp; 
   temp = stack -> nextnode;
   free(stack);
   return temp; 
} 


void clear(void)//node * stack)
{
while(top != NULL)
{

  top = pop(top);
/*
  while(!is_empty(stack))
{
  pop(stack);
}
*/
}
}

void calculate(char * inString)
{
	char * strPointer = inString;
printf("strPointer %s \n", strPointer);
	char numStringordering[20];
	int currentNum;
	int numLength;
	int assigned = 0;
	top = NULL;

	if (strPointer[0] == '\n') 
	{
	   return;
	}

	while (strPointer[0] != '\0')
     {
	if (isdigit(strPointer[0]))
	{
		numLength = strspn(strPointer,"0123456789");
		if (assigned)
		{
		  top = push(top, currentNum);
		}
		else
		{
		  assigned = 1;
		}

	strncpy(numStringordering, strPointer, numLength);
	numStringordering[numLength] = '\0';
	currentNum = atoi(numStringordering);
	strPointer += numLength;

	}
 	else
	{
		switch (strPointer[0])
		{
			case '+':
			{
				if (top != NULL)
				{
					currentNum += top -> numdata;
					top = pop(top);
				}
				else
				{
					printf("Error! Too many operations.\n");
					return;
				}
			}
			break;
			case '-':
			{
				if (top != NULL)
				{
					currentNum = top -> numdata - currentNum;
					top = pop(top);
				}
				else
				{
					printf("Error! Too many operations.\n");
					return;
				}
			}
			break;
			case '*':
			{
				if (top != NULL)
				{
					currentNum *= top -> numdata;
					top = pop(top);
				}
				else
				{
					printf("Error! Too many operations.\n");
					return;
				}
			}
			break;
			case '/':
			{
				if (top != NULL)
				{
					currentNum = top -> numdata/currentNum;
					top = pop(top);
				}
				else
				{
					printf("Error! Too many operations.\n");
					return;
				}
			}
			break;
			case ' ': 
			break;
			case '\n':
			{
				if (top == NULL)
				{
					printf("The result is: %d. \n", currentNum);
					return;
				}
				else
				{
					printf("Error! Too many numbers.\n");
					clear();
					return;
				}

			}
			default:
			{
				printf("Error! Illegal input.\n");
				clear();
				return;		
			}
	}
	strPointer++;
	}
	
	}
		
	return;			


}

int main(int argc, char*argv[])
{
      //printf("Please input a polish postfix expression.\n\n> ");
  //fgets(argv[1],50,stdin);
     
          calculate(argv[1]);
}

Never never NEVER NEVER change argv[] values. They are command line inputs, not to be modified by the program. Copy them to an internal buffer.

If you use strPointer 10 7 + then argv[1] = "10" argv[2] = "7" argv[3] = "+"
You might want to read this series if you want to use command line parameters.

HOWEVER if just do

./test (no argument this time)

output>

(blank prompt so I input)

10 7 +

then it displays


strPointer 10 7 +

Segmentation fault

so StrPointer is getting what it's suppose to get but the segmentation fault comes out
of nowhere. Earlier on it wouldn't even display that, it would just terminate

Well, what's the value of your required argv[1] ? If you don't have any value, what are you 'calculating'? StrPointer is not getting what it's supposed to.

This article has been dead for over six months. Start a new discussion instead.