function return pointer of int?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Kanze

    #31
    Re: function return pointer of int?

    On Nov 6, 3:44 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
    James Kanze <james.ka...@gm ail.comwrites:
    On Nov 6, 7:39 am, Barry Schwarz <schwa...@dqel. comwrote:
    On Wed, 5 Nov 2008 22:30:03 -0800 (PST), baichuan0...@16 3.com wrote:
        [...]
    void *fun(int i)
    {
       void *p = &i;
       p += (1<<5) + 4;
       return p;
    }
    What do you think this accomplishes?
    Your first executable statement contains a constraint violation.
    I'm not sure what you mean by the "first executable
    statement". The definition of p (with its initialization)
    definitely generates executable code, and is executed.  And
    there's no problem with this statement; it is legal and well
    defined, and must work in any implementation.
    The cross-post is confusing matters (as always!).
    Especially as it concerns an area in which C and C++ try to be
    identical---all too often using different words to (hopefully)
    say the same thing.
    In C there is a clear distinction between declarations and
    statements, both in the formal syntax as well as in the less
    formal text of the standard, so "the first statement" refers
    unambiguously to the increment of p (and the extra
    "executable " is redundant).  In C++, as you know, there is a
    "declaratio n statement" so the distinction is lost.  Barry is
    presumably assuming the code is C.
    I'd missed that difference. Historically, of course, it made
    sense, since you couldn't mix declarations and (other)
    statements.

    My main point still holds, of course: no C or C++ compiler will
    accept pointer arithmetic on an incomplete type (and void is an
    incomplete type). The code simply won't compile.

    --
    James Kanze (GABI Software) email:james.kan ze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientier ter Datenverarbeitu ng
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

    Comment

    • jameskuyper

      #32
      Re: function return pointer of int?

      James Kanze wrote:
      ....
      My main point still holds, of course: no C or C++ compiler will
      accept pointer arithmetic on an incomplete type (and void is an
      incomplete type). The code simply won't compile.
      More accurately: no fully conforming compiler for either language will
      accept such code without first issuing a diagnostic. A number of
      popular compilers will indeed accept pointer arithmetic on void*,
      which is performed as if it were char*. As long as they also issue the
      mandatory diagnostic message, they can do so while remaining fully
      conforming.

      Comment

      • Keith Thompson

        #33
        Re: function return pointer of int?

        Michael <michael@michae ldadmum.no-ip.orgwrites:
        [...]
        change
        int * get_p_t(int t) {
        to
        int * get_p_t(int&t) {
        Not in C (note the cross-post).

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Keith Thompson

          #34
          Re: function return pointer of int?

          James Kanze <james.kanze@gm ail.comwrites:
          [...]
          My main point still holds, of course: no C or C++ compiler will
          accept pointer arithmetic on an incomplete type (and void is an
          incomplete type). The code simply won't compile.
          Any conforming C or C++ compiler must issue a diagnostic for any
          attempt to peform pointer arithmetic on a pointer to an incomplete
          type, since it's a constraint violation. At least in C, it's not
          actually required to reject the translation unit; it may legally
          compile it successfully, and the resulting program has behavior that
          is not defined by the standard (though of course it may be defined by
          the implementation) .

          The only case where a C translation unit *must* be rejected is when it
          contains a #error directive that survives preprocessing. I'm not sure
          what the corresponding rules are for C++.

          In particular, gcc (specifically the C compiler that's part of the gcc
          suite) allows arithmetic on void* by default. This is a permissible
          extension as long as it issues a diagnostic. g++ doesn't support this
          particular extension by default; I don't know whether it has an option
          to enable it.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • Michael

            #35
            Re: function return pointer of int?

            Davy wrote:
            Hi all,
            >
            I am writing a function, which return the pointer of the int. But it
            seems to be wrong. Any suggestion?
            >
            int * get_p_t(int t) {
            return &t;
            }
            >
            int main()
            {
            printf("v1\n");
            int t = 5;
            int * p_t[2];
            >
            p_t[0] = &t; // right
            p_t[1] = get_p_t(t); //wrong
            >
            return 0;
            }
            >
            Best regards,
            Davy
            change
            int * get_p_t(int t) {
            to
            int * get_p_t(int&t) {

            Comment

            Working...