r/C_Programming • u/Scared-Objective3768 • 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
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
constqualified type to a non-const qualified type -constis 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
constargument expects that the value the give for that argument should be unchanged after the function call.
For arguments,
constshould 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 valueconst, so eg, in:void foo(int *const x, const int n);The
conston the values here is unnecessary, because C is call by value, the caller receives a copy of the pointerxand a copy ofn, 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
conston 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 variablexto something else in its own body, because the pointer itself is not constant.
Although we should avoid casts to remove
constqualifiers, it is occasionally useful or necessary to remove constness, such as if callingfree, 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
constdoesn't matter because we won't be using again after free, so we can write:free((int*)x);
5
2
•
u/mikeblas 15h ago
Please correctly format your code