[C]malloc Arraypointer

Razorhawk

Webdesigner und MSP
Hallo

Wenn ich einen Arraypointer reservieren wollte mit malloc, welcher 3*6 Werte speichern will, wieviel muss ich dann als Platz allocieren in malloc?

Danke!
 
Hallo wie wäre es so:

Code:
const int SIZE = 18;
int* array = (int*)malloc(sizeof(int) * SIZE);

oder als 2 dimensionales Array:

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

int main(){
    const int X_SIZE = 3;
    const int Y_SIZE = 6;
    int** array = (int**)malloc(sizeof(int*) * X_SIZE);
    int i = 0;
    for(i = 0; i < X_SIZE; i++){
        *array = (int*)malloc(sizeof(int) * Y_SIZE);
        memset(*array, 0, sizeof(int) * Y_SIZE);
    }
    //freigeben nicht vergessen
    return 0;
}

Gruß

RedWing
 
Zuletzt bearbeitet:
Zurück