How to dynamically define a 2 dimensional array in c

Updated on technology 2024-03-11
11 answers
  1. Anonymous users2024-02-06

    The method for dynamically defining a 2-dimensional array in C++ is:

    Define a two-dimensional pointer.

    Assign an array of Line Pointers.

    Assign an array of data to each row.

    Use two-dimensional arrays.

    Release the data array for each row (the memory is freed manually when the memory is no longer in use).

    Release the Row Pointer array.

    #include

    #include

    using namespace std ;

    #define row 5

    #define col 10

    int main()

    int **pp,i,j;

    pp=new int *[row];Assign an array of row pointers and return the first address of the array to pp

    for( i=0;i

    C++ dynamically defines a 2-dimensional array, in effect, assigning an array of pointers first, and then assigning a corresponding array of data to each array of pointers. The difference from a true two-dimensional array is:

    The storage space of a two-dimensional array is continuous in memory, and it is actually a one-dimensional array, but the one-dimensional array is divided into n segments; In the case of a dynamically allocated two-dimensional array, the row pointer array is a continuous one-dimensional array, where one-dimensional pointers are stored, and these pointer values are not necessarily continuous, but are related to memory usage.

    It can also be seen that dynamic two-dimensional arrays have one more row of pointer arrays of excess memory than true two-dimensional arrays.

    The column width of each row of a dynamically defined two-dimensional array can be variable (usually the same width); A true two-dimensional array must have the same column width per row.

    A true two-dimensional array is automatically released after the end of its defined function; Dynamically defined two-dimensional arrays that need to be used for memory release with explicit **.

  2. Anonymous users2024-02-05

    Start by defining a pointer.

    For example: int * a;

    Then define the array when needed:

    a =new int[8][8];

    It is to define a two-dimensional array of 64 numbers. It can also be like this:

    a =new int[8]

    It defines a two-dimensional array with an indefinite number of columns, and the array name is a, such as a[0][2].

  3. Anonymous users2024-02-04

    1. new dynamically define the array.

    2. Because new is used to dynamically open up space, it can of course be used to open up an array space. In this way, the following statement:

    int size=50;

    int *p=new int[size];That's right.

    3. But can two-dimensional dynamic arrays also be defined in this way?

    int size=50,column=50;

    int (*p)[column]=new int [size][column]

    1) First of all, new int[size][column] is determined when it is dynamically generated, so it is not wrong, then it is int(*p)[column], this sentence is problematic, why is this sentence wrong, that is because, this is a definition statement, and the definition statement is compiled by the compiler first, when the compiler runs here, it finds that the column is not a constant, so it cannot be compiled.

    2) The reason why the compiler thinks that column is not a constant is because in the compilation phase, the compiler's role is to check for syntax errors, and pre-allocated space, it does not execute the program, therefore, it does not execute the assignment statement (it only checks for errors on this statement, and allocates space), so in the compilation phase, it will consider column to be a variable. So the above definition of a two-dimensional array is wrong, it can't be compiled.

    Change it to this: int size=50

    int (*p)[50]=new int [size][50]

    That's right.

  4. Anonymous users2024-02-03

    1. Let's talk about two-dimensional first, which can be understood in this way

    int n[3] has 3 ints

    Then, int m[5][3] has 5 int[3] assigned values:

    n[1] = 3 give 3 to 1.

    m[4]=m[4] is an int[3] type, so assigned, that is, so assigned:

    The [0] of m[4] is 5: m[4][0]=5

    m[4][1]=9

    m[4][2]=4

    int k[4][5][3] has 4 int[5][3]2, example:

    int***creategrid(int m,int n,int t)

    int***tt = null;

    tt = (int***malloc(sizeof(int)*m);

    for(int i=0;i

  5. Anonymous users2024-02-02

    #include

    int **creategrid(unsigned m, unsigned n, unsigned t);Create an integer 3D array

    void freegrid(int **c1,unsigned m,unsigned n);Release the shaping 3D array

    static void free1(int **c1,unsigned n);

    static void free2(int **c1,unsigned m,unsigned n,unsigned k);

    int **creategrid(unsigned m, unsigned n, unsigned t) *create an integer 3D array*

    if(i!=m) *2D allocation failed, freeing up previously allocated memory*

    for(i=0;i!=m;++i) Assign the third dimension

    if(j!=n)

    break;

    if(j!=n) *3rd Dimension Allocation Fails, Releasing Previously Allocated Memory*

    return c1;

    static void free1(int **c1,unsigned m) *free the first m pointers*

    static void free2(int **c1,unsigned m,unsigned n,unsigned k)

    void freegrid(int **c1,unsigned m,unsigned n) *free memory*

  6. Anonymous users2024-02-01

    The following correctly defines a two-dimensional array -- the following correctly defines an array (minus the two-dimensional word).

    a) int a[ ]3];The syntax is wrong, there is no initialization, [blank] is not allowed, and the size of the array is not indefinite.

    b) int a[ ]= --The array is correctly defined, it is a one-dimensional array, the array has only 1 element, and the initialized value is 2*3, which is 6.

    c) int a[ ]3]=;- Syntax error, the curly braces are not filled with initialization values.

    d) int a[2][3]=,,} syntax error, the array is declared as 2 rows and 3 columns, but initialized to 3 rows and 2 columns. If it's int a[3][2]=,,} the syntax is correct.

    So the answer is b

  7. Anonymous users2024-01-31

    First of all, let's make sense that a two-dimensional array should be a pointer.

    A because I don't know the exact size, so the compilation error C is the same as A, and D is due to a problem with assigning a two-dimensional array.

    b is a pointer.

    No matter what the array is, it's a continuous memory space, so there's a corresponding entry address, and this understands, no matter what the array is, it's like this, and the two-dimensional is actually a one-dimensional, but I think it's convenient.

  8. Anonymous users2024-01-30

    Are you sure you're not mistaken? int a= is a one-dimensional array!

    Two-dimensional arrays are neither.

    a, just give the initial value.

    c is not written like this, and the compilation is wrong.

    d.

  9. Anonymous users2024-01-29

    First of all, you need to understand the true meaning of a two-dimensional array, the first dimension is the starting address of a set of data, and the second dimension is a certain value in a certain set of data. Secondly, you need to know that a[4] means that each dimension of a two-dimensional array a is a one-dimensional array of four elements. The compiler can calculate the length of the first dimension based on the number of initial values you assign to a (the total number of elements is 4, if it is not divisible, the length is increased by 1, and there should be several array elements without initial values).

    The meaning of a[3] is superficially true, but the problem is that the compiler cannot directly identify the exact length of its second dimension (you can consider that if the total number of initial values is 2, the second dimension length of a can be considered to be 1 or 2,..).This will produce an error. Therefore, it can be defined as a[4]= but not a[3]=.

    As for the question you added, I have actually already talked about it. Because the initial value is 5 elements, 5 4 = 1, but it cannot be divided, so 1 should be added, that is, the actual array a is essentially defined as a[2][4], and its inner elements are:

    5000 (the three zeros after it are the system defaults).

    Hope it helps. ^-

  10. Anonymous users2024-01-28

    Since it is created dynamically, the number of rows and columns of the created two-dimensional array should be "universal", that is, the "user" should determine the size of the two-dimensional array. It's difficult. Because two-dimensional arrays need to involve row pointers, and the number of column elements in each row needs to be fixed, there is no way to achieve "universality" with this idea.

    For this reason, it must be clearly understood that the elements of the so-called two-dimensional array are still stored in one-dimensional mode in memory. Truth be told, two-dimensional arrays don't exist in nature.

    That being the case, it is sufficient to "construct" a so-called two-dimensional array, as long as it provides sufficiently useful functions (tools) to manipulate the two-dimensional array.

    To sum up, accept the values of the rows and columns of the two-dimensional array determined by the user, apply for the space of a one-dimensional array, and locate and operate according to the two-dimensional array method.

    For the sake of simplicity, I've used the following definition, and you can change the types to suit your needs:

    typedef int user_type;

    Defines a "control head" that controls a two-dimensional array (matrix) and consists of three members.

    typedef struct matrix

    matrix;

    matrix *initmatrix(int row, int col);Initialize the two-dimensional array.

    user_type getelementat(matrix matrix, int i, int j);Obtain the element that specifies the subscript.

    void setelementat(matrix matrix, int i, int j, user_type val);Assign a value to an element that specifies a subscript.

    void destroymatrix(matrix *matrix);Destroy the two-dimensional array.

    void destroymatrix(matrix *matrix)

    void setelementat(matrix matrix, int i, int j, user_type val)

    user_type getelementat(matrix matrix, int i, int j)

    matrix *initmatrix(int row, int col)

    Use the above functions as tools to complete a series of operations such as two-dimensional array initialization, assignment, and value taking, and if you need to compare, you also need to write a comparison function.

  11. Anonymous users2024-01-27

    (1) The second dimension is known.

    char (*a)[n];pointer to array a = (char (*n])malloc(sizeof(char *)m);

    printf("%d", sizeof(a));4. Pointer printf("%d", sizeof(a[0])) n, a one-dimensional array.

    free(a);

    2) The first dimension is known.

    char* a[m];array of pointers int i;

    for(i=0; i

    3) Both dimensions are unknown.

    char **a;

    int i;

    a = (char **malloc(sizeof(char *)m);Assign an array of pointers.

    for(i=0; i

Related questions
11 answers2024-03-11

Here's how.

If the QQ space is not displayed (not updated), it is recommended to try to clear the IE cache and reopen it to view. If you clear the IE cache and still do not display (do not update) the space dynamics, please refer to the following information: >>>More

8 answers2024-03-11

Brief steps: win--> settings --> Control Panel -->Accessibility Options--> mouse -->m-->s --optional) --a (application). >>>More

13 answers2024-03-11

1. For us, CAD does not display line width by default, which means that when we use CAD drawing, the thickness of the drawn lines is the same. First, we click on Format above the toolbar. >>>More

21 answers2024-03-11

Hi! Dear, it is recommended that you go to Xiaomi after-sales service to test the authenticity of Xiaomi authorized repair points. >>>More

12 answers2024-03-11

How to type out the underline in word Teacher Shi: There are not only underlines in word, sometimes we also need to underline, so how to type out the underline in word. For example, the input method for the effect above: >>>More