Ich habe ein kleines Problem, vor allem Entschuldigung für mein Englisch, zweitens mache ich eine Matrix 2-D, aber ich habe ein Problem mit dem Arbeitsgedächtnis, Ich verstehe nicht, warum es nicht funktioniert. Der Code ist in C geschrieben. Ich möchte einen oder mehrere Strings setzen und in ein Array speichern, dazu muss ich die Kapazität des Arrays dynamisch erhöhen.2-D Matrix In C, realloc funktioniert nicht
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
erstellen Struct
typedef struct {
int rows;
int col;
int flagPosition;
int wordBiggest;
int tamanioString;
} matrixTest;
int insertString(char **matriz);
void rellenarMatriz(char **matriz, char string[], matrixTest *datosMatriz);
int getElementBiggest(char **matriz, int size);
Haupt
int main()
{
char *stringg = NULL;
insertString(&stringg);
free(stringg);
return 0;
}
In der nächsten Funktion initialisieren ich die Struktur und ich bin Laden der Zeichenfolge, diese Zeichenfolge dann schicke ich an eine andere Funktion "RellenarMatriz" genannt. Bis hier ist alles in Ordnung.
int insertString(char **matriz){
char __string[300];
int i = 0;
int sizeRow = 0;
matrixTest datosMatriz = { 1,1,0,0,0 };
matriz = (char **) calloc(datosMatriz.rows, sizeof(char *));
for (i = 0; i < datosMatriz.rows; i++)
matriz[i] = (char*)calloc(datosMatriz.col, sizeof(char));
puts("User say: ");
while(fgets(__string, 300, stdin)){
puts("User say: ");
i = 0;
sizeRow = 1;
while(i < strlen(__string) - 1 ){
if(__string[i] == ' ')
sizeRow++;
i++;
}
datosMatriz.tamanioString = strlen(__string) - 1;
datosMatriz.rows = datosMatriz.rows + sizeRow;
datosMatriz.flagPosition = sizeRow;
rellenarMatriz(matriz, __string, &datosMatriz);
}
return 0;
}
Hier ist das Problem .. die realloc und calloc Funktionen anschauen. Ich werde verrückt
void rellenarMatriz(char **matriz, char string[], matrixTest *datosMatriz){
int i = 0,
j = 0;
datosMatriz->wordBiggest = getElementBiggest(&string, datosMatriz->tamanioString);
if(datosMatriz->col < datosMatriz->wordBiggest){
(*matriz) = realloc((*matriz), (datosMatriz->wordBiggest) * sizeof(char *));
for(i = datosMatriz->col; i < datosMatriz->wordBiggest; i++){
matriz[i] = calloc(1, sizeof(char));
}
datosMatriz->col = datosMatriz->wordBiggest;
}
for(i = 0; i < datosMatriz->col; i++)
(*matriz) = realloc(matriz[i], (datosMatriz->rows) * sizeof(char *));
for(i = 0; i < datosMatriz->col; i++){
for(j = datosMatriz->rows-datosMatriz->flagPosition; j < datosMatriz->rows; j++)
matriz[i][j] = calloc(1, sizeof(char)); // Here is the biiig problem. Warning: assignment makes integer from pointer without a cast|
}
for(i = 0; i < datosMatriz->rows; i++){
for(j = 0; j < datosMatriz->col; j++){
printf("%d and %d\n", i, j);
matriz[i][j] = 'a';
}
}
for(i = 0; i < datosMatriz->rows; i++){
puts("\t");
for(j = 0; j < datosMatriz->col; j++)
printf("[%c]", matriz[i][j]);
puts("\n");
}
}
Diese nicht
int getElementBiggest(char **matriz, int size){
int i = 0,
biggest = 0,
countWord = 0;
for(i = 0; i < size; i++){
if((char)(*matriz)[i] == ' '){
if(countWord > biggest)
biggest = countWord;
countWord = 0;
}
else
countWord++;
}
return biggest;
}
auch keine Rolle, ob Sie einen Fehler in meiner Redaktion in Englisch finden, bitte sagen Sie mir, ich bin, dass beschlagen und wäre so gut dass du meine Syntax korrigierst.
'matriz = (char **) calloc (...' Oh, brechen Sie die Eingabe. – MikeCAT
'insertString' ist falsch. Sie ändern nur die lokale Variable. In 'rellenarMatriz' sind Sie eine ganze Dimension, die nicht synchron ist. Die Variable in 'main' muss' char ** 'sein. –
Ich empfehle Ihnen, Ihren Speicherzuweisungscode separat zum Code zum Füllen des Speichers zu schreiben. Dies erleichtert das Debuggen und Pflegen des Codes. –