0

I am trying to write a shell program in C. The following function is supposed to get user line input. I keep receiving an "invalid conversion from void* to char** error on the line where I attempt to malloc.

I do not understand why. Can anyone explain?

char *get_line_input(void)
{
  int scan;
  int buff_size = 1024;
  int argument_tracker = 0;
  char *line = malloc(sizeof(char) * buff_size);

  while (1) {
    scan = getchar();
    if (scan == '\n') {
      line[argument_tracker] = '\0';
      return line;
    } else {
      line[argument_tracker] = scan;
    }
    argument_tracker++;
  }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256

2 Answers2

2

You are probably using a C++ compiler/C++ mode where there's no implicit conversion from void* to T*. In C, you shouldn't get this error as void* can be assigned to any other data pointer without explicit cast.

Either you can use a C compiler or if you must use C++ compiler then cast appropriately.

P.P
  • 117,907
  • 20
  • 175
  • 238
1

Because void * can't be implicitly converted to char * in c++ and you're probably using c++. Quick fix:

char *line = (char *)malloc(sizeof(char) * buff_size);

You should read Do I cast the result of malloc?

Community
  • 1
  • 1
icebp
  • 1,608
  • 1
  • 14
  • 24