Home > Back-end >  About the use of multidimensional pointer
About the use of multidimensional pointer

Time:10-07

Pure C due to work requirement, so the brush with C topic
If inscribe http://https://leetcode-cn.com/problems/triangle/
 
The official answer key
* * triangle int minimumTotal (int, int triangleSize, int * triangleColSize) {
Int f [triangleSize] [triangleSize];
Memset (f 0, sizeof (f));
F [0] [0]=triangle [0] [0].
For (int I=1; I & lt; TriangleSize; + + I) {
[I] [0]=f f [0] + [I - 1] triangle [I] [0];
For (int j=1; J & lt; i; + + j) {
F [I] [j]=fmin (f [I - 1] [1], [I - 1) [j] f) + triangle [I] [j];
}
[I] [I] f=f [I - 1] [I - 1) + triangle [I] [I];
}
Int ret=f [triangleSize - 1] [0];
For (int I=1; I & lt; TriangleSize; I++)
Ret=fmin (ret, f [triangleSize - 1] [I]);
Return ret.
}

Questions as follows:
1. The answer key to use the c language is a standard c? Why can use the variable initialization array length
2. The triangle is two-dimensional pointer, how is ability of triangle [j] operation [I]? Vs can't run, the reason is that the compiler doesn't know how much * triangle array length, can not find the triangle [I] [j], and the following is a possible, created a one dimensional array pointer, each one dimensional pointer pointing to a separate memory space
 
Int cache=(int) * * * * malloc (sizeof (int *) * triangleSize);
For (int I=0; I & lt; TriangleSize; I++)
{
Cache [I]=(int *) malloc (sizeof (int) * triangleColSize [I]);
}

3. Aimed at the problems in question 2, similar to the situation of the subject, how to gracefully to a multidimensional array using an array subscript to operation,

Attach my code
 
* * triangle int minimumTotal (int, int triangleSize, int * triangleColSize)
{
Int cache=(int) * * * * malloc (sizeof (int *) * triangleSize);
Int triangle_p=(int) * * * * malloc (sizeof (int *) * triangleSize);
Int res=99999999999;
If (1==triangleSize)
Return triangle [0] [0].
else if (! TriangleSize)
return -1;
For (int I=0; I & lt; TriangleSize; I++)
{
Cache [I]=(int *) malloc (sizeof (int) * triangleColSize [I]);
Memset (cache [I], 0, sizeof (int) * triangleColSize [I]);

}
Cache [0] [0]=triangle_p [0] [0].//run here collapse
For (int I=1; I & lt; TriangleSize; I++)
{
For (int j=0; J & lt; TriangleColSize [I]; J++)
{
If (0==j)
{
Cache [I] [0]=cache [0] + [I - 1] triangle_p [I] [0];
}
The else
{
Cache [I] [j]=MIN (cache [j], [I - 1] cache [I - 1]] [j - 1) + triangle_p [I] [j];
}
}
}
For (int I=0; I & lt; TriangleColSize [triangleSize - 1); I++)
{
Res=MIN (res, cache [triangleSize - 1] [I]);
}
return res;
}


CodePudding user response:

refer to the original poster ShengZu lv response:
questions as follows:
1. The answer key to use the c language is a standard c? Why can use the variable initialization array length
2. The triangle is two-dimensional pointer, how is ability of triangle [j] operation [I]? Vs can't run, the reason is that the compiler doesn't know how much * triangle array length, can not find the triangle [I] [j], and the following is a possible, created a one dimensional array pointer, each one dimensional pointer pointing to a separate memory space
 
Int cache=(int) * * * * malloc (sizeof (int *) * triangleSize);
For (int I=0; I & lt; TriangleSize; I++)
{
Cache [I]=(int *) malloc (sizeof (int) * triangleColSize [I]);
}

3. Aimed at the problems in question 2, similar to the situation of the subject, how to gracefully to a multidimensional array using an array subscript to operation,
Cache [0] [0]=triangle_p [0] [0].//run here collapse

1, this is the C99 standard grammar, using variable initialization array length and the local scope of array called a variable-length array
2, the triangle is two-dimensional pointer, how is ability of triangle [j] operation [I]? This is the basic operations of a two dimensional array, triangle [I] is a pointer, triangle [I] [j] is the element
3, obviously you only from the cache of the two-dimensional distribution of space, without access to triangle_p distribution of two-dimensional space is wrong, of course

CodePudding user response:

C99 supports dynamic array
Vs errors because of his support for c99 standard incomplete

CodePudding user response:

LS said, add
2 [triangle is two-dimensional pointer, how is ability of triangle [j] operation [I]? Vs can't run, the reason is that the compiler doesn't know how much * triangle array length, can not find the triangle [I] [j]], if so, your code
If (1==triangleSize)
Return triangle [0] [0].//why can operate? I=0, j=0

So the problem is not the compiler know not to know how many length, the length is your guarantee, if the length can't guarantee, the compiler will only an array of illegal operation, so the parameters of the validity of the pointer and array length is yourself to ensure that, because the compiler doesn't know what runtime is the incoming parameters, so as long as your grammar, you can compile,

3 your cache [I] know malloc allocates memory, why there is no thought of triangle_p [I] don't allocate memory? So triangle_p [0] [0]; Is illegal,

CodePudding user response:

Sorry to stick the wrong code above is I debug the code, the actual triangle_p this pointer
 
* * triangle int minimumTotal (int, int triangleSize, int * triangleColSize)
{
Int cache=(int) * * * * malloc (sizeof (int *) * triangleSize);
Int res=99999999999;
If (1==triangleSize)
Return triangle [0] [0].
else if (! TriangleSize)
return -1;
For (int I=0; I & lt; TriangleSize; I++)
{
Cache [I]=(int *) malloc (sizeof (int) * triangleColSize [I]);
Memset (cache [I], 0, sizeof (int) * triangleColSize [I]);

}
Cache [0] [0]=triangle [0] [0].//run here collapse
For (int I=1; I & lt; TriangleSize; I++)
{
For (int j=0; J & lt; TriangleColSize [I]; J++)
{
If (0==j)
{
Cache [I] [0]=cache [0] + [I - 1] triangle_p [I] [0];
}
The else
{
Cache [I] [j]=MIN (cache [j], [I - 1] cache [I - 1]] [j - 1) + triangle_p [I] [j];
}
}
}
For (int I=0; I & lt; TriangleColSize [triangleSize - 1); I++)
{
Res=MIN (res, cache [triangleSize - 1] [I]);
}
return res;
}


CodePudding user response:

The
reference 3 floor qybao response:
LS said, add
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related