Please clear this :-

I am new to this forum.I hope I am posting the question in the right place.

char qu[]={"Nitin","Gupta"}; // Invalid
char *qu[]={"Nitin","Gupta"}; // Valid

Why is it so?
What actually is *qu[] type declaration?

Thanks.I have a pile of C++ questions.Can I ask them here?

char* is a type of string literals (it should be const char*, but whatever). You could do
1
2
typedef char* cstring;
cstring qu[] = {"...", "..."};

A char pointer is the address of the memory location where your string begins. A char is just a single character. A valid piece of code would be char qu[] = {'N', 'G'};.
Thank You
In standard c++, the type of a string literal is technically const char*.But C++ provides an automatic conversion to char*.
Please clear this line.
It says that even though the type of "hello" is const char* (that is, the contents of "hello" cannot be modified), the line char* ptr = "hello"; will compile fine. Although it's not a good thing to do and the same wouldn't work with const and non-const pointers in any other case. Reasons why someone thought this was a good idea are unknown to me. There is nothing to gain here. Unless you hate typing const.
Last edited on
Allowing char*s to point to string literals is an evil-ness to support backwards compatibility with C. :-( [1]

C++ code should always, always, always use const char*.

1
2
3
4
5
6
7
8
int main()
{
    char* evil = "evil thing to do"; // literal is in const memory

    evil[4] = '!'; // DIE, DIE, DIE!!! (4 sounds like Japanese 'shi' [2])

    return 0;
}


Running with VC++ 2008:

First-chance exception at 0x00411398 in temp_test.exe: 0xC0000005: Access violation writing location 0x00415740.
Unhandled exception at 0x00411398 in temp_test.exe: 0xC0000005: Access violation writing location 0x00415740.

Andy

[1] "String literals inside functions: automatic variables or allocated in heap?"
From http://stackoverflow.com/questions/173681/string-literals-inside-functions-automatic-variables-or-allocated-in-heap

It is undefined behaviour to modify a string literal, and is most likely the cause of the crash in your program (ISO C++: 2.13.4/2). The standard allows for a conversion from a string literal to char* for backwards compatibility to C and you should only have that conversion in your code if you absolutely need it.

[2] "Are there any unlucky numbers in Japan?"
http://japanese.about.com/blqow7.htm
Last edited on
PS If you use Windows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <iostream>
using namespace std;

int main()
{
    cout << boolalpha;

    char* evil = "evil thing to do"; // literal is in const memory

    // Verify that the calling process has read access to the specified range of memory. [3]
    bool bCanRead  = (FALSE == IsBadReadPtr(evil, sizeof(evil)));

    // Verify that the calling process has write access to the specified range of memory. [3]
    bool bCanWrite = (FALSE == IsBadWritePtr(evil, sizeof(evil)));

    cout << "Can read  = " << bCanRead  << endl;
    cout << "Can write = " << bCanWrite << endl;

    return 0;
}


Can read  = true
Can write = false


Andy

[3] But you shouldn't use IsBadReadPtr/IsBadWritePtr is real code. From MSDN

Important This function is obsolete and should not be used. Despite its name, it does not guarantee that the pointer is valid or that the memory pointed to is safe to use. For more information, see Remarks on this page.

But it is safe to use in this tiny example! (These functions try to read from/write to the memory location specified, and hande any exceptions that are raised; Windows structured exceptions rather than C++ ones.)
Last edited on
Thanks Everyone
Topic archived. No new replies allowed.