CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    char* p="array"; it is valid but int *po=&20; is illegal. but why, how etc?

    tell me if i m wrong somewhere!!

    int *pint=&10;
    here pointer is pointing to an integer which is not even declared so it is not written in memory, that's why when we does such declaration it comes out as an error at compile time or run time.

    But what about this
    char *pchar="array";
    here "array" returns it's address(much similar to &10 as in previous case here array byself returns it's address) but this one is legal and works fine. But why? There was a similar declaration in the previous case but that failed, can i not use the same logics to say it is wrong as i said in previous case?. If yes then why it works? if no then where am i getting it wrong? If you are in doesn't know category then open your book and tell me what is it. :P

    thanks!
    Last edited by vkash; July 13th, 2012 at 08:57 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: char* p="array"; it is valid but int *po=&20; is illegal. but why, how etc?

    Quote Originally Posted by vkash
    int *pint=&10;
    here pointer is pointing to an ******* which is not even declared so it is not written in memory, that's why when we does such declaration it comes out as an error at compile time or run time.
    The expression &10 is illegal because it is illegal to take the address of an integer literal, according to the rules of the language. The "not even declared" thing is a possible reason why this rule exists, but it didn't have to be this way.

    Quote Originally Posted by vkash
    char *pchar="array";
    here "array" returns it's address(much similar to &10 as in previous case here array byself returns it's address) but this one is legal and works fine. But why? There was a similar declaration in the previous case but that failed, can i not use the same logics to say it is wrong as i said in previous case?
    In this context, the array is converted to a pointer to its first element, and this pointer is used to initialise pchar (which really should be a pointer to const char). So, no problem here. If you want to harp on the "not even declared" thing, then you can see it as the compiler arranging for space to be reserved for the string literal's data.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: char* p="array"; it is valid but int *po=&20; is illegal. but why, how etc?

    sorry but...
    doesn't get the point

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: char* p="array"; it is valid but int *po=&20; is illegal. but why, how etc?

    What do you not understand?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: char* p="array"; it is valid but int *po=&20; is illegal. but why, how etc?

    Quote Originally Posted by laserlight View Post
    The expression &10 is illegal because it is illegal to take the address of an integer literal, according to the rules of the language. The "not even declared" thing is a possible reason why this rule exists, but it didn't have to be this way.


    In this context, the array is converted to a pointer to its first element, and this pointer is used to initialise pchar (which really should be a pointer to const char). So, no problem here. If you want to harp on the "not even declared" thing, then you can see it as the compiler arranging for space to be reserved for the string literal's data.
    first of all i think there is little language gap, i doesn't understand all of your words(i am not good in english),mainly in last two lines of blue part :P
    secondly is it just a C++ hard written rule that this is illegal in integer but legal in char array? OR there is any logical answer of this?
    if it is just a rule then OK!! else i want to know reason.
    and i think u didn't give good reason for char array to be stored as a pointer? I already know the bold part, rest all is about compiler and if compiler is this much kind then why not it give a small byte of memory to integer(hahaha...).

    sorry if my words annoying u i mean if they doesn't make a sence...

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: char* p="array"; it is valid but int *po=&20; is illegal. but why, how etc?

    Quote Originally Posted by vkash
    i think u didn't give good reason for char array to be stored as a pointer?
    I did not state that a char array will be stored as a pointer. I stated that an array is converted to a pointer to its first element. The reason(s) for this behaviour has to do with it being inherited from C and C's historical relationship with assembly language.

    Quote Originally Posted by vkash
    is it just a C++ hard written rule that this is illegal in integer but legal in char array? OR there is any logical answer of this?
    You can accept it as a rule. I can think of a logical reason though: once you have an array, it is almost certain that a pointer will be involved (accessing an element of the array at least conceptually involves pointer arithmetic); but for an integer, you might not involve a pointer.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jul 2002
    Posts
    2,543

    Re: char* p="array"; it is valid but int *po=&20; is illegal. but why, how etc?

    To understand this, you need more practical, rather than philosophical approach.
    Code:
    char* p = "array";
    Handling this line, compiler places the string "array" to special read-only executable area, and initilalizes p with pointer to the beginning of this string.
    Code:
    x = x + 20;
    Constant 20 in this case exists only as part of executable code (inline integer constant in the resulting Assembly code), it does not have an address, and cannot be dereferenced. This is not logical exercise, this is just the way C++ compiler works. Knowing a bit of Assembly and understanding how C++ compller works can help you to solve such kind of logical problems.

  8. #8
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: char* p="array"; it is valid but int *po=&20; is illegal. but why, how etc?

    As a general rule, there's no implicit conversion from const to non-const.
    However, in C language "abc", which is a string literal, is of type array of (non-constat) chars.
    So, a declaration of this kind is perfectly legal in C:
    Code:
       char *p = "abc";
    C++, which is derived from C, tried for a while to preserve backward compatibility to legacy code written in C.
    So, even if in C++ a string literal is of type array of const char, the C++ standards, including ISO/IEC 14882:2003, still considered the above declaration as legal.
    C++03
    char* p = "abc"; // valid in C, deprecated in C++
    That has been changed in C++11.
    It just clearly states:
    C++11
    char* p = "abc"; // valid in C, invalid in C++
    That's all.
    Last edited by ovidiucucu; July 13th, 2012 at 01:18 PM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured