Home > Software engineering >  Questions about the in VC6.0 create dynamic array
Questions about the in VC6.0 create dynamic array

Time:10-02

Such as topic, have an excel spreadsheet, saved a m * n in the table data, add code can be read out in VC6.0 m * n all of the data, now in order to realize such a function, dynamic load n1 and n2 within the scope of a column of data, and the data into into the dynamic array, where 0 & lt; N1 & lt; N2 & lt; M, and find out all the elements in the dynamic array and, n1 and n2 here is through the dialog box of the edit control input, and seen some routines, I use like int * p=new int [n2 - n1 + 1] way to create a dynamic array, thought is simpler, but read the data one by one assignment, only the first array element has a value, behind of no value, seems to be address values, again and again to find problems, there is still no solution, consult the expert in the BBS

CodePudding user response:

You create a one-dimensional array
According to your description I feel you should use a two-dimensional array

CodePudding user response:

Fyi:
 # include & lt; stdio.h> 
#include
#include
Int * * newarr2d (int rows, int cols) {
Int * * p, I;

P=(int) * * malloc (rows * sizeof (int *));
If (NULL==p) exit (1);
For (I=0; iP=(int *) [I] malloc (cols * sizeof (int));
If (NULL==p [I]) exit (1);
}
return p;
}
Void deletearr2d (int * * p, int rows) {
int i;

For (I=0; iFree (p [I]);
}
Free (p);
}
Int main () {
Int * * arr2d, I, j, r, c;

R=4;
C=5;
//in the heap open up a two-dimensional array of int 4 x5
Arr2d=newarr2d (r, c);
For (I=0; iFor (j=0; JArr2d [I] [j]=I * c + j;
}
}
For (I=0; iFor (j=0; JPrintf (" % 2 d ", arr2d [I] [j]);
}
printf("\n");
}
Deletearr2d (arr2d, r);

R=6;
C=3;
//in the heap open up a two-dimensional array of int 6 x 3
Arr2d=newarr2d (r, c);
For (I=0; iFor (j=0; JArr2d [I] [j]=I * c + j;
}
}
For (I=0; iFor (j=0; JPrintf (" % 2 d ", arr2d [I] [j]);
}
printf("\n");
}
Deletearr2d (arr2d, r);

return 0;
}
//0 1 2 3 4
//5 6 7 8 9
//10 11 12 13 14
//15 16 17 18 19
//0 1 2
//3, 4, 5
//6 7 8
//9 10 11
//12 13 14
//15 16 17
//

CodePudding user response:

reference 1st floor xianglitian response:
you create is a one dimensional array
According to your description I feel you should use a two-dimensional array


Absolute one-dimensional array will be enough, I just want to put a column in a table element, is an integer in the array, then the length of the array is through the dialog input of the difference of the two Numbers of the decision, that is to say, at runtime to determine the length of the array, the question now is, I use the new application of memory is only the first element has a value, then use the vector tried, also is such result, is only the first element has a value, value is the address at the back of the element, do you have this kind of situation

CodePudding user response:


refer to the second floor zhao4zhong1 response:
are for reference only:
 # include & lt; stdio.h> 
#include
#include
Int * * newarr2d (int rows, int cols) {
Int * * p, I;

P=(int) * * malloc (rows * sizeof (int *));
If (NULL==p) exit (1);
For (I=0; iP=(int *) [I] malloc (cols * sizeof (int));
If (NULL==p [I]) exit (1);
}
return p;
}
Void deletearr2d (int * * p, int rows) {
int i;

For (I=0; iFree (p [I]);
}
Free (p);
}
Int main () {
Int * * arr2d, I, j, r, c;

R=4;
C=5;
//in the heap open up a two-dimensional array of int 4 x5
Arr2d=newarr2d (r, c);
For (I=0; iFor (j=0; JArr2d [I] [j]=I * c + j;
}
}
For (I=0; iFor (j=0; JPrintf (" % 2 d ", arr2d [I] [j]);
}
printf("\n");
}
Deletearr2d (arr2d, r);

R=6;
C=3;
//in the heap open up a two-dimensional array of int 6 x 3
Arr2d=newarr2d (r, c);
For (I=0; iFor (j=0; JArr2d [I] [j]=I * c + j;
}
}
For (I=0; iFor (j=0; JPrintf (" % 2 d ", arr2d [I] [j]);
}
printf("\n");
}
Deletearr2d (arr2d, r);

return 0;
}
//0 1 2 3 4
//5 6 7 8 9
//10 11 12 13 14
//15 16 17 18 19
//0 1 2
//3, 4, 5
//6 7 8
//9 10 11
//12 13 14
//15 16 17
//


Miss zhao, I set up a separate file with a new and dynamic one-dimensional array can also be spread by vector do, is on I am not in the function, whether new or vector, is the first element has a value, the back of the element has no value, especially the vector, the element is behind the address values, I really can't find the reason, please the teacher for help

CodePudding user response:

A two-dimensional array vector Arr2d;

CodePudding user response:

Search COleSafeArray excel

CodePudding user response:

reference 5 floor zhao4zhong1 reply:
two-dimensional array vector Arr2d;


Miss zhao, I don't use a 2 d array, using a one-dimensional dynamic array, the question now is, loop assignment, on both the vector and the new method, is only the first element has a value, value is the address at the back of the element, there should be no allocated memory? I set up a separate file, paste casually online, a similar code execution is no problem, but put this code in my function is not line, I haven't find out why, this is my confusion, is looking forward to your reply

CodePudding user response:

refer to 6th floor zgl7903 response:
search COleSafeArray excel


I have all data can be read from the excel in, is now in a problem when I create a one-dimensional dynamic array, as if the memory not application is successful, see to miss zhao's reply, thank you for your answer

CodePudding user response:

To set breakpoints view element number is how many?

CodePudding user response:

reference yunfei_21 reply: 3/f
Quote: refer to 1st floor xianglitian response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related