LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-27-2009, 02:22 PM   #1
cpthk
Member
 
Registered: Aug 2009
Posts: 31

Rep: Reputation: 15
How to return empty char * ?


Hi:

Here is my code:
Code:
char * func1(){

...
...
return "";
}
This code give me warning "deprecated convertion from string to char *
I then try:
Code:
char * func1(){

...
...
return '';
}
This gives me error "empty character constant", how do I fix this?
 
Old 08-27-2009, 02:27 PM   #2
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Depending on how you're using the return value, you likely either want to return:
"\0"

or

NULL
 
Old 08-27-2009, 02:38 PM   #3
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
First you need to make yourself clear why you get this warning. It's appearing because you return a constant string literal while the function should return a (non const) char pointer.

I can think of two ways to fix this, one in an ugly way (but leaving the function signature intact), the other the much better way - assuming you want a valid c string as returned data.

dirty (static data as returned data):
Code:
char * func1()
{
    // ...
    
    static char empty[1] = { '\0' };
    return empty;
}

better (const correctness, valid string literal)
Code:
const char * func1()
{
    // ...
    return "";
}
 
Old 08-27-2009, 03:28 PM   #4
cpthk
Member
 
Registered: Aug 2009
Posts: 31

Original Poster
Rep: Reputation: 15
so anything like "", gcc treats it as constant?

Another question, if I do "", does gcc treat it as string or char * ? I know in vc++, it treats "" as char *.
 
Old 08-27-2009, 04:49 PM   #5
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
Yes, GCC will treat it as 'const char *'. MSVC has some settings to treat string literals as constant too IIRC (and store them in read-only memory). Are you sure 'char *' is the default setting there ? Would be a bit surprising, but MSVC has always been a sort of black box to me concerning some of its settings.
 
Old 08-27-2009, 05:30 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,282

Rep: Reputation: 172Reputation: 172
what are you trying to do?

what you are returning is a constant string, i.e. compiled in.
the fact that you specified it ""
means it's there at compilation, so is, of course, constant
 
Old 08-27-2009, 06:02 PM   #7
cpthk
Member
 
Registered: Aug 2009
Posts: 31

Original Poster
Rep: Reputation: 15
I guess MSVC treat "" as const char *. I said char * just to say MSVC doesn't treat "" as std::string
 
Old 08-27-2009, 06:19 PM   #8
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by fantas View Post
Code:
char * func1()
{
    // ...
    
    static char empty[1] = { '\0' };
    return empty;
}
Quick aside - the biggest reason that this is code above is a bad technique is that it is actually functionally different from:

Code:
char *func1()
{
    // ...
    
    return "\0";
}
The reason being this code following:

Code:
{
   char *p = func1();
   *p = 'A';
}
Not only will code using the static variable not segfault, but it'll allow you to reassign the value of empty. At least on modern systems with modern gcc, you should get a segfault due to writing to a read-only codespace. Both aren't good, but one can lead to "dangerous things" where the other (hopefully) just crashes.
 
Old 08-27-2009, 06:30 PM   #9
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
I agree - both are bad, but I at least made a mention of it not being preferable, while you gave it as a solution to the OP. Apart from that, your code will still spawn this warning mentioned in the first post ...

And "dangerous things" ??? Oh, please ... *facepalm*

Last edited by fantas; 08-27-2009 at 06:46 PM.
 
Old 08-27-2009, 09:27 PM   #10
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by cpthk View Post
so anything like "", gcc treats it as constant?

Another question, if I do "", does gcc treat it as string or char * ? I know in vc++, it treats "" as char *.
From the C++ standard 2.13.4 String Literal

Quote:
A string literal is a sequence of characters (as defined in 2.13.2) surrounded by double quotes, optionally beginning with the letter L, as in "..." or L"...". A string literal that does not begin with L is an ordinary string literal, also referred to as a narrow string literal. An ordinary string literal has type “array of n const char” and static storage duration, where n is the size of the string as defined below, and is initialized with the given characters. A string literal that begins with L, such as L"asdf", is a wide string literal. A wide string literal has type “array of n const wchar_t” and has static storage duration, where n is the size of the string as defined below, and is initialized with the given characters.
Or put another way anything in double quotes should be const char *.

If you plan to return a sting literal then the signature of your function should reflect that the return value is a const. If you are returning a string that can be modified but you want to be able to return an empty string return 0, the null pointer, and then test that the return value points to a valid address before doing anything with the value.
 
Old 08-28-2009, 06:19 AM   #11
cpthk
Member
 
Registered: Aug 2009
Posts: 31

Original Poster
Rep: Reputation: 15
so what is the final answer if I really need to return char * instead of const char * ?
 
Old 08-28-2009, 07:31 AM   #12
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by cpthk View Post
so what is the final answer if I really need to return char * instead of const char * ?
Returning 0 or NULL would be the best action so that you do not try and modify the string pointed to but you can use the same way as you are even though the quote by graemef states it is constant. As IIRC a string literal will deform to a char* for C compatibility reasons. This behaviour is depreciated yet not disallowed.

Last edited by dmail; 08-28-2009 at 07:43 AM. Reason: added depreciated comment
 
Old 08-28-2009, 09:01 AM   #13
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
Quote:
Originally Posted by cpthk View Post
so what is the final answer if I really need to return char * instead of const char * ?
The question remains why would you have to return a non-const char * if not for backwards compatibility to some badly coded interfaces ?
Or is it that you want to be able to modify the char * ?
 
Old 08-28-2009, 09:50 AM   #14
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by fantas View Post
while you gave it as a solution to the OP.
Hrrm? No I didn't. I said either return NULL or return "\0" directly.

Quote:
Apart from that, your code will still spawn this warning mentioned in the first post ...
Code:
[10:39:29][aconole@weston:~]
$ cat t.c
#include <stdio.h>

char *func1()
{
    // ...
    return "\0";
}

int main()
{
    char *f = func1();

    *f = 'a';

    return 0;
}
[10:39:35][aconole@weston:~]
$ gcc -o t t.c
[10:39:39][aconole@weston:~]
$ ./t
Segmentation fault
It does not - (Note the crash... it's not guaranteed by C99, but will work this way on most linux machines compiled with gcc).

With a static member:
Code:
[11:08:00][aconole@weston:~]
$ cat t.c
#include <stdio.h>

int global = 0;

char *func1()
{
    static char empty[1] = {'\0'};

    return &empty;
}

int main()
{
    char *f;

    f = func1();

    *f = 'a';

    return 0;
}
[11:09:11][aconole@weston:~]
$ gcc -o t t.c
t.c: In function ‚func1‚:
t.c:9: warning: return from incompatible pointer type
[11:09:18][aconole@weston:~]
$ ./t
[11:09:21][aconole@weston:~]
$
Note - the warning, because we are not returning a correct pointer type (should be const), and the fact that this code did not crash, but instead, has altered the value of the variable empty.

Relevant sections are
6.2.4, 6.2.5, and 6.2.6 of C99

Last edited by orgcandman; 08-28-2009 at 10:18 AM. Reason: Adding some output and some standards references
 
Old 08-28-2009, 09:56 AM   #15
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
Quote:
Originally Posted by orgcandman View Post
Hrrm? No I didn't. I said either return NULL or return "\0" directly.
So what's the difference ?

Quote:
Originally Posted by orgcandman View Post
Code:
[10:39:35][aconole@weston:~]
$ gcc -o t t.c
[10:39:39][aconole@weston:~]
$
It does not.
Of course not, without enabling all warnings.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
C++ Return Char Arrays (not with pointers) sadarax Programming 4 02-03-2006 04:49 PM
Postfix: why is 'Return-Path' of bounce message empty Chowroc Linux - Networking 1 12-28-2005 03:52 AM
php scripts return empty pages ararag Linux - Software 1 02-23-2004 10:46 AM
empty char in java nephilim Programming 13 09-24-2003 02:29 AM


All times are GMT -5. The time now is 01:22 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration