views:

880

answers:

2

it has been a while since I messed with C code.

I am getting the following error when compiling C code under ubuntu using gcc.

command i am using to compile code is(if these errors are because of comiler I am using , please let me know how to make that go away):

gcc -o runnable mycode.C 

error: invalid conversion from ‘void*’ to ‘char**’

line 39 is :

sequence=malloc(sizeof(char *)*seqNum);

sequence is declared as:

char **sequence;

seqNum is declared as:

int seqNum
+3  A: 

You need to cast the result of malloc to be the type you want.

So:

 char **sequence;
 ...
 sequence = (char **)malloc(sizeof(char *) * seqNum);

Also remember that if you're going to use sequence you'll need to allocate a list of "char *"s as you have done but then you don't have the pointed-to memory allocated, it's only allocated the space for the list of pointers themselves.

Part of the reason for this being an error is that assigning between different pointer types can change the alignment required. Malloc is guaranteed to return a pointer to space with alignment suitable for any type.

Legooolas
That only applies to C++. The poster believes he is writing a C application, which is less strict.
Raymond Martineau
Ah yes, just saw that in the comment on the answer. It's still good practice to cast the result of malloc in C, mind :)
Legooolas
Yeah, no, it isn't.
Cirno de Bergerac
Best practices for C and C++ differ with respect to malloc(); in C, it's best *not* to cast the result, since doing so will mask a warning if there isn't a prototype for malloc() in scope (which happens more often than you think).
John Bode
+5  A: 

Added: The fasted solution to Arron's actual problem is provided by sgm in a comment. The text below is all accurate, and hopefully helpful, but a second rate solution to the problem at hand.


Your compiler is being very stiffnecked about pointer casts (are you using a c++ compiler?), adding an explicit cast like

sequence=(char**)malloc(sizeof(char *)*seqNum);

should make the error go away. Alternately you might be able to convince the compiler to go easy on you with some kind of option like

$(CC) --lighten-up-baby code.c

which might be preferable if this is in some third party code that you don't really want to hack. Read your compiler documentation to find the option you want. Since all the gccs I have on hand (versions 4.0 and 4.2) are happy with that code, I'm not in a good place to offer advice on switches to turn this behavior off.

dmckee
+1 exactly. C compilers don't complain but C++ compilers should.
Mehrdad Afshari
C99 compilers are more picky about typing than C89 compilers ever were, and gcc can be made extra-picky with switches like -pedantic
Legooolas
I am using gcc -o to compile the code on ubuntu. what you mentioned made that error go away. But now i am getting the same error on this line for(i=0;i<seqNum;i++) sequence[i]=malloc(sizeof(char)*seqLength); and doing same thing on this line is not helping...also, what do you suggest is the best way to compile c code on linux
The cast on the later line would be to a (char*), because that is the type of sequence[i]...
dmckee
If you're just creating a fixed-size two-dimensional array then you can do it in one malloc and assign it to the char **. Or if it's going to be a temporary then just declare it as char sequence[dim1][dim2] (as long as it's not too big to be on the stack :)
Legooolas