English 中文(简体)
我为什么能修改C的主线?
原标题:Why can I modify the const pointer in C?

今天,我尝试使用窒息性,但我发现,最复杂的变量仍然可以修改,使我感到困惑。

下面是该守则,相比之下,(最无效的*a,*b)功能,我试图修改a指的是:

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

int values[] = {40, 10, 100, 90, 20, 25};

int compare (const void *a, const void*b)
{
    *(int*)a=2;
/* Then the value that a points to will be changed! */
    return ( *(int*)a - *(int*)b);
}

int main ()
{
    int n;
    qsort(values, 6, sizeof(int), compare);
    for (n = 0; n < 6; n++)
        printf("%d ", values[n]);
    return 0;
}

然后,我也试图改变a的价值。 本身:

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

int values[] = {40, 10, 100, 90, 20, 25};

int compare (const void *a, const void*b)
{
    a=b;
    return ( *(int*)a - *(int*)b);
}

int main ()
{
    int n;
    qsort(values, 6, sizeof(int), compare);
    for (n = 0; n < 6; n++)
        printf("%d ", values[n]);
    return 0;
}

However, I found both of them works.. Can anyone explain to me why I need to use const in the parameter list of compare if they can still be changed?

最佳回答

案例1:你正在使用静态的 cast子来消除 con。 您违反了该方法界定的合同。

案例2:你没有改变(主轴)的内容,而是分配包含一个最空白点的变数。

对实际影响: 如果有1起案件的话,你就可以在脚步中击.,如果真的看不到变。

建议: 只有当你知道你正在做些什么时,才摆脱混乱。

问题回答

It only works in this case because the pointer you re working against wasn t originally constant. Casting away constness and then modifying a value is undefined behaviour. UB means that the app can do anything from succeed to crash to make purple dragons fly out of your nostrils.

Its protecting you from silly mistakes, not when you try hard to make a mistake.
(int *)a when a is const something * is a bad practice, use (const int *)a instead.
a = b when a is const void * is ok because only the value pointed to is const. if you want both *a = x and a = x disallowed, declare a as const void * const.

实际上。

int compare (const void *a, const void*b);

这里需要考虑两点,点人和点人所指的记忆地点。 点人不是固定的,而是记忆地点。

如果您将签名改为:

int compare (const void *const a, const void *const void);

那么,所有东西都会合在一起。 在你看来,你可以改变点,而不是价值。 因此,贵点人可以指出不同的记忆点。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签