数组指针和指针数组

来源:互联网 发布:语文网络培训心得体会 编辑:程序博客网 时间:2024/06/07 08:15

转自:http://www.cnblogs.com/Romi/archive/2012/01/10/2317898.html

这两个名字不同当然所代表的意思也就不同。我刚开始看到这就吓到了,主要是中文太博大精深了,整这样的简称太专业了,把人都绕晕了。从英文解释或中文全称看就比较容易理解。

指针数组:array of pointers,即用于存储指针的数组,也就是数组元素都是指针

数组指针:a pointer to an array,即指向数组的指针

还要注意的是他们用法的区别,下面举例说明。

int* a[4]     指针数组     

                 表示:数组a中的元素都为int型指针    

                 元素表示:*a[i]   *(a[i])是一样的,因为[]优先级高于*

int (*a)[4]   数组指针     

                 表示:指向数组a的指针

                 元素表示:(*a)[i]  

注意:在实际应用中,对于指针数组,我们经常这样使用:

?
1
2
typedefint* pInt;
pInt a[4];

这跟上面指针数组定义所表达的意思是一样的,只不过采取了类型变换。

代码演示如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
  
usingnamespacestd;
  
int main()
{
int c[4]={1,2,3,4};
int *a[4]; //指针数组
int (*b)[4]; //数组指针
b=&c;
//将数组c中元素赋给数组a
for(inti=0;i<4;i++)
{
a[i]=&c[i];
}
//输出看下结果
cout<<*a[1]<<endl;//输出2就对
cout<<(*b)[2]<<endl;//输出3就对
return0;
}

注意:定义了数组指针,该指针指向这个数组的首地址,必须给指针指定一个地址,容易犯的错得就是,不给b地址,直接用(*b)[i]=c[i]给数组b中元素赋值,这时数组指针不知道指向哪里,调试时可能没错,但运行时肯定出现问题,使用指针时要注意这个问题。但为什么a就不用给他地址呢,a的元素是指针,实际上for循环内已经给数组a中元素指定地址了。但若在for循环内写*a[i]=c[i],这同样会出问题总之一句话,定义了指针一定要知道指针指向哪里,不然要悲剧。

类似的还有指针函数和函数指针,遇到了再说吧。

-----------------自测代码:逗号分割产品列表-----------------------------

**********charSplit.h***************

#ifndef CHARSPLIT_H
#define CHARSPLIT_H

#define MAX_PRODUCTLISTITEM_PRODUCTID_LEN 16
typedef unsigned char UINT8;
typedef unsigned short int UINT16;
typedef signed short int INT16;
typedef signed char INT8 ;

typedef struct {
        UINT8   strProducts[100*(MAX_PRODUCTLISTITEM_PRODUCTID_LEN+1)+1];               //未分割之前
        UINT16  productNum;                                     //产品总数
        UINT8   productArray[100][MAX_PRODUCTLISTITEM_PRODUCTID_LEN+1]; //分割之后
}t_products_info;

#endif

 

*************charSplit.c***********
#include <stdio.h>
#include "charSplit.h"
#include <string.h>
//函数声明
void split_productlist(UINT8 *srcStr, t_products_info *info,char symbol);

void split_productlist(UINT8 *srcStr, t_products_info *info,char symbol)
{
  char (*pArray)[MAX_PRODUCTLISTITEM_PRODUCTID_LEN+1]=NULL; //一维数组指针
  //char **pArray=NULL;
  int dim1_index=0;
  int dim2_index=0;
  char *pcur=srcStr;
  pArray=info->productArray;  //一维数组指针指向二维数组首地址,一个指针指向一个长度为 MAX_PRODUCTLISTITEM_PRODUCTID_LEN+1的内存块
  while(*pcur!='\0')
  {
        printf("pcur: %c \n",*pcur);
          if(*pcur!=symbol)
        {
                if(dim1_index>=MAX_PRODUCTLISTITEM_PRODUCTID_LEN)
                {
                        printf("dim1_index is too large ,memory out!");
                        break;
                }
                else
                {
                        pArray[dim2_index][dim1_index]=*pcur;   //pArray是数组指针,dim2_index 为Array of Pointers(存指针的数组)的索引
                         dim1_index++;
                }
        }
        else
        {
                dim1_index=0;
                dim2_index++;
                if(dim2_index>=100)
                {
                        printf("dim2_index is too large ,memory out!");
                        break;
                }
        }
        info->productNum=dim2_index+1;
        pcur++;
  }
  return;
}


int main()
{
    char products[1024];
    t_products_info prodInfo={0};
    strcpy(prodInfo.strProducts,"a,ab,acb,dds,hhl");
    split_productlist(prodInfo.strProducts,&prodInfo,',');
   
    return;
}


是不是还可以这样定义?
typedef (*char)[17] CharArrayPointer_17;
typedef *CharArrayPointer_17[100] pCharArrayPointers_100;  //存放100个长度为17的一维字符数组指针

 

 

 

 

 

 

0 0
原创粉丝点击