I would Like the following code to produce a structure like the one below the code. I do not get it from the code.
I want the code to create a 3D dynamic array that holds data as shown below such that when I want to print a specific string like printf("%s\n",szData[2][3]);
I get "string4"
. Also, I would like to get a specific character from a specific location e.g putchar(szData[0][3][2]
I get 'r'
.
Instead the code below prints out the last saved data despite specifying a location. For example printf("%s",szData[0][0]);
which should have been "string1"
it prints out "string2"
which is data for szData[2][1]
.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char***szData;
char temp[500];
int i,j,k,x,n,m,index;
printf("Rows:");
scanf("%d",&n);
szData=(char***)malloc(n*sizeof(char));
for(i=0;i<n;i++){
printf("Col: ");
scanf("%d",&m);
szData[i]=(char**)malloc(m*sizeof(char));
for(j=0;j<m;j++){
printf("string[%d][%d]=",i,j);
scanf("%s",temp);
szData[i][j]=(char*)malloc(25*sizeof(char));
szData[i][j]=temp;
}
}
printf("memmory[%d][%d]=%s\n",0,2,szData[0][2]);//Printing string from specific location
return 0;
}
Expected structure:
+----------+----------+----------+----------+
| 0 | 1 | 2 | 3 |
+----+----------+----------+----------+----------+
| 0 | string1 | string2 | string3 |string4 |
+----+----------+----------+----------+--------- +
+----------+----------+
| 0 | 1 |
+----+----------+----------+
| 0 | string1 | string2 |
+----+----------+----------+
+----------+
| 0 |
+----+----------+
| 1 | string1 |
+----+----------+
+----------+----------+----------+----------+
| 0 | 1 | 2 | 3 |
+----+----------+----------+----------+----------+
| 2 | string1 | string2 | string3 |string4 |
+----+----------+----------+----------+--------- +
+----------+----------+
| 0 | 1 |
+----+----------+----------+
| 2 | string1 | string2 |
+----+----------+----------+
+----------+----------+----------+----------+
| 0 | 1 | 2 | 3 |
+----+----------+----------+----------+----------+
| 2 | string1 | string2 | string3 |string4 |
+----+----------+----------+----------+--------- +
+----------+----------+
| 0 | 1 |
+----+----------+----------+
| 2 | string1 | string2 |
+----+----------+----------+
The code bellow works fine. Why not the above?
#include <stdio,h>
#include <stdlib.h>
#include <string.h>
int main()
{
char***szData;
char temp[25];
szData= (char *** )malloc(4 * sizeof(char ** )) ;
szData[0]=(char**)malloc(2*sizeof(char));
szData[0][0]=(char*)malloc(25*sizeof(char));
szData[0][0]="Henry\0";
szData[0][1]=(char*)malloc(25*sizeof(char));
szData[0][1]="Korir\0";
szData[1]=(char**)malloc(sizeof(char));
szData[1][0]=(char*)malloc(25*sizeof(char));
szData[1][0]="BSc.\0";
szData[2]=(char**)malloc(3*sizeof(char));
szData[2][0]=(char*)malloc(25*sizeof(char));
szData[2][0]="Software Engineering\0";
szData[2][1]=(char*)malloc(25*sizeof(char));
szData[2][1]="Kenyatta\0 ";
szData[2][2]=(char*)malloc(25*sizeof(char));
szData[2][2]="University\0";
szData[3]=(char**)malloc(sizeof(char));
szData[3][0]=(char*)malloc(25*sizeof(char));
szData[3][0]="From Kipsaiya, Kenya\0";
printf("szData[%d][%d]=%c size=%d\n",0,1,szData[2][0][0],sizeof(szData[0][1]));
strcpy(temp,szData[2][0]);
putchar(szData[0][0][0]);
printf("temp=%s size=%d\n",temp,sizeof(szData[2][0]));
return 0;
}