-4

Is someone possible to explain me what I am doing wrong here? I am trying to malloc "multi-dimensional" dynamic array like this.

Thank you

enum { MAX_WORDS = 100, MAX_LENGHT = 20 };

char **words;

// it fails here "void * cannot be assigned... type of char**"
words = malloc(MAX_WORDS * sizeof(char*));

for (int i = 0; i < MAX_WORDS; i++) {
    words[i] = malloc(MAX_LENGHT * sizeof(char));
}
Jax-p
  • 7,225
  • 4
  • 28
  • 58
  • 2
    Why do you think something is going wrong here? – n. m. could be an AI Feb 15 '16 at 14:09
  • Also, why `sizeof(int)`? – Iharob Al Asimi Feb 15 '16 at 14:09
  • And why do you use `char**` when you want to allocate an array of of arrays of `int`? Besides that (and the misspelling which I guess is from you *retyping* the code here on SO instead of copy-pasting an [MCVE](http://stackoverflow.com/help/mcve)) there's nothing wrong with the code you show. Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) containing the problem and show us. Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Feb 15 '16 at 14:09
  • 1
    Are you sure it is [tag:c]? I guess it is [tag:c++].. If yes, use `new` and `delete` instead of `malloc` and `free`. – LPs Feb 15 '16 at 14:12
  • Check what `malloc()` returns. – CinCout Feb 15 '16 at 14:14
  • @Joachim Pileborg thanks for tips, I misspelled some variables when I was translating. I'll take a look on these links; – Jax-p Feb 15 '16 at 14:16
  • A pointer is not an array! You cannot have a 2D array with `char **`! – too honest for this site Feb 15 '16 at 14:16
  • If you just *always* cast the result of `malloc` you can translate the same source as C or C++. – Peter - Reinstate Monica Feb 15 '16 at 14:16
  • 1
    @PeterA.Schneider Except that in C [you should not really cast the return of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) (or any other function returning `void *`). – Some programmer dude Feb 15 '16 at 14:17
  • @Joachim you are faster than unwind! I just beg to disagree (and was in a trolling mood -- sorry for that.)# – Peter - Reinstate Monica Feb 15 '16 at 14:18
  • @HappyCoder I doesn't let me compile it. I don't know hot to var_dump in C :/ – Jax-p Feb 15 '16 at 14:19
  • @PeterA.Schneider Why do you think to use `malloc` in this case with [tag:c++]? – LPs Feb 15 '16 at 14:19
  • @JaxCze Could you post the compile command, please? – LPs Feb 15 '16 at 14:19
  • @olaf How should I? I am using this [How do I work..](http://stackoverflow.com/questions/917783/how-do-i-work-with-dynamic-multi-dimensional-arrays-in-c) as example – Jax-p Feb 15 '16 at 14:20
  • @PeterA.Schneider Lets just say you forget to include `` (or any other header file which includes it) meaning that `malloc` is not declared. Sure the compiler will probably put up a warning, but "hey I just add a cast and the warning goes away". Now, what do you think happens on a 64-bit machine where pointers are 64 bits, but `malloc` has been implicitly declared by the compiler to return `int` which is 32 bits? – Some programmer dude Feb 15 '16 at 14:20
  • 1
    @Joachim for the better of a generation C compilers warn you if you use undeclared functions. And rest assured that I have read the relevant discussions and just don't think the reasons to *not* cast are compelling. It's more a matter of taste than anything, and code *does* get ported to C++, where the C type unsafeties are a nuisance. – Peter - Reinstate Monica Feb 15 '16 at 14:21
  • 2
    @PeterA.Schneider And for the better of a generation (and more) warnings have either been casted to silence or generally ignored, both by newbies and experienced programmers alike. Because hey, the compiler finished and created a program so it must work right? Also, using C-style casts in C++ is really something I discourage. – Some programmer dude Feb 15 '16 at 14:23
  • 1
    @JoachimPileborg You cannot cast away the warning which tells you that the return value of an undeclared function is assumed to be int. And while I find that some of the things people have strong opinions about are irrelevant or matters of taste (and they get pointed out because the critics have nothing substantial to contribute), heeding thy warnings is not one of them. Much harm could have been prevented if people had payed better attention to the tools they use. – Peter - Reinstate Monica Feb 15 '16 at 14:29
  • @JaxCze: Yes, the two most rated answers are blatanically wrong. See the answer of JensGustedt and my comment. – too honest for this site Feb 15 '16 at 14:30
  • 1
    @JaxCze Since you are clearly using a C++ compiler I suggest you learn to use C++ fully, I urge you to check [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to find a good beginners book. As for your problem, there's nothing you show that warrants dynamically allocating memory, as the sizes are both compile-time constants you can simply declare an array of arrays: `char words[MAX_WORDS][MAX_LENGTH];` – Some programmer dude Feb 15 '16 at 14:34
  • @Olaf I see only one answer (I may have missed the window of existence for the other). Why is the answer from Jacob wrong? It may be unsatisfying (one should use C++ features) or of questionable style (for people who have strong opinions about casting the result of malloc), but wrong? – Peter - Reinstate Monica Feb 15 '16 at 14:36
  • @PeterA.Schneider: There are at least four answers on the linked question! (You apparently missed the context) And about the answer of Jacob here, I already commented why it is clearly wrong. – too honest for this site Feb 15 '16 at 14:39
  • @Olaf re-iterating our discussion I understand only now that "blatanically wrong" referred to the answers to a different post mentioned by JaxCze in a comment (I wondered, as I stated, where the "second answer" was). I found the wording (which targeted a different answer) strong for Jacob's answer which was technically correct, so I intervened. (And that was the reason why I commented here and not there, in the other thread.) – Peter - Reinstate Monica Feb 15 '16 at 15:34

1 Answers1

1
words = malloc(MAX_WORDS * sizeof(char*));

Should read

words = (char**)malloc(MAX_WORDS * sizeof(char*));

Your compiler is mad because you are attempting to assign a void pointed to a char pointer, so you need to typecast it to char** to work properly.

EDIT: Apparently this is because I am using a C++ compiler. In C you should not be casting the result of malloc(). If you are using C++ you should switch to new and delete if possible.

Jacob H
  • 864
  • 1
  • 10
  • 25
  • 2
    Your *C++ compiler*. :-) – Peter - Reinstate Monica Feb 15 '16 at 14:17
  • I am possible to use C++ compiler. This works, thank you :) – Jax-p Feb 15 '16 at 14:22
  • 3
    @JaxCze You are *already* using a C++ compiler, otherwise you would not get the error you have. – Some programmer dude Feb 15 '16 at 14:27
  • 1
    No, it should not! This answer is wrong for C language. And for C++, you should use a `static_cast` at least, but actually no `malloc` at all. – too honest for this site Feb 15 '16 at 14:34
  • 1
    @Olaf I read your comment carefully ("This answer is wrong for C language"). In what sense do you think it is wrong? Does it not compile? Does it not link? Does it crash? Does it produce unintended results? Does it violate language standards? Afaics none of the above. It may be unsatisfying (one should use C++ features) or of questionable style (for people who have strong opinions about casting the result of malloc, and yes, one should use static_cast), but wrong? Of course Joachim said all there is to say (short of suggesting a book directly -- possibly Lippman e.a.'s C++ Primer). – Peter - Reinstate Monica Feb 15 '16 at 14:45
  • 5@PeterA.Schneider: I will not discuss this any further with you, do some research on the subject. Apparently you don't want the compiler to do part of his job by type-checking. A very general rule in any programming language is not to cast without actual need. And the question is tagged C, there is no hint it is C++ and OP does not provide the actual error. Thus, it could as well have been closed for not showing a reproducible problem. – too honest for this site Feb 15 '16 at 14:50
  • 1
    @Olaf, OP is using a C++ compiler for C code. I reproduced the error on my machine compiling with g++. OP asked for C code that would fix his problem, I responded with what he asked for. The note at the bottom of my post even says that my answer should not be used in C++. – Jacob H Feb 15 '16 at 14:52
  • @JacobH: Then it is no C code! Identical syntax does not imply identical semantics or language. – too honest for this site Feb 15 '16 at 14:52
  • 1
    @Olaf I never said it was, I think that OP should switch to a C compiler or be using new/delete instead. I was only responding to the problem he posed with an answer that solved his problem. – Jacob H Feb 15 '16 at 14:54
  • @JacobH: With the information given in the question (see [ask]!), your answer is wrong. I'm very confident I clarified that more than twice now and don't see much sense in repeating the same fact over and over again. – too honest for this site Feb 15 '16 at 14:58
  • The correct _C99_ solution, in my opinion, is the [answer by Jens Gutstedt](http://stackoverflow.com/a/12805980/1566221) to the question noted in a comment by @JaxCze. Although personally I would emphatically not use fixed-length arrays in this context; an `argv`-style list of C-strings of different lengths is more likely to be useful. – rici Feb 15 '16 at 15:21