Hi,
I tried to return a pointer to a constant string, but the compiler
gives the following warning if a cast is not used:
warning: assignment from incompatible pointer type
This is the code:
const char msg[] = "Test message";
const char *message(void) {
return msg;
}
int main(void){
const char * str;
str = (const char *)message;
str = (char *)message;
str = message; /* GCC warning! */
str = (const char *)msg;
str = (char *)msg;
str = msg;
return 0;
}
Oddly, GCC only gives the warning if no cast is used, but it doesn't
complain if the cast discards the const qualifier. Is this behavior
OK? I'm using GCC 4.1.2. Thanks!
Best regards,
Santi
Sep 17 '07
23 4788
Richard Heathfield <rj*@see.sig.in validwrites:
Eric Sosman said:
>Richard wrote On 09/19/07 12:54,:
>>[...] in what cases are &msg[0] and msg not the same in real live systems where they are 32 or 64 bit pointers?
In all cases.
Not so. &msg[0] and msg are identical when their value is used in an
expression.
When their value is used, yes, but the question was whether they're
the same in general. (The sizes of pointers are irrelevant.)
When they're the operand of a sizeof operator, their values are not
used, but they differ. Likewise for the operand of unary "&"
(but as has been pointed out, '&&msg[0]' is a constraint violation).
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Richard wrote:
>
Eric Sosman <Er*********@su n.comwrites:
Richard wrote On 09/19/07 12:54,:
Keith Thompson <ks***@mib.orgw rites: [...] Since msg is declared as an array, the expression '&msg' is the address *of the array*, whereas the expression 'msg' (in most contexts) yields the address of the arrsy's first element, the same as '&msg[0]'.
I'm a bit sleepy at the moment and understand the above,
but in what
cases are &msg[0] and msg not the same in real live
systems where they
are 32 or 64 bit pointers?
In all cases.
In what cases are the VALUES not the same.
Given: const char msg[] = "Test message";
the answer to your question is that
(&msg[0] == msg) equals one, on all C implementations .
I am not talking the types.
hence I mentioned the pointers or addresses.
He was talking types.
The point he was making is that
though ((char *)&msg == (char *)msg) equals one,
(&msg == msg) is undefined.
--
pete
Keith Thompson said:
Richard Heathfield <rj*@see.sig.in validwrites:
>Eric Sosman said:
>>Richard wrote On 09/19/07 12:54,: [...] in what cases are &msg[0] and msg not the same in real live systems where they are 32 or 64 bit pointers?
In all cases.
Not so. &msg[0] and msg are identical when their value is used in an expression.
When their value is used, yes, but the question was whether they're
the same in general.
Um, no, the question was: "in what cases are &msg[0] and msg not the same
in real live systems where they are 32 or 64 bit pointers?"
Clearly the pointer-size thing is of no interest to comp.lang.c, since the
cases where &msg[0] and msg are the same or not the same is nothing to do
with pointer size as far as we're concerned. So it resolves to: "in what
cases are &msg[0] and msg not the same?" Nothing in there about "in
general". Eric's answer to this (which he acknowledges is based on a
misreading of what is probably a typo!) was "in all cases", which is
clearly not correct, because there is a case where &msg[0] and msg are the
same.
(The sizes of pointers are irrelevant.)
Agreed.
When they're the operand of a sizeof operator, their values are not
used, but they differ. Likewise for the operand of unary "&"
(but as has been pointed out, '&&msg[0]' is a constraint violation).
Yes, there are indeed cases where they differ, but "in some cases" != "in
all cases".
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Richard Heathfield <rj*@see.sig.in validwrites:
Keith Thompson said:
>Richard Heathfield <rj*@see.sig.in validwrites:
>>Eric Sosman said: Richard wrote On 09/19/07 12:54,: [...] in what cases are &msg[0] and msg not the same in real live systems where they are 32 or 64 bit pointers?
In all cases.
Not so. &msg[0] and msg are identical when their value is used in an expression.
When their value is used, yes, but the question was whether they're the same in general.
Um, no, the question was: "in what cases are &msg[0] and msg not the same
in real live systems where they are 32 or 64 bit pointers?"
Sorry, I misunderstood your correction to be more general that it
obviously was. (I should spend more time reading and thinking when
something you post looks wrong; it happens, but it's not the way to
bet.)
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Chris Mantoulidis |
last post by:
I see some really weird output from this program (compiled with GCC
3.3.2 under Linux).
#include <iostream>
using namespace std;
int main() {
char *s;
s = "test1";
cout << "s = " << s << " and &s = " << &s << "\n";
|
by: wonderboy |
last post by:
Hey guys,
I have a simple question. Suppose we have the following
functions:-
//-----My code starts here
char* f1(char* s)
{
char* temp="Hi";
return temp;
|
by: kiran.agashe |
last post by:
Hi,
Please refer program below:
#include <string>
#include <cstdio>
using namespace std;
const char* f();
main()
{
|
by: fieldfallow |
last post by:
Hello all,
Before stating my question, I should mention that I'm fairly new to C.
Now, I attempted a small demo that prints out the values of C's numeric
types, both uninitialised and after assigning them their maximum defined
values. However, the output of printf() for the long double 'ld' and the
pointer of type void 'v_p', after initialisation don't seem to be right.
The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
|
by: djcredo |
last post by:
Hey all,
I want to return a pointer to a struct. Here is what I'lm trying to do:
struct Position{
int x;
int y;
};
Position* GraphicTag::getRenderCentre(){
| |
by: MC felon |
last post by:
how do we return strings or arrays from a function? i tried..
char some_func() //where i thought char would tell the compiler
that the return is a string
{
char str;
//something;
return str;
}
|
by: Adam |
last post by:
Hi,
I'd like to return an (arbitrary length) string array from a function so
that after calling the array I've got a list of strings I can access.
After much perusing of the internet I found a related answer here (by
Eric Sosman) which involved creating an array of pointers and using
that, so it looks something like:
|
by: darren |
last post by:
Hi everybody, have a quick look at this code:
=====
=====
int main(void) {
string msg;
makeString(msg);
cout << "back in main, result = " << msg << endl;
|
by: simonl |
last post by:
Hi,
I've been given the job of sorting out a crash in our product for
which we have the crash information and an avi of the event (which
can't possibly match but more of that later...) (btw this is a single
threaded VC9 / win32 app)
The call stack for the bug effectively goes
void* myBuf;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |