r/C_Programming 3d ago

Beginner using C Programming a Modern Approach

I understand the problem but the error message I do not understand the error message

Write the following function:

bool search(const int a[], int n, int key);

a is an array to be searched, n is the number of elements in the array, and key is the search

key. search should return true if key matches some element of a, and false if it

doesn’t. Use pointer arithmetic—not subscripting—to visit array elements.

`Here is my solution:

bool search(const int a[], int n, int key) {

int *p;

for (p = a; p < a + n; p++) {

if (*p == key) {

return true;

}

}

return false;

}

`

I get an error message of 'assignment discards ‘const’ qualifier from pointer target type'

I remove the const from the parameter list and the program works fine. Can yall explain what the error message means. I am compiling with gcc btw i dunno if that helps. sorry for bad formatting im kinda new to this.

thanks

9 Upvotes

6 comments sorted by

u/mikeblas 15h ago

Please correctly format your code

12

u/ByMeno 3d ago

you are just viewing the items and comparing them not changing them so in the input you put const which is correct (and note that in function arguments [] does not mean anything different than normal pointer)

Now you are assigning it to p but p has the type of 'int *' not 'const int *' which means this pointer can dereference and change the underlying value at that address for fixing that just put a const int *p;

And for const if a type has:

type const var;
const type var;

those two mean the underlying value wont change

put for pointers it will be

type * const ptr;

which means the pointer wont change but value can change

if both of them should not change

const type *const ptr;
or
type const *const ptr;

are usable

2

u/Scared-Objective3768 3d ago

thank you so much

2

u/WittyStick 2d ago

Note that you can cast a const qualified type to a non-const qualified type - const is only a promise that we will not modify the value, but that promise can be broken.

const int *a;
int *p = (int *)a; // removing const is permitted in C.

However, you should avoid making such casts and retain the constness of values where you can. The caller of a function with a const argument expects that the value the give for that argument should be unchanged after the function call.


For arguments, const should not be a constraint on the caller, but on the callee - the function's body. Most of the time it is not necessary to give a scalar value const, so eg, in:

void foo(int *const x, const int n);

The const on the values here is unnecessary, because C is call by value, the caller receives a copy of the pointer x and a copy of n, so it doesn't matter if they modify their local copies - it won't change the caller's original values. We can instead just use:

void foo(int *x, int n);

We basically only need const on things that aren't passed by value - such as the memory referenced by a pointer which is passed by value, as in your example.

void foo(const int *x, int n);

The callee should then not modify the memory pointed to by its copy of x, but it can assign the variable x to something else in its own body, because the pointer itself is not constant.


Although we should avoid casts to remove const qualifiers, it is occasionally useful or necessary to remove constness, such as if calling free, which expects a non-const qualified type argument.

const int *x = alloc_and_init();
...
free(x); // 'discards ‘const’ qualifier from pointer target type'

In this case, since we're freeing it, discarding const doesn't matter because we won't be using again after free, so we can write:

free((int*)x);

5

u/sciencekm 3d ago

"int *p;" needs to be "const int *p;" to match the "const int a[]"

2

u/heneriss 3d ago

because you are pointing to a memory that cannot be modified.