const*与*const的区别

下面是我做的关于一道const的问题,为什么sort可以排序const*p[]呢?

#include

struct Goods
{
    int number;
    int weight;
    int price;
};

void sort(const struct Goods* goods[],int size)
{
    int i,j;
    const struct Goods *temp;
    for(i=0;i1;i++)
    {
        for(j=0;j1;j++)
        {
            if((goods[j]->price)>(goods[j+1]->price))
                {
                    temp=goods[j];
                    goods[j]=goods[j+1];
                    goods[j+1]=temp;
                }

        }
    }
}

void read(struct Goods* goods)
{
    scanf("%d %d %d\n",&goods->number,&goods->weight,&goods->price);
}

void readArray(struct Goods goods[],int size)
{
    int i;
    for(i=0;ivoid print(const struct Goods* goods)
{
    printf("%d %d %d\n",goods->number,goods->weight,goods->price);
}

void printArray(const struct Goods goods[],int size)
{
    int i;
    for(i=0;ivoid printGoods(const struct Goods *address[],int size)
{
    int i;
    for(i=0;iint main()
{
    struct Goods goods[10];
    readArray(goods,10);

    const struct Goods*addresses[10];
    int i;
    for(i=0;i<10;i++)
    {
        addresses[i]=&goods[i];
    }
    sort(addresses,10);

    printArray(goods,10);
    printf("\n");
    printGoods(addresses,10);
}


const *p
可以记忆为const (*p),p指向地址的内容不可改变,但是p指向的地址是可以改变的这就是sort函数可以进行排序的原因
*const p
可以记忆为*(const p),const修饰的地址,p指向的地址不可改变,但是p指向地址的内容可以改变

你可能感兴趣的:(乱七八糟的东西)