The strlen
function take just a single argument of type const char*
(which means it will accept a non-const
char*
argument).
Some older compilers might accept function calls with an incorrect number of arguments. You need to fix the code.
That's assuming your code actually uses strlen
. As fernando.reyes suggests in a comment, it's likely you really meant strcpy
rather than strlen
(particularly since you don't use the result; calling strlen()
and discarding the result doesn't make much sense).
In that case, the problem is that stringLiteralPointer
points to (the array object associated with) a string literal. Attempting to modify such an array has undefined behavior. Some compilers might put string literals in read/write memory, but you should not depend on that.
Again, the solution is to fix your code. You need to arrange for stringLiteralPointer
to point to an array that's not a string literal. One way to do this is to use malloc
to allocate enough space (and be sure to check that the pointer returned by malloc
is non-null). If your pointer already points to a string literal, you can probably also use the non-standard strdup
function, which (attempts to) allocate a new read/write copy of the string and returns a pointer to it. Again, you need to confirm that the allocation succeeded.
Another alternative might be to use an array, rather than a pointer. This:
char *ptr = "literal";
causes ptr
to point to a string that you're not allowed to modify. This:
char arr[] = "literal";
makes arr
an array containing a read/write copy of the string literal.
And please make sure that the code in your question is the actual code you're compiling. Copy-and-paste it, don't re-type it. Please read this.
More generally, gcc has a number of options to enable extra warnings. If you compile your code with something like:
gcc -std=c99 -Wall -Wextra -O3 ...
you'll likely catch a number of questionable things in your code. The -std=c99
option tells gcc to (attempt) to conform to the 1999 ISO C standard; if your code is meant to be compliant to C90 (commonly but incorrectly called "ANSI C"), use -ansi
rather than -std=c99
. The -O3
option enables the maximum level of optimization; this causes the compiler to perform additional analysis that can also enable more warnings.
There are a number of C checkers that perform even more analysis than most compilers do. Lint was the original tool. Splint (formerly LCLint) is a modern implementation, and it's freely available here or (probably) via your system's package manager.