#include <stdio.h>
char *c[] = {"ENTER", "NEW", "point", "I"};
char **cp[] = {c+3, c+2, c+1, C};
char ***cpp = CP;
Main ()
{
printf ("%s#", **++cpp);
printf ("%s#", *--*++cpp);
printf ("%s#", *cpp[-2]+3);
printf ("%s#", cpp[-1] [-1] + 1);
}
char *c[] = {"ENTER", "NEW", "point", "I"};
This must be understood by defining a few strings and then putting them into an array, because the array has a string in it, so the array type must be char*.
when we define a string, we often write char* str = "Hello world";
So when we're going to define multiple strings, we can use arrays, like the one above.
char **cp[] = {c+3, c+2, c+1, C};
This is a double pointer. Because the C defined in front is the array type, which is a constant pointer (address), C+3,c+2,c+1,c is the address of each element in array C. Now to put the address of an array element in an array, what data type should be used? where a variable (or a string) address is needed, a pointer is required, and the address of an address requires a double pointer (because only the pointer can hold the address, here is the address of an address, so use the double pointer).
char ***cpp = CP;
The CP is a constant pointer, plus his type is char**, so it's a three-level pointer. Here the type of CPP is char***, is also a three-level pointer, so the CP directly assigned to the CPP there will be no problem (the same type of it (all are three-level pointers)).
printf ("%s#", **++cpp);
+ + has a high priority, so first execute ++cpp, which is to get the second element address of the array CP.
Two * * We look separately, see *++cpp First, this is to get the value of the second element of the array CP c+2. That is, the * (address) points to the content stored by the address (that is, the address of a variable or another variable, or a pointer), or an address that holds a variable address (a pointer to a pointer) ...).
looking at the **++cpp, the equivalent of * (C + 2), so here is the first address of the third element of array C, which is the first address of the "point" string, so the output point# here, and the execution of the statement causes the current point of the CPP to change.
printf ("%s#", *--*++cpp);
by the priority and the relationship between the execution order, we will first execute ++cpp, because the previous output statement has led to CPP plus 1, so now the CPP plus 1, then the array CP will be the third element address, that is, ++cpp point to cp+2. Then executes the *++cpp, then obtains the value c+1 of the third element of the array CP.
and then executes the--*++cpp, which is equivalent--(c+1), that is, C, and then executes the *c, so the first address of an array c is obtained, so the output is enter#. The execution of the statement also causes the current orientation of the CPP to change.
printf ("%s#", *cpp[-2] + 3);
The array subscript takes precedence, so it first executes cpp[-2], that is, * (cpp-2), because the first two output statements cause the CPP to add 2, so now-2, and then the Cpp-2 address is the first address of the group CP, so the first element of the array cp is c+3. Re-executing * (cpp[-2]), that is, * (C+3), which obtains the first address of the fourth element of the array C, the first address of the string "a", then adds 3 to the address, so the first address of the string St is obtained, so the output is st#. The current point of the CPP has not changed since the statement was executed.
printf ("%s#", cpp[-1] [-1] + 1);
execute cpp[-1 First], equivalent to * (CPP-1). Because the previous two output statements caused the CPP to be 2, it currently points to the third element of the array cp, so cpp-1 corresponds to the second element of the array CP c+2 and then executes cpp[-1][-1], equivalent to (c+2) [-1], i.e. * (c+2-1) = * (c+1), So get the first address of the second element of the array C, the first address of the string "NEW", then add 1 to the address, get the first address of the string ew, so the output is ew#. The current point of the CPP does not change after the statement has been executed.
The procedure is as follows:
#define NL 99
Main ()
{
int I, j, K, N;
float ***data;
Data= (malloc) (NL) *sizeof (FLOAT * *));
for (I=0;i <nl;i++)
{
data[i]= (float**) malloc ((NL) *sizeof (float*));
printf ("%d is ok!\n", i);
for (J=0;j <nl;j++)
{
data[i][j]= (float*) malloc (NL) *sizeof (float);
for (k=0;k <nl;k++)
{
data[i][j][k]=2.002;
}
}
}
printf ("%f\n", data[0][0][12]);
printf ("%d\n", &data[0][1][1]-&data[0][0][0]);
}
Why is it true that when NL takes a number smaller than 100, the result of subtracting two addresses is correct, and when the NL is greater than 100, the result is 1 more than the correct one. is a problem with the compiler. Or a problem with the memory boundary. How to make it output the correct result when it is greater than 100.
this data[i][j][k] is not an array of fully contiguous storage, where the location of its storage units depends on malloc (), such as data[0][0][0] and data[0][0][1, where the address discontinuity may exist. So there is a variable to compute the address offset. Contiguous storage can be achieved if a predetermined size is nl*nl*nl*sizeof (float) in a piece of memory, and then the ***data and one by one are mapped using the following three nested for statements.
The procedure is as follows:
#define NL 3
Main ()
{
int i,j,k;
float ***data;
Float *total= (float*) malloc (nl*nl*nl*sizeof (float));
int x=0;
Data= (malloc) (NL) *sizeof (FLOAT * *));
for (I=0;i <nl;i++)
{
data[i]= (float**) malloc ((NL) *sizeof (float*));
printf ("%d is ok!\n", i);
for (J=0;j <nl;j++)
{
data[i][j]= (float*) malloc (NL) *sizeof (float);
X=I*NL*NL+J*NL;
data[i][j]=&total[x];
for (k=0;k <nl;k++)
{
data[i][j][k]=2.002f;
}
}
}
printf ("\n%f\n", Data[nl-1][nl-1][nl-1]);
printf ("%d\n", &data[0][1][0]-&data[0][0][0]);
return 0;
}
I made a mistake with my algorithm. When NL is an odd number, it is true that NL takes an even number and does not add 1. And only when the last dimension of the cycle will be more than 1 of the situation, like &data[0][1][10]-&data[0][1][1] there will be no problem, this is malloc allocation of space problems. Or the problem with the memory address.
It 's not clear what you're doing with the offset, but I think it's about malloc, about the algorithm it allocates memory for. &data[0][1][10]-&data[0][1][1]//This memory is malloc once allocated, is continuous. &data[0][2][10]-&data[0][1][1]//this is in the loop, between the two malloc allocations, the memory may be discontinuous. You can't cut it like this. Example of contiguous memory, given one above, and the second one posted.