@ Clear drop pass array to function

Source: Internet
Author: User
Tags function prototype

One-dimensional arrays

3 Principles for passing an array to a function

1. The function call simply passes the array name.

2. In a function definition, the type of the formal parameter must be the same as the array, and the size of the array does not have to be specified.

3. A function prototype must be defined as an array of parameters.

1#include <stdio.h>2#include <stdlib.h>3 voidSortintX[],intm);4 intMain ()5 {6inti;7intmarks[5] = { +, -, the,Bayi, *};8      9printf"Marks before sorting\n");Ten for(i =0; I <5; i++) Oneprintf"%d", Marks[i]); Aprintf"\ n"); -       -Sort (marks,5); theprintf"Marks after sorting\n"); - for(i =0; I <5; i++) -printf"%d", Marks[i]); -printf"\ n"); +       -System"Pause"); + } A   at voidSortintX[],intm) - { -intI, J, T; -        - for(i =1; I <= m1; i++) -  for(j =1; J <= M-i; J + +) inif(x[j-1] >=X[j]) -            { tot = x[j-1]; +x[j-1] =x [j]; -X[J] =T; the            } *}

Two-dimensional arrays

1. The function call simply passes the array name.

2. In a function definition, you must use two square brackets to indicate that the array is two-dimensional.

3. The size of the second dimension of the array must be specified.

4. The function prototype must be defined in the same way as the function header.

//method One, the formal parameter gives the length of the second dimension. //For example:#include<stdio.h>voidFuncintNCharstr[[5] ){inti; for(i =0; I < n; i++) printf ("\nstr[%d] =%s\n", I, Str[i]);}voidMain () {Char* p[3];Charstr[][5] = {"ABC","def","Ghi"};func (3, str);}//method Two, the formal parameter is declared as a pointer to an array. //For example:#include<stdio.h>voidFuncintNChar(*STR) [5] ){inti; for(i =0; I < n; i++) printf ("\nstr[%d] =%s\n", I, Str[i]);}voidMain () {Char* p[3];Charstr[][5] = {"ABC","def","Ghi"};func (3, str);}//method Three, the formal parameter is declared as a pointer to the pointer. //For example:#include<stdio.h>voidFuncintNChar**str) {inti; for(i =0; I < n; i++) printf ("\nstr[%d] =%s\n", I, Str[i]);}voidMain () {Char* p[3];Charstr[][5] = {"ABC","def","Ghi"};p [0] = &str[0][0];p [1] = str[1];p [2] = str[2]; Func (3, p);}

Passing a string to a function

1. The string to be passed must be declared as the formal parameter of the function at the time of definition. For example:

void display (char item_name[])

{

......

}

2. The function prototype must be able to indicate that the parameter is a string. For the above function definition, its prototype can be written:

void display (char str[]);

3. The function call must take a string array name without subscript as the fact argument. For example:

Display (name);

Where name is an array of strings that have been correctly declared in the calling function.

In the C language, like arrays, a string cannot be passed to a function by value.

@ Clear drop pass array to function

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.