E
Emmanuel Delahaye
Jack Klein wrote on 02/05/05 :
I have some lines above that are used for tests... If you are curious:
#define main main_
#if 0
#include "ed/inc/sysalloc.h"
#include "ed/inc/sys.h"
#undef assert
#define assert(e) ASSERT (e)
#else
#include <assert.h>
#endif
/* ---------------------------- appli
----------------------------------- */
He he... Following good advices read here about string literals not
beeing mutable, I compile in paranoid mode. So, it's not 'plain wrong',
but rather 'plain safe'.
gcc with -Wwrite-strings (among other switches)
Attempt to get rid of the warnings :
#include<stdio.h>
#include<string.h>
int main (void)
{
char const * a= "bcd";
strcpy(a,"hello");
a = "fgh";
a[0] = 't';
printf("%s",a);
return 0;
}
main.c: In function `main_':
main.c:18: warning: passing arg 1 of `strcpy' discards qualifiers from
pointer target type
main.c:20: warning: assignment of read-only location
meaning that there is still a problem.
Actually:
strcpy(a,"hello");
and
a[0] = 't';
*are* plain wrong because the string is not mutable.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char * a= "bcd";
clrscr();
strcpy(a,"hello");
a = "fgh";
a[0] = 't';
printf("%s",a);
}
Get a better compiler an tunigs...
main.c:17: warning: return type defaults to `int'
main.c:17: warning: function declaration isn't a prototype
main.c: In function `main':
main.c:18: warning: initialization discards qualifiers from pointer
target type
main.c:21: warning: assignment discards qualifiers from pointer target
type main.c:24: warning: control reaches end of non-void function
Perhaps you're the one who needs to get a better compiler? I see two
concerns:
1. It issues diagnostics for lines 17 through 24 from source that
only has 12 lines.
I have some lines above that are used for tests... If you are curious:
#define main main_
#if 0
#include "ed/inc/sysalloc.h"
#include "ed/inc/sys.h"
#undef assert
#define assert(e) ASSERT (e)
#else
#include <assert.h>
#endif
/* ---------------------------- appli
----------------------------------- */
2. FAR MORE IMPORTANTLY, it issues two "warning" messages that are
JUST PLAIN FLAT OUT WRONG (or are you compiling with a C++ compiler?).
Nope.
The warning messages for lines 18 and 21 state "discards qualifiers
from pointer target type". NO, THESE LINES DO NOT. The type of the
string literals "bcd" and "fgh" are array of char, and most
specifically not array of const char.
He he... Following good advices read here about string literals not
beeing mutable, I compile in paranoid mode. So, it's not 'plain wrong',
but rather 'plain safe'.
So what C compiler did you use that produced this incorrect messages?
gcc with -Wwrite-strings (among other switches)
Attempt to get rid of the warnings :
#include<stdio.h>
#include<string.h>
int main (void)
{
char const * a= "bcd";
strcpy(a,"hello");
a = "fgh";
a[0] = 't';
printf("%s",a);
return 0;
}
main.c: In function `main_':
main.c:18: warning: passing arg 1 of `strcpy' discards qualifiers from
pointer target type
main.c:20: warning: assignment of read-only location
meaning that there is still a problem.
Actually:
strcpy(a,"hello");
and
a[0] = 't';
*are* plain wrong because the string is not mutable.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"