difference between "char*" and "const char*" 
Author Message
 difference between "char*" and "const char*"

Explain me please difference between "char*" and "const char*".Why i can't
write in the const char.
__________________________________________________________________ Alexander
Zolotov ICQ#: 167738419 Current ICQ status: + More ways to contact me
__________________________________________________________________


Fri, 13 May 2005 15:43:25 GMT  
 difference between "char*" and "const char*"

Quote:
> Explain me please difference between "char*" and "const char*". Why
> i can't write in the const char.

Nice question! Because ... it's the difference?

T.M.



Fri, 13 May 2005 15:45:23 GMT  
 difference between "char*" and "const char*"
Char* points to an array of chars that can be changed.
Where as const char* points to an array of constant chars. This is useful
for passing to other functions and ensuring they do not change the contents
of strings etc.

Regards
Simon Jefferies


Quote:
> Explain me please difference between "char*" and "const char*".Why i can't
> write in the const char.
> __________________________________________________________________
Alexander
> Zolotov ICQ#: 167738419 Current ICQ status: + More ways to contact me
> __________________________________________________________________



Fri, 13 May 2005 15:44:02 GMT  
 difference between "char*" and "const char*"

Quote:

> Explain me please difference between "char*" and "const char*".Why i
> can't write in the const char.

const stands for "constant", which means "unchanging".

Why would you think something marked as "unchanging" would be changeable?



Fri, 13 May 2005 16:30:37 GMT  
 difference between "char*" and "const char*"
Hello, Simon!
Thanks, but why if i write
Quote:
>>>const char* lpstrInitialDir;<<<
>>>strcpy(lpstrInitialDir,dir);;<<<

compiler give me error, so in this case
Quote:
>>>strcpy((LPTSTR)lpstrInitialDir,dir);;<<<

only warning.
You wrote  on Mon, 25 Nov 2002 07:44:02 -0000:

 SJ> Char* points to an array of chars that can be changed.
 SJ> Where as const char* points to an array of constant chars. This is
 SJ> useful for passing to other functions and ensuring they do not
 SJ> change the contents of strings etc.

 SJ> Regards



 >> Explain me please difference between "char*" and "const char*".Why i
 >> can't write in the const char.
 >> __________________________________________________________________
 SJ> Alexander
 >> Zolotov ICQ#: 167738419 Current ICQ status: + More ways to contact me
 >> __________________________________________________________________




Fri, 13 May 2005 19:03:16 GMT  
 difference between "char*" and "const char*"

Quote:
> Thanks, but why if i write
> >>>const char* lpstrInitialDir;<<<
> >>>strcpy(lpstrInitialDir,dir);;<<<
> compiler give me error, so in this case
> >>>strcpy((LPTSTR)lpstrInitialDir,dir);;<<<
> only warning.

And how about the following?

        char initdir[_MAX_PATH];
        strcpy(initdir,dir);

        0 errors
        0 warnings
        0 access violations
        0 memory leakage

Why do you strictly want to modify a constant string? The reason for
the "const" is that the string CANNOT be modified.

T.M.



Fri, 13 May 2005 19:16:37 GMT  
 difference between "char*" and "const char*"
Hello, Torsten!

Because i have not creating this string.I am only use.This string is member
structure OPENFILENAME.

You wrote  on 25 Nov 2002 12:16:37 +0100:


 >> Thanks, but why if i write
 >>>>> const char* lpstrInitialDir;<<<
 >>>>> strcpy(lpstrInitialDir,dir);;<<<
 >> compiler give me error, so in this case
 >>>>> strcpy((LPTSTR)lpstrInitialDir,dir);;<<<
 >> only warning.

 TM> And how about the following?

 TM>         char initdir[_MAX_PATH];
 TM>         strcpy(initdir,dir);

 TM>         0 errors         0 warnings         0 access violations
 TM> 0 memory leakage

 TM> Why do you strictly want to modify a constant string? The reason for
 TM> the "const" is that the string CANNOT be modified.

 TM> T.M.




Fri, 13 May 2005 20:17:06 GMT  
 difference between "char*" and "const char*"


Quote:
>Hello, Simon!
>Thanks, but why if i write
>>>>const char* lpstrInitialDir;<<<
>>>>strcpy(lpstrInitialDir,dir);;<<<
>compiler give me error, so in this case
>>>>strcpy((LPTSTR)lpstrInitialDir,dir);;<<<
>only warning.

The purpose of the cast - (LPTSTR) - is to tell the compiler the
programmer wants something of one type to be viewed as another type. The
programmer takes responsibility for the correctness of this! Your
extract is inadequate for other reasons. Try the following which was
compiled using VC6 SP5.

C:\WINNT\Temp\const> nl const.c
     1  /* const.c
     2   *
     3   * Dummy to show casting with const
     4   *
     5   * When       Who       What
     6   * 2002-11-25 W.Briscoe Original
     7   */
     8  #include <string.h> // For strcpy
     9
    10  #define WIN32_LEAN_AND_MEAN
    11  #pragma warning(disable: 4100 4115 4201 4214 4514)
    12  #include <windows.h> // for LPTSTR
    13  #pragma warning(default: 4100 4115 4201 4214)
    14
    15  #pragma warning(push, 4) // Maximise diagnostics
    16
    17  void foo0(const char *dir) {
    18    const char *lpstrInitialDir;
    19    strcpy(lpstrInitialDir, dir); // target points nowhere and can change nothing
    20  }
    21
    22  void foo1(const char *dir) {
    23    const char *lpstrInitialDir;
    24    strcpy((LPTSTR)lpstrInitialDir, dir); // points nowhere, mismatched override
    25  }
    26
    27  void foo3(const char *dir) {
    28    char target[255];
    29    const char *lpstrInitialDir;
    30    lpstrInitialDir = target;
    31    strcpy((LPTSTR)lpstrInitialDir, dir); // points somewhere, wrong override
    32  }
    33
    34  void foo4(const char *dir) {
    35    const char *lpstrInitialDir;
    36    lpstrInitialDir = "Hello Alex";
    37    strcpy((void*)lpstrInitialDir, dir); // undefined behavior, correct override
    38  }

C:\WINNT\Temp\const> gvim t.t

C:\WINNT\Temp\const> cl -nologo -c const.c
const.c
const.c(19) : warning C4090: 'function' : different 'const' qualifiers
const.c(19) : warning C4024: 'strcpy' : different types for formal and actual parameter 1
const.c(19) : warning C4700: local variable 'lpstrInitialDir' used without having been initialized
const.c(24) : warning C4700: local variable 'lpstrInitialDir' used without having been initialized

If you respond with a complete example, a better response is likely.

As to your original question:


Quote:
>Explain me please difference between "char*" and "const char*".Why i
can't
>write in the const char.

Because you said you would not do so!

Quote:
>__________________________________________________________________
Alexander
>Zolotov ICQ#: 167738419 Current ICQ status: + More ways to contact me
>__________________________________________________________________

What does this mean?

I suspect you do not yet know what a pointer is.
You can   write to   a pointer when it has no value;
you can't write with a pointer when it has no value.
--
Walter Briscoe



Fri, 13 May 2005 20:08:47 GMT  
 difference between "char*" and "const char*"
Hello, Walter!

I can't undarstand, why

Quote:
>>>const char *lpstrInitialDir;
>>>lpstrInitialDir = "Hello Alex";
all right, but
>>>const char *lpstrInitialDir;

 >>>strcpy(lpstrInitialDir, "Hello Alex");
wrong

You wrote  on Mon, 25 Nov 2002 12:08:47 +0000:



 WB> writes
 >> Hello, Simon!
 >> Thanks, but why if i write
 >>>>> const char* lpstrInitialDir;<<<
 >>>>> strcpy(lpstrInitialDir,dir);;<<<
 >> compiler give me error, so in this case
 >>>>> strcpy((LPTSTR)lpstrInitialDir,dir);;<<<
 >> only warning.
 WB> The purpose of the cast - (LPTSTR) - is to tell the compiler the
 WB> programmer wants something of one type to be viewed as another type.
 WB> The programmer takes responsibility for the correctness of this!
 WB> Your extract is inadequate for other reasons. Try the following
 WB> which was compiled using VC6 SP5.

 WB> C:\WINNT\Temp\const> nl const.c      1  /* const.c      2   *
 WB>      3   * Dummy to show casting with const      4   *
 WB>      5   * When       Who       What      6   * 2002-11-25 W.Briscoe
 WB> Original      7   */
 WB>      8  #include <string.h> // For strcpy      9     10  #define
 WB> WIN32_LEAN_AND_MEAN     11  #pragma warning(disable: 4100 4115 4201
 WB> 4214 4514)
 WB>     12  #include <windows.h> // for LPTSTR     13  #pragma
 WB> warning(default: 4100 4115 4201 4214)
 WB>     14     15  #pragma warning(push, 4) // Maximise diagnostics
 WB> 16     17  void foo0(const char *dir) {
 WB>     18    const char *lpstrInitialDir;
 WB>     19    strcpy(lpstrInitialDir, dir); // target points nowhere and
 WB> can change nothing     20  }
 WB>     21     22  void foo1(const char *dir) {
 WB>     23    const char *lpstrInitialDir;
 WB>     24    strcpy((LPTSTR)lpstrInitialDir, dir); // points nowhere,
 WB> mismatched override     25  }
 WB>     26     27  void foo3(const char *dir) {
 WB>     28    char target[255];
 WB>     29    const char *lpstrInitialDir;
 WB>     30    lpstrInitialDir = target;
 WB>     31    strcpy((LPTSTR)lpstrInitialDir, dir); // points somewhere,
 WB> wrong override     32  }
 WB>     33     34  void foo4(const char *dir) {
 WB>     35    const char *lpstrInitialDir;
 WB>     36    lpstrInitialDir = "Hello Alex";
 WB>     37    strcpy((void*)lpstrInitialDir, dir); // undefined
 WB> behavior, correct override     38  }

 WB> C:\WINNT\Temp\const> gvim t.t

 WB> C:\WINNT\Temp\const> cl -nologo -c const.c const.c const.c(19) :
 WB> warning C4090: 'function' : different 'const' qualifiers const.c(19)
 WB> : warning C4024: 'strcpy' : different types for formal and actual
 WB> parameter 1 const.c(19) : warning C4700: local variable
 WB> 'lpstrInitialDir' used without having been initialized const.c(24) :
 WB> warning C4700: local variable 'lpstrInitialDir' used without having
 WB> been initialized

 WB> If you respond with a complete example, a better response is likely.

 WB> As to your original question:


 WB> writes
 >> Explain me please difference between "char*" and "const char*".Why i
 WB> can't
 >> write in the const char.
 WB> Because you said you would not do so!

 >> __________________________________________________________________
 WB> Alexander
 >> Zolotov ICQ#: 167738419 Current ICQ status: + More ways to contact me
 >> __________________________________________________________________
 WB> What does this mean?

 WB> I suspect you do not yet know what a pointer is.
 WB> You can   write to   a pointer when it has no value;
 WB> you can't write with a pointer when it has no value.
 WB> --
 WB> Walter Briscoe




Fri, 13 May 2005 22:12:36 GMT  
 difference between "char*" and "const char*"
    In the first, (lpstrInitialDir = "Hello Alex";) the value of the pointer
changes

    In the second, (strcpy), the value of what is pointed to changes.

The demonstrate further:
    char *p1,*p2,*p3;
    p1 = "Hello World";
    p2 = p1;
    p3 = p1;

    p2 = "Greetings, Earthlings";
    // p1 still points to "Hello World"
    strcpy(p3, "Bye, All");
    // p1 now points to "Bye, All" -- however, since I've just overwritten a
text literal, it may have crashed on the strcpy.

--
Truth,
James Curran
www.NovelTheory.com  (Personal)
www.NJTheater.com   (Professional)
www.aurora-inc.com   (Day job)


Quote:
> Hello, Walter!

> I can't undarstand, why

> >>>const char *lpstrInitialDir;
> >>>lpstrInitialDir = "Hello Alex";
> all right, but
> >>>const char *lpstrInitialDir;
>  >>>strcpy(lpstrInitialDir, "Hello Alex");
> wrong



Fri, 13 May 2005 22:35:15 GMT  
 difference between "char*" and "const char*"

Quote:

> Hello, Simon!
> Thanks, but why if i write
> >>>const char* lpstrInitialDir;<<<
> >>>strcpy(lpstrInitialDir,dir);;<<<
> compiler give me error, so in this case
> >>>strcpy((LPTSTR)lpstrInitialDir,dir);;<<<
> only warning.

Because when you use a cast, you're telling the compiler, "I know I'm
not supposed to do this, but trust me, I know what I'm doing."  If you
misuse casts, you're liable to get a crash when you run the program.

e.g.
    char const * lpstrInitialDir = "C:\\";
    strcpy((LPSTR) lpstrInitialDir, dir);

This will, I believe, result in an access violation.

--
Craig Powers
MVP - Visual C++



Fri, 13 May 2005 22:57:28 GMT  
 difference between "char*" and "const char*"


Quote:
> Hello, Walter!

> I can't undarstand, why

> >>>const char *lpstrInitialDir;
> >>>lpstrInitialDir = "Hello Alex";
> all right, but
> >>>const char *lpstrInitialDir;
>  >>>strcpy(lpstrInitialDir, "Hello Alex");
> wrong

In the latter case, you also didn't allocate any memory for lpstrInitialDir
(assuming the above is actual un-snipped code). lpstrInitialDir doesn't
point anywhere initially.

A point to keep in mind: "char*" will cast silently and cleanly to "const
char*". If a function or structure takes a const pointer you can assign a
non-const pointer it (or pass it to a function) without problems. Example:

void Function(const char* pSomeText);
//...

char myText[256];
strcpy(myText, "Hello Alex");
Function(myText);            // <- works fine; casts to const char* for you

- or even better -

CString myText;
myText = "Hello Alex";

Function(myText);            // <- works fine; casts to const char* for you

Jay



Sat, 14 May 2005 01:12:23 GMT  
 difference between "char*" and "const char*"
Alex,
Please don't top-post. That is, put your reply at the top of the posting
you are answering. It makes the total picture difficult to understand.



Quote:
>Hello, Walter!

>I can't undarstand, why

>>>>const char *lpstrInitialDir;
>>>>lpstrInitialDir = "Hello Alex";
>all right, but
>>>>const char *lpstrInitialDir;
> >>>strcpy(lpstrInitialDir, "Hello Alex");
>wrong

[snip]
I have cut the rest of your posting as it is not needed to understand
this. I am going to try drawing you some pictures. I REALLY think you
need to find a beginner's C book. I have no recommendation now.

                     ._._._._.
0x????????           |?|?|?|?|          const char * lpstrInitialDir;

                     ._._._._.
0x????????           |A|B|C|D|          lpstrInitialDir = "Hello Alex";
                         |
   ----------------------+
   |                 ._._._._.
0x0A0B0C0D           |H|E|L|L|...

                     ._._._._.
0x????????           |A|B|C|D|          strcpy(lpstrInitialDir, "oops");
                         |
   ----------------------+
   |                 ._._._._.
0x0A0B0C0D           |o|o|p|s|...

The last operation has overwritten the string literal "Hello Alex".
It is similar to 5 = 6;
It is illegal. The const prevents you from doing it.
In my last posting, I asked you to construct a compilable example.
Why did you not do so.
Try stepping through such code in a de{*filter*} or put printf statements in
your code to see what was happening.
I am beginning to become irritated as you are doing less work in getting
an understanding of your question than I.
Do you have a VC compiler. Please show its use!
--
Walter Briscoe



Sat, 14 May 2005 01:04:52 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Why not "const char *strerror(int);"??

2. CComBSTR into a "const char *"

3. Difference between "char *arr" and "char arr[]"

4. char foo[]=""; vs char* foo="";

5. char a[] = "abc"; char* a = "abc";

6. merits of "#define", "const", and "enum" ???

7. decode/unescape "%uxxxx" char in VC++

8. char a[] = ("x");

9. Any != between (char) NULL and ""

10. is this allowed char *name = "bryan"

11. Delete "/" from char pointer

12. What is "unsigned char data[0];"

 

 
Powered by phpBB® Forum Software