always prints ASCII character 255

This is a discussion on always prints ASCII character 255 within the C Programming forums, part of the General Programming Boards category; I am passing a string through an external validation function (that I wrote and was working previously) and the function ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    59

    always prints ASCII character 255

    I am passing a string through an external validation function (that I wrote and was working previously) and the function is correctly picking up all the errors, but when I enter the correct input and printf the result to the screen it prints the ASCII character of 255 (y with two dots above it). It does this no matter what number I put in and because of the ASCII value I have a feeling it has something to do with my buffer size of 256? When I changed the buffer size to 1256 (just to troubleshoot) it printed nothing to the screen when there was correct input and carried on to the next step.

    When I change the printf sign to %s it returns segmentation fault

    Here is my code;

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    char *validate(char *valid);
    
    int main(void)
    {
    	char    buffer1[256], buffer2[256], buffer3[256];
    	char    *input_1, *input_2, *input_3; 
            char    *valid_1, *valid_2, *valid_3;
    
    	printf("Please enter input 1.\n");
    	input_1 = fgets(buffer1, 256, stdin);
    	
    	valid_1 = validate(input_1);
    	printf("valid_1 is %c\n", valid_1);
    
    	printf("Please enter input 2.\n");
    	input_2 = fgets(buffer2, 256, stdin);
    	
    	valid_2 = validate(input_2);
    	printf("valid_2 is %c\n", valid_2);
    	
    	printf("Please enter input 3.\n");
    	input_3 = fgets(buffer3, 256, stdin);
    	
    	valid_3 = validate(input_3);
    	printf("valid_3 is %c\n", valid_3);
    	
    	return(0);
    }
    
    
    char *validate(char *valid)
    {
    	char	*valid_input;
    	int	length, j, k;
    	
    	
    	length = strlen(valid);
    	
    	for (k=0; k<length; k++)
    	{
    		if (valid[k] == '\n')
    		{
    			valid[k] = '\0';
    			length--;
    		}
    	}
    	
    	for(j = 0; j < length; j++)
    	{
    		if (('0' <= valid[j]) && (valid[j] <= '9'))
    		{
    			continue;
    		}
    		else if (valid[j] == '.')
    		{
    			if (j ==0)
    			{
    				printf("Please enter a number before the dot in your input.\n");
    				exit(EXIT_FAILURE);
    			}
    			else if (j == (length-1))
    			{
    				printf("Please enter a number after the dot in your input.\n");
    				exit(EXIT_FAILURE);
    			}
    			else
    			{
    				continue;
    			}
    		}
    		else if (valid[j] == '+')
    		{
    			if (j == 0)
    			{
    				continue;
    			}
    			else
    			{
    				printf("There is a plus sign in the middle of your entry.\n");
    				exit(EXIT_FAILURE);
    			}
    		}
    		else if (valid[j] == '-')
    		{
    			printf("There is a negative sign in your entry.\n");
    			exit(EXIT_FAILURE);
    		}
    		else
    		{
    			printf("You have not entered the correct input.\n");
    			exit(EXIT_FAILURE);
    		}
    	}
    	
    	return(valid_input);
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Your first major problem is that your validate() function is returning a string that you haven't initialized. The only place valid_input is even used in that function is the definition at the top of the function and the return statement. The validate() function really doesn't even need to return anything at all. You're modifying the valid string inside the function and it will persist in the calling function. You'd have to change the way you call validate() since it wouldn't be returning a value anymore, obviously.


    Code:
    	for (k=0; k<length; k++)
    	{
    		if (valid[k] == '\n')
    		{
    			valid[k] = '\0';
    			length--;
    		}
    	}
    What if the '\n' isn't right before the '\0'? Your length will be off. That loop might better be written as:
    Code:
    for(k = 0;valid[k] != '\0';++k)
    {
      if(valid[k] == '\n')
      {
        valid[k] = '\0';
        length = k;
        break;
      }
    }
    Or better yet, let the standard library do the work for you:
    Code:
    char *p;
    if((p = strchr(valid, '\n')))
    {
      *p = '\0';
      length = p - valid;
    }
    Of course, length really isn't necessary if you change your loop conditions to just check if valid[index] is equal to '\0'.


    Code:
    	printf("valid_1 is %c\n", valid_1);
    valid_1 is defined as a char * and should not be printed using the %c format specifier. Use %s to print strings.


    Code:
    	printf("Please enter input 1.\n");
    	input_1 = fgets(buffer1, 256, stdin);
    
    	valid_1 = validate(input_1);
    Why even have input_1? You can just do this:
    Code:
    fgets(buffer1, 256, stdin);
    valid_1 = validate(buffer1);
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    In Main:

    Where are you allocating memory for these pointers?

    Code:
    	char    *input_1, *input_2, *input_3; 
            char    *valid_1, *valid_2, *valid_3;
    In this function:

    Code:
    char *validate(char *valid)
    {
    	char	*valid_input;
    Where do you allocate memory for this pointer?


    In this snippet:
    Code:
    	valid_1 = validate(input_1);
    	printf("valid_1 is %c\n", valid_1);
    You are calling validate() with an uninitialized pointer.

    In the printf the "%c" says character but valid_1 is a character pointer.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Quote Originally Posted by jimblumberg View Post
    In Main:

    Where are you allocating memory for these pointers?

    Code:
    	char    *input_1, *input_2, *input_3; 
            char    *valid_1, *valid_2, *valid_3;
    The input variables don't need memory allocated because fgets() just makes them point to buffer variables' memory.

    The valid variables wouldn't need memory allocated either, if validate() was written correctly (see my above post).
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Thanks guys.

    I got rid of all the "input_x" and put in your loop to recalculate the length. Out of interest, when would '\n' not be right before '\0' in this scenario?

    I initialised the string (i think) by putting into main

    Code:
    char *valid_input;
    I also changed the external function so it does not return a value and have the function called in main like this;

    Code:
    fgets(buffer1, 256, stdin);
    	
    valid_1 = validate(buffer1);
    printf("valid_1 is %s\n", valid_1);
    Is that what you meant?

    I am now getting the correct number printing using %s

    Thanks again!!

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Quote Originally Posted by browser
    Out of interest, when would '\n' not be right before '\0' in this scenario?
    It wouldn't in this scenario, but it would be nice if validate() was a re-usable function, right? And in order to be re-usable it should know as little about the calling function as possible. And not having validate() assume the input came from fgets() is just one more way to increase its re-usability. You're right though; in this scenario it doesn't make a difference.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  2. Extended character ASCII in LINUX/WINDOWS
    By intmail in forum Linux Programming
    Replies: 2
    Last Post: 01-26-2006, 02:24 PM
  3. Replies: 3
    Last Post: 01-25-2006, 07:04 PM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 04:32 PM
  5. Replies: 1
    Last Post: 07-31-2002, 10:49 AM

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