3D Array als Parameter übergeben
-
Hallo,
wie kann man ein 3D Array an eine Funktion übergeben?
int getVal(char pcID[][2][256]) { char pcTmpID[][2][256] = pcID; ... } int main() { char pcID[][2][256]; getVal(pcID); printf("Value:%s2\n", pcID[0][1]); ... }
Danke.
-
So:
void foo(char moep[][2][256]){} char bar[3][2][256]; foo(bar);
oder so:
void foo(char*** moep){} char bar[3][2][256]; foo((char***)bar);
-
Hallo shuriko,
in deiner main - Funktion ist ein Fehler enthalten:
Du gibst in deinem Beispiel nicht an, wie groß dein
3D-Array werden wird.char pcID[][2][256];
Wenn du die Anzahl kennst, trag sie hier ein.
Ansonsten würde ich vielleicht folgenden Typ verwenden:char ***pcID;
Hierbei musst du dann mit malloc() ein entsprechendes Array
anlegen.Hier habe ich es mal für dich gemacht:
#include <stdio.h> #include <stdlib.h> #include <string.h> int getVal(char ***pcID) { int i; for (i=0; i<10; ++i) { snprintf(pcID[i][0], 256, "Hallo %2d", i); } return 1; } int main (void) { char ***pcID; int i, j; pcID = malloc(10*sizeof(*pcID)); for (i=0; i<10; ++i) { pcID[i] = malloc(2*sizeof(*pcID[i])); for (j=0; j<2; ++j) { pcID[i][j] = malloc(256*sizeof(*pcID[i][j])); memset(pcID[i][j], 0x0, 256*sizeof(*pcID[i][j])); } } printf("size: %d\n", sizeof(*pcID)); getVal(pcID); for (i=0; i<10; ++i) { printf("%2d: >%s< >%s<\n", i, pcID[i][0], pcID[i][1]); } return 0; }
-
mcr schrieb:
Hierbei musst du dann mit malloc() ein entsprechendes Array
anlegen.Hier habe ich es mal für dich gemacht:
#include <stdio.h> #include <stdlib.h> #include <string.h> int getVal(char ***pcID) { int i; for (i=0; i<10; ++i) { snprintf(pcID[i][0], 256, "Hallo %2d", i); } return 1; } int main (void) { char ***pcID; int i, j; pcID = malloc(10*sizeof(*pcID)); for (i=0; i<10; ++i) { pcID[i] = malloc(2*sizeof(*pcID[i])); for (j=0; j<2; ++j) { pcID[i][j] = malloc(256*sizeof(*pcID[i][j])); memset(pcID[i][j], 0x0, 256*sizeof(*pcID[i][j])); } } printf("size: %d\n", sizeof(*pcID)); getVal(pcID); for (i=0; i<10; ++i) { printf("%2d: >%s< >%s<\n", i, pcID[i][0], pcID[i][1]); } return 0; }
also das ganze geht doch viel einfacher und zwar so:
int main () { char (*pcId)[2][256]; int anzahl = 2; // In anzahl steht die 1. dimension pcId = (char (*)[2][256]) malloc ( anzahl * sizeof ( *pcId ) ); /* Mache etwas */ return 0; }
int getVal(char pcID[][2][256]) { char pcTmpID[][2][256] = pcID; ... }
und in der Funktion getVal würde ich für den formellen parameter die äquivalente schreibweise zu char pcID[][2][256] nehmen und zwar char (*pcId)[2][256]
in der funktion selber musst du statt char pcTmpID[][2][256] char (*pcTmpID)[2][²56] nehmen.
-
Ok, ich schicke am Montag meine überarbeitete Variante. Das war toll, das ihr mir geholfen habt. Danke Leute!
-
Ok, hier ist meine Funktion. Übergabeparameter werden hier nicht kontrolliert etc.
Man übergibt: <KEY> <VAL> <INDEX>
#include <stdio.h> #include <string.h> #define BUF_SIZE 256 int nMapKeyValue (char *pcName, char *pcValue, int nIndex, char pc_KeyValue[][2][256]) { int j; int n_Base = 10; char pc_IndexBuff[3]; char *pc_ID; char *pc_Index; char *pc_Key; char *pc_Value; if(!(pc_Index = (char *)calloc(BUF_SIZE, 1))) { printf("Speicherallozierung fiel aus!"); goto END; } if(!(pc_Key = (char *)calloc(BUF_SIZE, 1))) { printf("Speicherallozierung fiel aus!"); goto END; } if(!(pc_Value = (char *)calloc(BUF_SIZE, 1))) { printf("Speicherallozierung fiel aus!"); goto END; } for (j = 0; j < nIndex; j++) { strncpy(pc_Index, (const char *)(itoa(j, pc_IndexBuff, n_Base)), 3); strncat((strcpy(pc_Key, pcName)), pc_Index, sizeof(pc_Key)); strncat((strcpy(pc_Value, pcValue)), pc_Index, sizeof(pc_Value)); pc_ID = (char *)&pc_KeyValue[j][0]; strcpy(pc_ID, pc_Key); printf("%10s %5s: %-20s\n", pc_Index, "KEY", pc_ID); pc_ID = (char *)&pc_KeyValue[j][1]; strcpy(pc_ID, pc_Value); printf("%10s %5s: %-20s\n", pc_Index, "VAL", pc_ID); } printf ("----------------------\n"); free(pc_Index); free(pc_Key); free(pc_Value); END: return 0; } int main(int argc, char *argv[]) { int nIndex = atoi(argv[3]); int nTmp = 0; char pc_ID[nIndex][2][256]; nMapKeyValue((char *)argv[1], (char *)argv[2], nIndex, pc_ID); while (nTmp < nIndex) { printf("%10i %5s: %-10s\n", nTmp, "KEY", pc_ID[nTmp][0]); printf("%10i %5s: %-10s\n", nTmp, "VAL", pc_ID[nTmp][1]); nTmp++; } return 0; }
Für eine elegantere Lösung bin ich sehr dankbar.