zoukankan      html  css  js  c++  java
  • C语言函数二维数组传递方法

    方法一:形参给出第二维的长度。

    例如:

    #include<stdio.h>
    #include
    <math.h>
    #include
    <ctype.h>
    #include
    <string.h>
    #include
    <stdlib.h>


    void func(int n,char str[][5]){
        
    int i;
        
    for(i=0; i<n; i++)
          printf(
    "\nstr[%d]=%s\n",i,str[i]);
        }

    int main()
    {
      
    char* p[3];
      
    char str[][5]={"abc","def","ghi"};
      func(
    3,str);
      
    return 0;

    }

     方法二:形参声明为指向数组的指针。

    例如:

     #include<stdio.h>

    #include<math.h>
    #include
    <ctype.h>
    #include
    <string.h>
    #include
    <stdlib.h>
    void func(int n,char (*str)[5]){
        
    int i;
        
    for(i=0; i<n; i++)
          printf(
    "\nstr[%d]=%s\n",i,str[i]);
        }
    int main()
    {
      
    char* p[3];
      
    char str[][5]={"abc","def","ghi"};
      func(
    3,str);
      
    return 0;
    }

    方法三:形参声明为指针的指针。

    例如:

     #include<stdio.h>

    #include<math.h>
    #include
    <ctype.h>
    #include
    <string.h>
    #include
    <stdlib.h>
    void func(int n,char **str){
        
    int i;
        
    for(i=0; i<n; i++)
          printf(
    "\nstr[%d]=%s\n",i,str[i]);
        }

    int main()
    {
      
    char* p[3];
      
    char str[][5]={"abc","def","ghi"};
      p[
    0]=&str[0][0];
      p[
    1]=str[1];
      p[
    2]=str[2];
      func(
    3,p);
      
    return 0;
    }
    作者:cpoint
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    goweb-goweb基础
    玩转Git
    程序爱好者的常用网站
    高等数学思维导图——6.微分方程
    前端趣玩——超炫的聚光灯效果
    Python课程笔记(四)
    高等数学思维导图——5.多元函数微分法及其应用
    算法很美(四)
    第五章——定积分必记公式
    十大经典排序算法
  • 原文地址:https://www.cnblogs.com/cpoint/p/2021712.html
Copyright © 2011-2022 走看看