Home > Back-end >  Language used in the global variable int * * a, can be completed in a void type function of dynamic
Language used in the global variable int * * a, can be completed in a void type function of dynamic

Time:09-25

Because you want to use more matrix, for simple save trouble, then write a function, at the bottom of the show function of them in order to verify my function is correct, the other two, however, I attempt fails, the cause is unknown, please answer, thank you!
I'm calling from the original code below
# include
# include
Int * * a;
M * * a void creat (int, int, int n);
M * * a void MAC (int, int, int n);
Void show (int * * a, int m, int n);
Int main () {
Int m, n;
The scanf (" % d % d ", & amp; M, & amp; N);
Creat (a, m, n);
Show (a, m, n);
MAC (a, m, n);
return 0;

}
M * * a void creat (int, int, int n) {
int i;
A=(int) * * malloc (m * sizeof (int *));
for(i=0; iA [I]=(int *) malloc (n * sizeof (int));
}
M * * a void MAC (int, int, int n) {
int i;
for(i=0; iFree (a [I]);
Free (a);
}
M * * a void show (int, int, int n) {
int i,j;
for(i=0; ifor(j=0; jThe scanf (" % d ", a [I] + j).
for(i=0; ifor(j=0; jPrintf (" % d ", a [I] [j]);
If (jPrintf (" ");
The else
printf("\n");
}
}

CodePudding user response:

Not make parameter, argument

To make effective arguments, add a pointer, a call to a address

CodePudding user response:

Thank invited,
Don't think so complicated memory, you apply for a block of memory, and that is a continuous space
[at least from the process or the programmer's perspective, he is continuous, (may be physically must not continuous, but it doesn't matter, this kind of transformation, the operating system will help you complete)],
Yes, a long sequence of things,
Just you how to operate it is your thing, this is a person around things in programming,
Imagine a rope 4 X 5=20 cm, you fold it up and zigzag back into the fold, [every to 5 cm folded]

Assumption is not intuitive, you him at the Angle of cut, and then put flat [but you have to remember, they again in memory or continuous]
Now he is a 4 X 5 matrix
After all, this is done
 int * p=(int *) malloc (sizeof (int) * 20); 
for(int i=0; i <4. I++) {
For (int j=0; J & lt; 5; J++) {
I * * p++=5 + j;
}
}


But you not have obsessive-compulsive disorder, want to use a two-dimensional array,
Tell you, there is no such operation, unless you manually to construct such a thing like a compiler
A two-dimensional array int a [4] [5].
Is, in fact, what kind of things?
You don't think he is a simple double pointer to int * * p=a;
So you would be wrong, double pointer is not designed to operate two dimensional array, believe me from today began to give up the idea,
Two-dimensional array, use Pointers to express what is, is the array pointer, int (* p) [5]. This is not a novice can understand things,
, he is a pointer to an array type,
===
Under the C89 standard:
And the size of the array type must be specified, such as int (* p) [5]; The five must specify
You can't define this pointer int (* p) []; Or is the int (* p) [n].//n is specified at runtime
Because, for the compiler code that you write p++, he must know the address how many bytes have to jump back,
For the int (* p) [5], p++, is p leapt back five integers,
===

~ ~ ~
[see pointer is pointing at ordinary times int, char these basic types], not as you imagine, put N Pointers in the one-dimensional array
, if you have a [I] [j] this kind of thing always cherished,
Sorry C89 standard does not support the dynamic two-dimensional arrays, impossible,
C99 can support dynamic array, compile time plus - STD=C99,
If your compiler doesn't support for c99, or are you using vc, and if you don't know how to open the c99 compiler options, please forget him, don't do this kind of attempt,
Things weren't as complicated as you think, don't over think,,, to understand the essence,
Under the C99 compiler can run the following code, [most of the code copy directly to you, then I do change, according to your intention to achieve, you can experience]
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
 # include & lt; Stdio. H> 
#include
Void creat (void * * a, int m, int n);
Void MAC (void * a);
Void show (int m, int n, int (* a) [n]);

Int main ()
{
Int m, n;
The scanf (" % d % d ", & amp; M, & amp; N);
/* you can imagine why malloc pointer is void type,
Are programmers man-made gave him casts, optional kneading
What do you think he is round he is round, you think he is a flat he is flat
But, in essence, he is a line, is a continuous memory, -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- this */

Int (* a) [n].

Creat (* *) (void & amp; A, m, n);
Show (m, n, a);
MAC (void *) (a);
return 0;
}

/* */
Void creat (void * * p, int m, int n)//
{
* p=malloc sizeof (int) * m * n);//
}

Void MAC (void * a)
{
Free (a);//want to know, why is this free time without a double pointer,
//recall just memory, how to apply for a free corresponding to a malloc
}
Void show (int m, int n, int (* a) [n])
{
int i, j;
For (I=0; i For (j=0; J & lt; n; J++)
The scanf (" % d ", a [I] + j).

For (I=0; i For (j=0; J & lt; n; J++)
{
Printf (" % d ", a [I] [j]);
If (j & lt; N - 1)
Printf (" ");
The else
printf("\n");
}
}

Program execution results