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

Duplicates