Re: How to check if char pointer is a constant!?
Phil Sutter <[email protected]> Thu, 7 Jul 2016 22:26:21 +0200
| Newsgroups | org.kernel.vger.linux-c-programming |
|---|---|
| Message-ID | <[email protected]> |
Hi,
On Thu, Jul 07, 2016 at 04:40:56PM -0300, Daniel. wrote:
> I have some function, say
> void empty(char *p) { p[0] = '\0'; }
>
> If I call it like this empty("Hello") it will segfaults since "Hello"
> is put in readonly section of the program. Is there a way to check for
> this?! Maybe some nasty gcc extension!?
The problem here is upon calling the function, the const char *
parameter is implicitly casted to char *. So inside the function there
is no way to check this I'd say. You can call gcc with -Wwrite-strings
to have it generate a warning whenever a string constant is assigned to
a non-const char * variable.
HTH, Phil