problem converting int to char with or without cast?

This is a discussion on problem converting int to char with or without cast? within the C Programming forums, part of the General Programming Boards category; Code: int isEmpty(STACK *stack) { return (stack->top < 0); } ... int pop(STACK *stack) { int x; if(stack->top < 0) ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    problem converting int to char with or without cast?

    Code:
    int isEmpty(STACK *stack)
    {
    	return (stack->top < 0);
    }
    
    ...
    
    int pop(STACK *stack)
    {
    	int x;
    	
    	if(stack->top < 0)
    		printf("\n*** Stack is empty.\n");
    	else 
    	{
    		x = stack->number[stack->top];
    		stack->top--;
    	}
    	
            return x;
    }
    
    ...
    
    void reverseStack(STACK *stack)
    {
    	char tempString[MAX_SIZE];
    	int i = 0, inc = 0;
    	
    	while(!isEmpty(stack))
    	{
    		tempString[inc] = pop(stack);
    		inc++;
    	}
    	for(inc; inc < strlen(tempString); inc++)
    		push(stack, (int) tempString[inc] - 48);
    }
    this is just a common stack with top etc... and when I print out the pop(stack) as %d with printf it prints fine and stack->top is correct when printed and all information is sent correctly to reverseStack but when I try to set tempString[inc] = pop(stack) it is returned as random ASCii nothingness and I also tried typecasting (char) pop(stack) with the same result... the point of this function is simply to temporarily store a stack as a string so it can be reloaded in the opposite order. Any help as to why this is occuring would be greatly appreciated!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int pop(STACK *stack)
    {
    	int x;
    	
    	if(stack->top < 0)
    		printf("\n*** Stack is empty.\n");
    	else 
    	{
    		x = stack->number[stack->top];
    		stack->top--;
    	}
    	
            return x;
    }
    What's the value of x if your stack is empty?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    although that is a good point and I fixed that in the reverseStack function I use isEmpty as a check so it shouldn't matter because the stack will never be used when emptied.

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    If it "will never be used" why bother with the check in pop()? This is sloppy. Check in pop, return an appropriate invalid value to indicate the error.

    As for your reversal problem:
    Code:
    while(!isEmpty(stack))
    	{
    		tempString[inc] = pop(stack);
    		inc++;
    	}
    	for(inc; inc < strlen(tempString); inc++)
    		push(stack, (int) tempString[inc] - 48);
    Why are you subtracting 48? Presumably to convert the ASCII character '0' into the number 0, '1' into 1, '2' into 2, etc. But, what was originally in the stack when you popped it? A number, or a character? If it was a number, then 0 would go into tempString, not '0', and 1 would go in, not '1'. Thus when you do 1-48, you get the value -47, which is not what you want. Further, since you are using strlen, your tempString can't have any 0 values in it, because that will stop strlen.

    Also, if it IS your intention to first have "stack" contain ascii character values ('0' - '9'), then you don't null terminate your tempString after filling it in the pop/inc++ loop, so strlen will keep going until it reaches a random null character in your uninitialised array.

    Summary: Your code is weird. Explain what data stack originally contains. If it starts with character represenations of digits, then it should end with that, no need to subtract 48. If it starts with actual integer values, then it should also end with that when reversed. Again, no need to subtract. And, in the latter case, strlen will be useless if stack can contain a 0 value.
    Last edited by cwr; 10-10-2005 at 07:06 PM.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Yes, I realize this and that is why I credited him with making a valid point and said I fixed it but after fixing my "sloppiness" the problem at hand still exists. The reverseStack never goes out of bounds but when printing pop it returns all valid integers but tempString[inc] = pop(stack); and then printing tempString[inc] as %c returns ASCii even though pop(stack) = 1 and then setting a char = int should simply put the value 1 in the character array as far as I see.

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    See my edit. If you don't understand, explain to me what kind of values stack originally had, and what kind of values you intend stack to have after reversal? If you still don't understand, post a minimal but complete program that illustrates the problem. ie, include a main function that pushes some values on, calls your reverse function, and prints the result before and after.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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