王康吉 發表於 2025-12-18 10:05:45

C语言中指针数组和数组指针的具体使用

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>一、指针数组</li><ul class="second_class_ul"><li>1.语法格式</li><li>2.常见用法</li></ul><li>二、数组指针</li><ul class="second_class_ul"><li>1.语法格式</li><li>2.常见用法</li></ul></ul></div><p>指针数组和数组指针是C语言中高频混淆的概念,核心区别在于<strong>本质不同</strong>:</p>
<ul><li>指针数组是<strong>&ldquo;数组&rdquo;</strong>(数组的每个元素都是指针),主要用来<strong>储存数据</strong>;</li><li>数组指针是<strong>&ldquo;指针&rdquo;</strong>(指针指向一个完整的数组),主要用来<strong>访问数据</strong>。</li></ul>
<p class="maodian"></p><h2>一、指针数组</h2>
<blockquote><p>一个普通数组,但其<strong>每个元素的类型是指针</strong>(可以指向 int、char、结构体等任意类型)。</p></blockquote>
<p class="maodian"></p><p class="maodian"></p><h3>1.语法格式</h3>
<blockquote><p>数据类型 *数组名[数组长度];</p></blockquote>
<div class="jb51code"><pre class="brush:cpp;">int *p;   // 指针数组:数组p有5个元素,每个元素是int*类型的指针
char *str;// 指针数组:数组str有3个元素,每个元素是char*类型的指针(常用于字符串)</pre></div>
<p><strong>关键:</strong>[ ] 优先级高于 *,所以 p 先与 [ ] 结合,确定是数组,再由 * 确定数组元素是指针。</p>
<p class="maodian"></p><p class="maodian"></p><h3>2.常见用法</h3>
<p><strong>存储多个字符串</strong>(避免字符串数组的内存浪费)、<strong>指向多个一维数组</strong>。</p>
<ul><li><strong>存储多个字符串</strong></li></ul>
<p>字符串常量的本质是字符数组的首地址,用 char * 指针数组存储这些地址,高效且灵活。</p>
<div class="jb51code"><pre class="brush:cpp;">#include &lt;stdio.h&gt;

int main() {
    // 指针数组:每个元素指向一个字符串常量(首地址)
    char *fruit[] = {"apple", "banana", "cherry", "date"};
    int n = sizeof(fruit) / sizeof(fruit); // 数组长度:4

    // 遍历指针数组,打印每个字符串
    for (int i = 0; i &lt; n; i++)
    {
      // fruit 是指针,指向字符串首地址,%s 直接解析
      printf("fruit[%d]: %s\n", i, fruit);
    }

    return 0;
}</pre></div>
<p>输出:</p>
<blockquote><p>fruit: apple</p>
<p>fruit: banana</p>
<p>fruit: cherry</p>
<p>fruit: date</p></blockquote>
<p><strong>优势:</strong>字符串长度不同时,指针数组仅存储地址(4 字节 / 个),比 char fruit(固定 20 字节 / 个)节省内存。</p>
<ul><li><strong>指向多个一维数组</strong></li></ul>
<div class="jb51code"><pre class="brush:cpp;">#include &lt;stdio.h&gt;

int main() {
    int arr1[] = {1,2,3};
    int arr2[] = {4,5,6};
    int arr3[] = {7,8,9};

    // 指针数组:每个元素指向一个int型一维数组
    int *p_arr[] = {arr1, arr2, arr3};

    // 遍历:通过指针数组访问每个一维数组的元素
    for (int i = 0; i &lt; 3; i++)
    {
      for (int j = 0; j &lt; 3; j++)
      {
            // p_arr 是数组首地址,p_arr 等价于 *(p_arr + j)
            printf("%d ", p_arr);
      }
      printf("\n");
    }

    return 0;
}</pre></div>
<p>输出:</p>
<blockquote><p>1 2 3</p>
<p>4 5 6</p>
<p>7 8 9</p></blockquote>
<p class="maodian"></p><h2>二、数组指针</h2>
<blockquote><p>一个指针变量,专门指向一个<strong>完整的数组</strong>(而非数组的单个元素)。</p></blockquote>
<ul><li>对一维数组的数组名<ul><li>&amp;:值不变,类型升级为数组指针,即<strong>第一行</strong>元素的指针。</li><li>* :地址值不变,类型降级,指向数组中<strong>第一个</strong>元素的指针。</li></ul></li></ul>
<p>一维数组的数组名是指向第一个元素的指针。数组名+1,步进值是 sizeof(基类型) 的大小。</p>
<p>二维数组的数组名是指向第一行第一个元素的指针。数组名+1,步进值是一行的大小。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121810014349.jpg" /></p>
<h3>1.语法格式</h3>
<blockquote><p>数据类型 (*指针名)[数组长度];</p></blockquote>
<div class="jb51code"><pre class="brush:cpp;">int (*p);   // 数组指针:p是指针,指向一个「int类型」的数组
char (*q); // 数组指针:q是指针,指向一个「char类型」的数组</pre></div>
<p><strong>关键:</strong>必须加括号 (),强制 p 先与 * 结合,确定是指针,再由 确定指向的数组类型(int )。</p>
<h3>2.常见用法</h3>
<p><strong>指向二维数组</strong>、<strong>作为函数参数接收二维数组</strong>(避免二维数组传参退化问题)。</p>
<ul><li><strong>指向二维数组并遍历</strong></li></ul>
<div class="jb51code"><pre class="brush:cpp;">#include &lt;stdio.h&gt;

int main() {
    // 二维数组:3个元素,每个元素是int类型的一维数组
    int arr = {{1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15}};

    // 数组指针:p指向int类型的数组(与二维数组的元素类型匹配)
    int (*p) = arr;

    // 遍历二维数组(3种等价写法)
    for (int i = 0; i &lt; 3; i++)
    {
      for (int j = 0; j &lt; 5; j++)
      {
            // 写法1:p(最直观,等价于二维数组访问)
            // 写法2:*(p + j)(p是第i个一维数组的首地址)
            // 写法3:*(*(p + i) + j)(p+i指向第i个一维数组)
            printf("%d ", *(*(p + i) + j));
      }
      printf("\n");
    }

    return 0;
}</pre></div>
<p>输出:</p>
<blockquote><p>1 2 3 4 5&nbsp;</p>
<p>6 7 8 9 10&nbsp;</p>
<p>11 12 13 14 15&nbsp;</p></blockquote>
<p><strong>核心逻辑:</strong>arr 是二维数组首地址(指向 arr ,即第一个 int 数组),与数组指针 p 的类型完全匹配,因此 p = arr 合法。</p>
<ul><li><strong>作为函数参数接收二维数组</strong></li></ul>
<div class="jb51code"><pre class="brush:cpp;">#include &lt;stdio.h&gt;

// 函数参数:用数组指针接收二维数组(int退化为int(*))
void print2DArray(int (*p), int row)
{
    for (int i = 0; i &lt; row; i++)
    {
      for (int j = 0; j &lt; 5; j++)
      {
            printf("%d ", p);
      }
      printf("\n");
    }
}

int main()
{
    int arr = {{1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15}};
    // 传参:直接传入二维数组名(退化为数组指针)
    print2DArray(arr, 3);
    return 0;
}</pre></div>
<p>输出:</p>
<blockquote><p>1 2 3 4 5&nbsp;</p>
<p>6 7 8 9 10&nbsp;</p>
<p>11 12 13 14 15&nbsp;</p></blockquote>
<p><strong>注意:</strong>函数参数中 int (*p) 等价于 int p[ ](两者都是数组指针的简写),但不能写成 int *p(这是指针数组,类型不匹配)。</p>
<p>到此这篇关于C语言中指针数组和数组指针的具体用法的文章就介绍到这了,更多相关C语言 指针数组和数组指针内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>C语言中的数组指针数组与函数指针数组</li><li>C语言深入分析数组指针和指针数组的应用</li><li>简单分析C语言中指针数组与数组指针的区别</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: C语言中指针数组和数组指针的具体使用