   Convert unsigned char * to char *?
  Hi, I'm trying to use fgets to read from stdin (I've heard it's better to use fgets instead of gets), but my buffer is an unsigned char *, while the first argument requires a char *.  I then need to convert it back to unsigned char* for the next function that uses it, QuickUsbWriteCommand.  I've tried typecasting it, but it doesn't work.  Any help would be greatly appreciated.  Here are the important parts of my code.(By the way, it's a program that sends data to a USB module)...buffer[index] = (unsigned char *)malloc(200);...for (index = 0; index &lt; bufcnt; index++)    {bytesread = fgets(buffer[index], 200, stdin);  if (bytesread == 0)  {   //end of file or read errorshutdown = TRUE;    break; } bytes2write[index] = bytesread;for (repeat = 100; repeat &gt; 0; repeat--)  result = QuickUsbWriteCommand(hDevice, 0, buffer[index], bytes2write[index]);//QuickUsbWriteCommand requires an unsigned char* for buffer[index]   if (result == FALSE)   {    QuickUsbGetLastError(&error);    printf("QuickUsbWriteCommand error %d index %d\n", error, index);    fclose(inFile);    QuickUsbClose(hDevice);    Sleep(5000);    // let the user see the error    return 1; } lastIndex = index;  }
Quote:Original post by ianp622I've tried typecasting it, but it doesn't work.How do you mean it doesn't work? It should work. Post how you did the cast, and if you got an error message doing it, post it.;  When I do...bytesread = fgets((char *)buffer[index], 200, stdin);I get... (oh, this is Visual C++ BTW)error C2440: '=' : cannot convert from 'char *' to 'unsigned long'   There is no context in which this conversion is possibleI don't know why it says cannot convert to unsigned long, because fgets says it takes the buffer as a char *.   ;  Ah, well... fgets returns char*, so if bytesread is an unsigned long it won't work.   ;  Yeah, thanks so much for the help.  I just realized that now and came back to post that lol.;Quote:(I've heard it's better to use fgets instead of gets)fgets() is better than gets() because fgets() provides a means of specifying the maximum number of characters to read (gets() does not) which means its a much nicer function to use if you are concerned about buffer overruns.   