confused between declaration & definition

Discussion in 'C++' started by arnuld, Nov 8, 2006.

  1. arnuld

    arnuld Guest

    1) int i = 3;
    2.) int* pi;
    3.) int* pi2 = &i
    4.) char* pc;
    5.) char c;
    6) char c2 = 'a'
    7.) char* pc2 = &c2
    8.) char* ps = "Stroustroup";
    9.) extern double d;

    2, 4 & 9 are the only declarations here. right ?

    i know that 1, 3, 5, 6, 7, 8 are definitions but can we call them
    declarations too?

    Is 2 a legal declaration?

    i dont understand the difference between 7 & 8.
    arnuld, Nov 8, 2006
    #1
    1. Advertising

  2. * arnuld:
    > 1) int i = 3;
    > 2.) int* pi;
    > 3.) int* pi2 = &i
    > 4.) char* pc;
    > 5.) char c;
    > 6) char c2 = 'a'
    > 7.) char* pc2 = &c2
    > 8.) char* ps = "Stroustroup";
    > 9.) extern double d;
    >
    > 2, 4 & 9 are the only declarations here. right ?


    All are declarations, but only 9 is a pure declaration (not a definition).


    > i know that 1, 3, 5, 6, 7, 8 are definitions but can we call them
    > declarations too?


    Yes. Any definition is a declaration.


    > Is 2 a legal declaration?


    Yes.


    > i dont understand the difference between 7 & 8.


    7 declares a pointer to char and initializes it with the address of a
    single char.

    8 declares a pointer to char and initializes it with the address of the
    first char in a zero-terminated sequence of chars (the string "Stroustrup").

    8 is bad form (because it means you can try to modify a string literal
    without the compiler detecting that error). It's only allowed in order
    to have backwards compatibility with old C. Except for interfacing to
    old C code that requires it, you should write

    char const* ps = "Stroustrup";

    or equivalently

    const char* ps = "Stroustrup";



    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 8, 2006
    #2
    1. Advertising

  3. arnuld

    Kai-Uwe Bux Guest

    arnuld wrote:

    > 1) int i = 3;
    > 2.) int* pi;
    > 3.) int* pi2 = &i
    > 4.) char* pc;
    > 5.) char c;
    > 6) char c2 = 'a'
    > 7.) char* pc2 = &c2
    > 8.) char* ps = "Stroustroup";
    > 9.) extern double d;
    >
    > 2, 4 & 9 are the only declarations here. right ?


    2 and 4 look like definitions to me.

    > i know that 1, 3, 5, 6, 7, 8 are definitions but can we call them
    > declarations too?



    Yes, the standard defines [3.1/2]:

    A declaration is a definition unless it declares a function without
    specifying the function?s body (8.4), it contains the extern specifier
    (7.1.1) or a linkage-specification (7.5) and neither an initializer nor
    a function-body, it declares a static data member in a class declaration
    (9.4), it is a class name declaration (9.1), or it is a typedef
    declaration (7.1.3), a using-declaration (7.3.3), or a using-directive
    (7.3.4).

    As you can see, every definition is a declaration.


    > Is 2 a legal declaration?


    Looks legal to me. Did you run it by your compiler?


    > i dont understand the difference between 7 & 8.


    8 is a special case since the right hand side is really const. Usually, when
    you define a T*, you are allowed to modify the pointee; and any attempt to
    define a T* so that it points to a (an array of) T char should fail. In
    order to support C code, there are special provisions for char*.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 8, 2006
    #3
  4. Alf P. Steinbach wrote:
    > * arnuld:
    >> 1) int i = 3;
    >> 2.) int* pi;
    >> 3.) int* pi2 = &i
    >> 4.) char* pc;
    >> 5.) char c;
    >> 6) char c2 = 'a'
    >> 7.) char* pc2 = &c2
    >> 8.) char* ps = "Stroustroup";
    >> 9.) extern double d;
    >>
    >> 2, 4 & 9 are the only declarations here. right ?

    >
    > All are declarations, but only 9 is a pure declaration (not a
    > definition).


    It depends on the context. 2, 4, 5, can also be declarations if
    they happen to be inside a class definition.

    >[..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 8, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    568
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    405
    Chris Torek
    Aug 7, 2004
  3. Jon Slaughter
    Replies:
    4
    Views:
    433
    Jon Slaughter
    Oct 26, 2005
  4. Replies:
    11
    Views:
    4,685
    Juha Nieminen
    Jan 28, 2008
  5. Larry Woods

    Confused over definition of "root" of site

    Larry Woods, Jan 13, 2004, in forum: ASP General
    Replies:
    2
    Views:
    149
    Larry Woods
    Jan 13, 2004
Loading...

Share This Page