指针数组和数组指针作为函数参数使用

1. 区分:根据优先级   [] > *

指针数组与数组指针
区分指针数组与数组指针的方法,还是得看优先级()>[ ]>*,
(*p)[n]:根据优先级,先看括号内,则p是一个指针,这个指针指向一个一维数组,数组长度为n,这是“数组的指针”,数组指针就是指向数组的指针;
*p[n]:根据优先级,先看[],则p是一个数组,再结合 *,这个数组的元素是指针类型,共n个元素,这是“指针的数组”,指针数组是存放指针的数组。

2.   指针数组作为函数参数  :  优先级  [] > *

void print_strings(const char *strings[], int count) {  
    for (int i = 0; i < count; i++) {  
        printf("%s\n", strings[i]);  
    }  
}  

int main() {  
    const char *my_strings[] = {"Hello", "World", "!"};  
    print_strings(my_strings, 3);  
    return 0;  
}

2. 数组指针作为函数参数

void print_matrix(const char *matrix[], int rows, int cols) {  
    for (int i = 0; i < rows; i++) {  
        for (int j = 0; j < cols; j++) {  
            printf("%c ", matrix[i][j]);  
        }  
        printf("\n");  
    }  
}  


int main() {  
    const char *matrix[] = {  
        "Hello",  
        "World",  
        "Foo"  
    }; // 注意这不是真正的二维字符数组,而是字符串数组(即字符指针数组)  
    print_matrix(matrix, 3, /* 这里需要假设每行长度相同或提供其他方式来知道列数 */);  
    return 0;  
}

3. 

总结:指针数组和数组指针都可以用做函数参数。但是

当用 二维字符数组或者说是字符串数组 作为函数参数时,建议使用 数组指针。

#include <stdio.h>  
#include <stdint.h>  
#include <stdlib.h>  

void print(const char (*ptr)[16],int size)  // or void print(const char ptr[][16],int size)
{
    for ( int i=0; i< size; i++)
      printf("ptr[%d] =%s \n",i,ptr[i]);

}

 
int main() 
{  

   const char str[4][16]={"orderId",  "srcCode","dstCode","podCode"};  
   print(str,4);
   return 0;  
}

错误示范:

#include <stdio.h>  
#include <stdint.h>  
#include <stdlib.h>  

void print_strings( char *strings[], int count) {  

        strcpy(strings[0],"good");

}  

int main() {  
     char my_strings[10][20] = {"Hello", "World", "!"};  
    print_strings(my_strings, 3);  

    printf("%s \n",my_strings[0]);
    return 0;  
}

报错:test.c:8:6: note: expected ‘char **’ but argument is of type ‘char (*)[20]’
 void print_strings( char *strings[], int count) { 

但是下面是可以的

#include <stdio.h>  
#include <stdint.h>  
#include <stdlib.h>  

void print_strings( char *strings[], int count) {  

           printf("%s \n",strings[0]);

}  

int main() {  
     char *my_strings[] = {"Hello", "World", "!"};  
    print_strings(my_strings, 3);  

 
    return 0;  
}
本质: char *my_strings[] 是一个 char **  ; char my_strings[10][20] 是一个字符数组,所以用数组指针是最好

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-11 19:28:02       5 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-11 19:28:02       5 阅读
  3. 在Django里面运行非项目文件

    2024-06-11 19:28:02       4 阅读
  4. Python语言-面向对象

    2024-06-11 19:28:02       6 阅读

热门阅读

  1. 智能合约中存储和计算效率漏洞

    2024-06-11 19:28:02       11 阅读
  2. Shell脚本要点和难点以及具体应用和优缺点介绍

    2024-06-11 19:28:02       16 阅读
  3. Pytorch容器

    2024-06-11 19:28:02       15 阅读
  4. Unity 数据存储

    2024-06-11 19:28:02       17 阅读
  5. Data Management Controls

    2024-06-11 19:28:02       15 阅读
  6. 【AI应用探讨】— Gemini模型应用场景

    2024-06-11 19:28:02       16 阅读
  7. 设计模式---工厂模式

    2024-06-11 19:28:02       15 阅读
  8. C++经典150题

    2024-06-11 19:28:02       17 阅读
  9. k8s 小技巧: 查看 Pod 上运行的容器

    2024-06-11 19:28:02       16 阅读
  10. Elasticsearch 认证模拟题 - 9

    2024-06-11 19:28:02       16 阅读