a few doubts!

E

Emmanuel Delahaye

Jack Klein wrote on 02/05/05 :
#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?"
 
K

Keith Thompson

Emmanuel Delahaye said:
Jack Klein wrote on 02/05/05 : [...]
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'.

On the one hand, the warning messages are diagnostics. A conforming C
compiler is free to issue whatever diagnostics it likes, for whatever
reason it likes. Diagnostics are required in some contexts, but
they're never forbidden, and they're never required (by the standard)
to be meaningful, or even accurate. And it's not inappropriate to
issue a warning when a string literal is used in a way that might
result in it being modified.

On the other hand, those particular warnings either are not strictly
correct, or refer to some language that's not quite C. In C, no
qualifiers are discarded by
char *a = "bcd";
gcc with -Wwrite-strings (among other switches)

That causes string literals to be treated as "const char*" rather than
"char*" -- thus the language being compiled is not quite C. (IMHO
it's an improvement over standard C, but that's not really the point.)
On the other hand, as far as I can tell, all the diagnostics enabled
by this are warnings, so it doesn't prevent any valid program from
being successfully compiled.

It would be nice if gcc were a little smarter about -Wwrite-strings
(i.e., if it triggered more specific warning messages rather than just
pretending that string literals are const).
 
S

Steffen Fiksdal

#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);
}


now, in TC there is absolutely no error .....i thought it
should........coz' when i declare a as a char * and assign it to some
string then it should be a constant and cannot do things like a[0] = '4'
and stuff........infact the entire thing here works properly....so why
should it work properly??

char* a= "bcd" creates a pointer to a char (not a constant pointer) which
points to a string constant. If you change the content of this string
constant undefined behaviour will occur. (Something like calling free()
twice, which the compiler also accepts...

If you need to assign like this and change the content afterwards., use:
char a[] = "bcd";
Then you can change the content, but be aware not to overflow....

Steffen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top