<16> "Understanding" array element as function parameter + "Mastering" array name as function parameter + "Mastering" array name as the note point of function parameter

Source: Internet
Author: User

#include <stdio.h>int sum (int x,int y) {    return x+y;} void Printnum (int x) {    ///To determine the value of x    if (x>0) {        printf ("%d\t", x);    } else{        printf ("0\t");}    } int main (int argc, const char * argv[]) {       int a[5]={1,-2,-3,-4,5};    Requirement: The sum of the first element and the last element of the array is required and    //a[0]    //a[4]    //           1      5     int s = sums (a[0], a[4]);     printf ("s =%d\n", s);        for (int i=0;i<5;i++) {               printnum (a[i]);        }        return 0;}
6 1    0    0    0    5    

"Mastering" the array name as a function parameter

#include <stdio.h>voidChangeintarr[2]){    //test, print the value of arrprintf"arr =%p\n", arr); arr[0]= -;}voidtest1 () {inta[2]={1,2}; printf ("A =%p\n", a); //a 0x7fff5fbff7c0//using the array name as the function parameter, the address is passed//The argument is the array name a, and the parameter is arr//At this point A and arr represent the same piece of memory space in memoryChange (a); printf ("a[0] =%d\n", a[0]);}//Define a functionfloatAvgfloatscore[5]){    floatsum =0.0f;  for(intI=0; i<5; i++) {        //The results of the door class were added together.Sum + =Score[i]; }        returnsum/5;}intMainintargcConst Char*argv[])    {test1 (); floatf1[5]={59.9,58.72,60.11,56.33,81.66}; //array masterpieces for function arguments//is address delivery    floatAV =avg (F1); printf ("%.2f\n", AV); return 0;}
A = 0x7fff5fbff6c0arr = 0x7fff5fbff6c0a[0] = 1863.34

"Mastering" the array name as a point of note for function parameters

The        array name is the parameter of the function         Note 1, the type and length of the parameter group must be the same as the actual parameter         
2, parameters of the length of thearray and the length of the argument can be inconsistent, although no error, the results of the operation and the actual difference
3 the length of the shape parameter group can not            be written 4, about the array name as a function parameter, the length of the array information loss problem

#include <stdio.h>//Print the contents of an arrayvoidPrint_arr (intarr[]) {     for(intI=0; i<Ten; i++) {printf ("%d\t", Arr[i]); }}voidPRINT_ARR2 (Chararr[]) {        //Why is it 8? //The C language specifies that, regardless of the type of data, the memory address of the data occupies 8 bytes in memory//int a = 4;    intLen =sizeof(arr);//8 bytesprintf"\nlen =%d\n", Len);//40? //for (int i=0; i<10; i++) {//printf ("%d\t", Arr[i]);//    }    }intMainintargcConst Char*argv[]) {    //array name as a note point for the function's arguments//1. The type and length of the parameter group should be consistent with the actual parameter.//1) If the type is inconsistent, it will cause an error    inta[Ten]={1,2,3,434,5,6,7,7,8,9}; //Print_arr (a);//iterate through the array and print out the normal wording//wrong wording//float f1[5]={1.2f,33.4f,2.3f,5.6f,3.14f};//    //The type of the argument and the type of the parameter do not match//Print_arr (F1); //Print_arr parameter is an array of type int//2, the length of the parameter array and the length of the argument can be inconsistent, although not error,//The result of the operation does not match the actualPrint_arr (a); //3, shape parameter group length can not write//4, about the array name as a function parameter, the length of the array information loss problem//int len = sizeof (a);//a number of bytes occupied//printf ("\nlen =%d\n", Len); // +//    //printf ("%p\n", a); //print_arr2 (0x7fff5fbff790);//double d1[3]={2.3,4.5,5.5};//print_arr2 (d1);    Charch[3]={'a','b','C'};        PRINT_ARR2 (CH); return 0;}

<16> "Understanding" array element as function parameter + "Mastering" array name as function parameter + "Mastering" array name as the note point of function parameter

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.