The pointer to the multi-dimensional array in C/C ++ is passed as a function parameter!

Source: Internet
Author: User

/* Program Author: Guan Ning
Site: www.cndev-lab.com
All manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author */

# Include <stdio. h>

Main ()
{
Void search_score ();/* define the number of custom Han types as not returned */
Void count_avg ();/* define the number of custom Han types as not returned */
Static float a [3] [4] = {97, 45.5, 66,77}, {88, 92.5, 78.5, 66}, {83, 74.5, 92,100 }}; /* enter the scores of each of the three students for each of the four courses */
Search_score (a, 0);/* change the number of custom courses to display the scores of each course */
Count_avg (* a, 12);/* change the custom number of courses to display the average score of each course for three students */
/* Note that the above * a is actually a [0] Or & a [0] [] 0, and this line is rewritten to count_avg (a [0], 12 ); or count_avg (& a [0] [0], 12) is also true */
}

Void search_score (p, n)
Float (* p) [4];/* defines a pointer Variable p, indicating that it is a pointer to a one-dimensional array containing four Integer Variables */
Int n;/* defines the form parameter n as an integer */
{
Int I;/* defines the variable used for loop I */
For (I = 0; I <4; I ++)/* this loop is used to print the four course scores of one student four times */
{
Printf ("% 7.2f", * (p + n) + I ));
}
Printf ("");
}

Void count_avg (p, n)
Float * p;
Int n;
{
Float * p_end;
Float sum = 0;
Float avg;
P_end = p + n-1;/* calculate the address of the last array element */

For (; p <= p_end; p ++)/* loop to the last element address and stop */
{
Sum + = * p;
}
Avg = sum/n;
Printf ("avg = % 7.2f", avg );
}

/*
Note that this question means to enter the scores of the three same 4 courses, calculate the average value, and display the scores of one of the four courses. In this example, the multi-dimensional array pointer and multi-dimensional array
As a summary of the transfer of the Han number parameter, You can carefully contact and understand what the concept of multi-dimensional array pointer is!
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.