By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,920 Members | 1,946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,920 IT Pros & Developers. It's quick & easy.

Question about odd syntax: char* c = {"Text"};

P: n/a
Hi,

I have run across the some strange syntax, to which I do not
understand the meaning:

const char* xmlHeader = {"<?xml"};

(specifically, this can be found in "tinyxmlparse.cpp", Line 828, from
the "tinyxml" project on Sourceforge at
<http://sourceforge.net/projects/tinyxml/>). I should note that this
does compile with gcc v4.0.1.

Originally, I thought this syntax would only make sense if we were
dealing with char*[] or char**. My second interpretation was that this
notation essentially dynamically allocates an array of chars,
initialized with "<?xml". However, this does not seem to hold, based on
the following test.

#include <iostream>

using namespace std;

int main ()
{
const char* a = "Test 1 has address ";
char* b = new char[12];
const char* c = {"Test 3 has address "};
char d[] = "Test 4 has address ";
char e[12];

cout << "a: " << a << ((const void *) a) << endl;
cout << "b: Test 2 has address " << ((const void *) b) << endl;
cout << "c: " << c << ((const void *) c) << endl;
cout << "d: " << c << ((const void *) d) << endl;
cout << "e: Test 5 has address " << ((const void *) e) << endl;

// delete [] c;
// delete c;
}

In my particular instance, I get

a: Test 1 has address 0x2eb0
b: Test 2 has address 0x500150
c: Test 3 has address 0x2ec4
d: Test 3 has address 0xbfffed84
e: Test 5 has address 0xbfffed98

This implies to me that the {}s do not actually do anything, as both a
and c do not appear to be pointing at something on the heap (like d and
e) or on the stack (like b). That can be further supported by
uncommenting either one of the delete's at the end, which gives me

malloc: *** Deallocation of a pointer not malloced: 0x2ec4; This
could be a double free(), or free() called with the middle of an
allocated block; Try setting environment variable MallocHelp to see
tools to help debug

My question is, does this notation actually serve any purpose? And while
I am at it, is there any nice notation to do in one statement what I
thought this did, i.e. something like

char temp_literal[] = "<?xml";
char* temp_pointer = new char[strlen (temp_literal)]";
strcpy (temp_pointer, temp_literal);
const char* c = temp_pointer;

Thank you for any help,
Bruce
May 26 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Bruce Adcock wrote:
I have run across the some strange syntax, to which I do not
understand the meaning:

const char* xmlHeader = {"<?xml"};

(specifically, this can be found in "tinyxmlparse.cpp", Line 828, from
the "tinyxml" project on Sourceforge at
<http://sourceforge.net/projects/tinyxml/>). I should note that this
does compile with gcc v4.0.1.


The Standard says

"If T is a scalar type, then a declaration of the form
T x = { a };
is equivalent to
T x = a;
"

HTH

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 26 '06 #2

P: n/a
Bruce Adcock <br**********@gmail.com> wrote:
is there any nice notation to do in one statement what I
thought this did, i.e. something like

char temp_literal[] = "<?xml";
char* temp_pointer = new char[strlen (temp_literal)]";
This is obviously not real code because of the syntax error, but be
aware that in real code you need strlen() + 1.
strcpy (temp_pointer, temp_literal);
const char* c = temp_pointer;


Since you have a const char*, this indicates that you do not intend to
change the chars that are pointed to, so why not just

const char* c = "<?xml";

?

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 26 '06 #3

P: n/a
Victor Bazarov posted:

The Standard says

"If T is a scalar type, then a declaration of the form
T x = { a };
is equivalent to
T x = a;
"

Didn't know that... it might come in handy for templates.
-Tomás
May 26 '06 #4

P: n/a
Marcus Kwok wrote:
Bruce Adcock <br**********@gmail.com> wrote:
is there any nice notation to do in one statement what I
thought this did, i.e. something like

char temp_literal[] = "<?xml";
char* temp_pointer = new char[strlen (temp_literal)]";


This is obviously not real code because of the syntax error, but be
aware that in real code you need strlen() + 1.
strcpy (temp_pointer, temp_literal);
const char* c = temp_pointer;


Since you have a const char*, this indicates that you do not intend to
change the chars that are pointed to, so why not just

const char* c = "<?xml";

?


'c' itself is probably intended to stay as is, as well, so

const char* const c =

is better, and if that's so, then

const char c[] =

is the best, IMO.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 26 '06 #5

P: n/a
Marcus Kwok wrote:
Bruce Adcock <br**********@gmail.com> wrote:
is there any nice notation to do in one statement what I
thought this did, i.e. something like

char temp_literal[] = "<?xml";
char* temp_pointer = new char[strlen (temp_literal)]";


This is obviously not real code because of the syntax error, but be
aware that in real code you need strlen() + 1.
strcpy (temp_pointer, temp_literal);
const char* c = temp_pointer;


Since you have a const char*, this indicates that you do not intend to
change the chars that are pointed to, so why not just

const char* c = "<?xml";

?


Hi,

Sorry about the errors (and thanks also to Victor Bazarov for the
quick reply). The second question mainly came about after trying to
understand the intent of the original code, and is more theoretical than
anything in particular I am trying to do right now. I _would_ however be
concerned about intermixing pointers to literals with pointers to
pointers to dynamic arrays, especially when it comes to using delete [].

Thanks a lot,
Bruce
May 26 '06 #6

P: n/a

Bruce Adcock wrote:
char temp_literal[] = "<?xml";
char* temp_pointer = new char[strlen (temp_literal)]";
strcpy (temp_pointer, temp_literal);
const char* c = temp_pointer;

Note that you should allocate 1 byte more...

Yes, there is a "special" notation for that... The wonderful concept of
procedural programming is more than 40 years old...
char* DuplicateString(const char* source) {
std::size_t size=strlen(source);
char* const buffer=new char[size+1]
memcpy(buffer, source, size+1); // copy all bytes of the string,
including the null terminator.
return buffer;
}

Now, you just have to do:

char* NewString=DuplicateString("hello world");
// And don't forget to release the memory (and catch the potential
std::bad_alloc exception)
delete[] NewString;

Of course, it is even better to use more modern technologies such as
std::string.

May 27 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.