Click here to Skip to main content
14,699,754 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello!!!
I have a problem in C code.
I read an input from user and I want to extract input's length using pointer,but I receive some errors in terminal.


Thanks in advance...

Error:
find_string_length_pointer.c: In function ‘main’:
find_string_length_pointer.c:10:24: error: invalid initializer
     char user_in[50] = user_input();
                        ^~~~~~~~~~
find_string_length_pointer.c: In function ‘user_input’:
find_string_length_pointer.c:20:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[50]’ [-Wformat=]
     scanf("%s" , &user_input);
            ~^    ~~~~~~~~~~~
find_string_length_pointer.c:21:12: warning: return makes integer from pointer without a cast [-Wint-conversion]
     return user_input;
            ^~~~~~~~~~
find_string_length_pointer.c:21:12: warning: function returns address of local variable [-Wreturn-local-addr]


What I have tried:

#include <stdio.h>


char user_input();
int length_input(char *str_input);


int main(void)
{
    char user_in[50] = user_input();
    int str_length = length_input(user_in);
    printf("Length of input: %s , is: %d." , user_in , str_length);
}


char user_input()
{
    char user_input[50];
    printf("Give a letter/string/sentence to find it length: ");
    scanf("%s" , &user_input);
    return user_input;
}


int length_input(char *str_input)
{
    int length = 1;
    while(*str_input != '\0')
    {
        length++;
        str_input++;
    }
    return length;
}
Posted
Updated 26-Jul-20 11:06am
v2
Comments
Richard MacCutchan 27-Jul-20 3:49am
   
Why not use strlen which does it for you?

Your user_input function declares the array user_input as a local variable. When this function returns, that data disappears! You need to pass user_in (in main) as an argument to the user_input function.

Although it isn't an error, using the same name (user_input) for a function and a local variable in that function is confusing.

You may have more errors, but that was the first thing I noticed.

With regard to length_input, C provides the function strlen to find the length of a string.
   
v4
Comments
Greg Utas 26-Jul-20 15:42pm
   
You need to pass user_in to your user_input function, the same as you pass it to your length_input function.

EDIT: Most (or all) of the errors that you get when compiling are because user_input is returning a char, which you then try to assign to a char array.
CPallini 26-Jul-20 15:51pm
   
5.
Mr. Utas is on the right track. You have it pretty close but it needs a few changes. Here is one way :
void get_input( char * input )
{
    printf( "Enter a string to find its length: " );
    scanf( "%s", input );
}

int main( void )
{
    int str_length = 0;
    char user_in[ 50 ] = { 0 }

    get_input( user_in );
    str_length = length_input( user_in );
    printf( "Length of input: '%s' is: %d\n" , user_in , str_length );
}

Your length function looks OK. has a problem : it should initialize length to zero.
   
v2
Comments
CPallini 26-Jul-20 16:08pm
   
"Your length function looks OK"
It is just a bit overstimating. :-)
My 5.
Rick York 26-Jul-20 18:22pm
   
Good point. Let's hope he sees that.
In order to fix the errors in your program, following the suggestions of Greg Utas, you might write
#include <stdio.h>

void user_input(char * str_input);
int length_input(char *str_input);

int main(void)
{
  char user_in[50];
  user_input( user_in );
  int str_length = length_input(user_in);
  printf("Length of input: %s , is: %d." , user_in , str_length);

  return 0;
}

void user_input(char * str_input)
{
  printf("Give a letter/string/sentence to find it length: ");
  scanf("%s" , str_input);
}

int length_input(char *str_input)
{
  int length = 0;
  while(*str_input != '\0')
  {
    length++;
    str_input++;
  }
   return length;
}
This would work, but, unfortunately the user_input function is not robust: scanf could overrun your buffer. See How to prevent scanf causing a buffer overflow in C? - Stack Overflow[^] for workarounds.
   
Comments
Rick York 26-Jul-20 18:30pm
   
My preferred work around to this issue is to avoid the problem entirely. I can't remember the last time I wrote a console program that required input from the user. I messed around with a very old console program today but even it does not prompt the user for input. If I require user input then I write a GUI or read it from a file.

Regardless, you are entirely correct.
Nick_is_asking 27-Jul-20 4:41am
   
Thanks guys.It works fine.
How can I read user input until newline ('\n' , doesn't work,because I get this error: Segmentation fault (core dumped) and '\0' is used until NULL character.)?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900