-6

Just trying to use a simple macro which reserves memory for a 2D array and initialize every the whole array with one value, but gcc throws this error :

2DMakro.c:39:5: error: expected expression
ALLOC_ARRAY2D(array, int, dim1Makro, dim2Makro, initMakro);

#include <stdio.h>
#include <stdlib.h>

#define ALLOC_ARRAY2D(ARRAY,TYPE,DIM1,DIM2,INIT)\
    (TYPE **ARRAY;\
    ARRAY = (TYPE**) malloc(DIM1*sizeof(TYPE));\
    for(int zeile = 0; zeile < DIM1; zeile++) {\        
    }\
    for(int a = 0; a < DIM1;a++){\
        for(int b = 0; b < DIM2; b++){\
            ARRAY[a][b] = INIT;\
        }\
    }

#define DEL_ARRAY2D(ARRAY,DIM1)\
    (for int zeile = 0; zeile < DIM1 ; zeile++) {\
        for(int spalte = 0; spalte < DIM2 ; spalte++){\
            //Gibt Speicherplatz frei   
            free(ARRAY[zeile]);\
        }\
    })


int
main(int argc, char *argv[]) {
    //Variabeln
    char *type,*dim1,*dim2,*init;
    char* dim1P, dim2P,initMakroP;
    int dim1Makro, dim2Makro, initMakro;

    //char zu integer konvertieren
    dim1Makro = atoi(argv[1]);
    dim2Makro = atoi(argv[2]);
    initMakro = atoi(argv[3]);

    ALLOC_ARRAY2D(array, double, dim1Makro, dim2Makro, initMakro);
    DEL_ARRAY2D(ARRAY, DIM1);
    return 0;
}

Well and this is my .c file, any suggestions what I did wrong? Thanks in advance.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Stan
  • 243
  • 2
  • 4
  • 12
  • 6
    If this is a simple macro, I don't want to see a complicated one. – Potatoswatter May 28 '15 at 08:43
  • 1
    Since there is only one `malloc` in there, how is the compiler supposed to know what `ARRAY[a][b]` means? – Weather Vane May 28 '15 at 08:48
  • Shouldn't it be either `malloc(DIM1*DIM2*sizeof(TYPE));` or `malloc(DIM1*sizeof(TYPE*));` with further `malloc` for each row? – Weather Vane May 28 '15 at 08:53
  • @WeatherVane mhm could you explain it a bit further ? – Stan May 28 '15 at 08:53
  • 2
    Sorry, but you _must_ re-write this code from scratch. It doesn't make any sense what-so-ever to use a macro for this. It is considered very poor programming practice, which is why people get agitated. You are also [not allocating a 2D array](http://stackoverflow.com/questions/30117161/why-do-i-need-to-use-type-to-point-to-type) and if your intention is not to allocate a 2D array but a segmented pointer-to-pointer mess, you are not doing it right anyhow. Also [don't cast the return value of malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Lundin May 28 '15 at 08:53
  • As @Lundin says, you need to get it right "in clear" before you attempt this horrible practice. – Weather Vane May 28 '15 at 08:55
  • @Lundin I get your point, but still if they had tried to explain it like you just did I would get it, no need for such harshness, i'm trying to learn from my mistakes. – Stan May 28 '15 at 08:57

1 Answers1

4

The comment //Gibt Speicherplatz frei is interrupting the macro. Comments in multi-line macros must be /* like this */ \.

Consider using inline functions instead, or anything else. The C preprocessor is a programming environment where even comments can be dangerous.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • 1
    While writing macros instead of inline functions is 1980s dinosaur practice, writing inline functions is 1990s dinosaur practice :) Instead let the compiler do the inlining for you, as it is nowadays far better than the programmer at determining when and what to inline. The inline keyword is pretty much obsolete for this reason. – Lundin May 28 '15 at 08:57
  • @Lundin Most build systems AFAIK still do not do the whole-program optimization necessary to move functions across translation units. You might be thinking of cases in C++ where the `inline` keyword is implicit. See also http://stackoverflow.com/questions/5987020/can-the-linker-inline-functions . – Potatoswatter May 28 '15 at 09:14
  • What I'm saying is that the compiler may inline functions even if you don't tell it to, and it doesn't have to inline functions that you explicitly declared as inline either (the inline keyword is just a hint saying that "this function might be good to inline"). The bottom line is that 99% of all function inlining attempts we see here on SO is pre-mature optimization and the programmers should get their priorities straight, such as for example: 1) make a sound program design, 2) write bug-free code, 3) write readable code, .... 65535) consider if inlining is necessary to improve performance. – Lundin May 28 '15 at 09:34