Home > Back-end >  Small white, there is a c language problem bothers me a lot of days, beg god for help
Small white, there is a c language problem bothers me a lot of days, beg god for help

Time:10-01

The great spirit, this program is input the student number, and then shows the four grades of the students, and is the place where I had doubts in the search function, why use p=* (score + n - 1)? I know to take score + n - 1 p is the address, but why do you want to use * to take? * not should be used to represent the address points to value? Consult bosses!
#include
Float * search (float score [] [4], int n);
Int main ()
{
Float score [] [4]={{60,70,80,90}, {56,89,67,88}, {34,78,90,66}};
Printf (" both Please input you need to score of student 's number: \ n ");
int n;
Float * p;
The scanf (" % d ", & amp; n);
P=search (score, n);
Printf (" You need to the number of student 's score: % d \ n ", n);
int i;
for(i=0; I<4. I++) {
Printf (" % 2 \ t f ", * (p + I));
//p++;
}
return 0;
}
Float * search (float score [], [4], int n) {
Float * p;
P=* (score + n - 1);
Return the p;
}

CodePudding user response:

Score is a two-dimensional array, each student one row, a total of four integer, number 1 student score corresponding array [0] [0] - score [0] [3], the number of the students score 2 [1] [0] - score [1] [3], the number of the students score 3 [2] [0] - score [2] [3], the number of the students score 4 [3] [0] - score [3], [3], the pointer p can take to address each of the 2 d data is the first n students first grade score [0], [n - 1] * (p + 1) second grade score [1], [n - 1] * (p + 2) third grade score [n - 1] [2],...

CodePudding user response:

#include
Float * search (float * score, int n);
Int main ()
{
Float score [] [4]={{60,70,80,90}, {56,89,67,88}, {34,78,90,66}};
Printf (" both Please input you need to score of student 's number: \ n ");
int n;
Float * p;
The scanf (" % d ", & amp; n);
P=search ((float *) score, n);
Printf (" You need to the number of student 's score: % d \ n ", n);
int i;
For (I=0; I<4. I++) {
Printf (" % 2 \ t f ", * (p + I));
//p++;
}
return 0;
}
Float * search (float * score, int n) {
Float * p;
P=sizeof (score + (n - 1) * (float).
Return the p;
}
Which would be more easy to understand once a pointer, the original is the key to the compiler how to explain this two only array

CodePudding user response:

Huh? A two-dimensional array?
Know this? Yes, this is the legendary dolls,
Use this metaphor, however, in this case a double pointer * * is not very appropriate, need to be modified slightly,
Sing loudly: the dolls in large dolls, a small dolls, two small dolls, three small dolls ah four small,
The little dolls with doug , dolls is pointer, doug is data
Uh huh, the song you will
Assuming that the Eva only three layer, large dolls (pointer) the outermost layers inside the dolls with four side by side (inner pointer),
As for these four little dolls, it contained many peas (data int, note with beans here, instead of dolls, set down discourse is too deep again ), you, in this case is 3, (3 line 4)
This is the parameter (float score [] [4]), mean; Score is the outer large dolls

Pointer to operate this thing, *, each with a *, you'll be able to understand to open a layer of dolls,
Now learned, the relationship between pointer to an array, is then a
Int arr [4].
Int * p=arr;
* p;//* (p + 0), p [0]
* (p + 1);//p [1]
* (p + 2);//p [2]
* (p + 3);//p [3].
Well, to see the code
 float * search (float score [] [4], int n) {
Float * p;
P=* (score + n - 1);
/*
* (score + 0);
What is the score? Big dolls, *? Open it (a);
Open again after you see the beans? - no, you see four small dolls
* (score + 0), the first of four "dolls"
* (+ 1) score? The second "dolls"
.
* (score + 3), the fourth "dolls"
Ok, what is p? Doug? Is not, not doug, dolls, back to the previous song,, dolls, doll is pointer
Throw a little dolls out of return from the function, and want to access the data (doug), to open a set of Eva, how are we going to use *, * (p + 0) * (p + 1)...
*/
Return the p;
}



Finally, the serious, how to torture you, in fact the compiler is translation
this function?
 
//to a simple first, warm up
Float * search (float score [4], int n);
//in fact to====& gt;
Float * search (float * score, int n);

//then the?
Float * search (float score [], [4], int n)
//in the compiler, actually he is such a thing=="
Float * search (float (* score) [4], int n);


Did you read it? If you don't understand, or think about dolls,

CodePudding user response:

In front of the two dimensional array to add an asterisk on behalf of the column, p=* (score + n - 1) on behalf of p points to a [n - 1) [0] address, * (score + 1) + 2 is a [1] [2]

CodePudding user response:

Score [] [] is a two dimensional array in the inside of the first element of the value (60) can be expressed in * * score, the last is a * (* (score + 2) + 3).
* (+ 1) score is the score of the array [1] [0] address, address for subsequent score [1] [0] score [1] [1] score [1] [2] score [1] [3]. There's
Score [2] [0] score [2] [1], and so on in the title is an address * p (score + n - 1) is also an address


I feel you still not understand main two-dimensional array pointer, said anyway, do you know a * in a two dimensional array is the address
Two * is numerical
  • Related