Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Pointer to a function parameter

Pointer to a function parameter

Scheduled Pinned Locked Moved ATL / WTL / STL
help
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    econy
    wrote on last edited by
    #1

    I write a function like:

    void func1(char chx[], int y)
    {
    int wsn = 0;

     wsn = \*(int \*) (&chx);
     if (wsn == 0) {
        ...
     }
    

    }

    Compiler works well, no warning, no error. But when the code is running, seems it get a wild pointer. the code crashed.

    L 1 Reply Last reply
    0
    • E econy

      I write a function like:

      void func1(char chx[], int y)
      {
      int wsn = 0;

       wsn = \*(int \*) (&chx);
       if (wsn == 0) {
          ...
       }
      

      }

      Compiler works well, no warning, no error. But when the code is running, seems it get a wild pointer. the code crashed.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Should be:

      void func1(char chx[], int y)
      {
      int wsn = 0;

      wsn = \*(int \*)chx;
      if (wsn == 0) {
      ...
      }
      

      }

      The variable name chx is already a pointer to the array, adding the addressof operator creates a pointer to that pointer.

      E 1 Reply Last reply
      0
      • L Lost User

        Should be:

        void func1(char chx[], int y)
        {
        int wsn = 0;

        wsn = \*(int \*)chx;
        if (wsn == 0) {
        ...
        }
        

        }

        The variable name chx is already a pointer to the array, adding the addressof operator creates a pointer to that pointer.

        E Offline
        E Offline
        econy
        wrote on last edited by
        #3

        Thanks, but I tested, &chx = chx.

        L 1 Reply Last reply
        0
        • E econy

          Thanks, but I tested, &chx = chx.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          No it doesn't. I have no idea how you tested this but it is not correct. Build the following, step through it with your debugger and you will see that they are different.

           int\* pAddressOf = (int \*) (&chx);
           int\* pNormal = (int \*)(chx);
          
          E 2 Replies Last reply
          0
          • L Lost User

            No it doesn't. I have no idea how you tested this but it is not correct. Build the following, step through it with your debugger and you will see that they are different.

             int\* pAddressOf = (int \*) (&chx);
             int\* pNormal = (int \*)(chx);
            
            E Offline
            E Offline
            econy
            wrote on last edited by
            #5

            I tested in Visual studio IDE.

            char s1[] = {'H', 'e', 'l', 'l', 'o'};

            printf("%lX,%lX",s1,&s1);

            And I get same value for s1 and &s1.

            1 Reply Last reply
            0
            • L Lost User

              No it doesn't. I have no idea how you tested this but it is not correct. Build the following, step through it with your debugger and you will see that they are different.

               int\* pAddressOf = (int \*) (&chx);
               int\* pNormal = (int \*)(chx);
              
              E Offline
              E Offline
              econy
              wrote on last edited by
              #6

              And I tested your code in Visual studio, use Printf("%lX"), It shows same value too

              L 2 Replies Last reply
              0
              • E econy

                And I tested your code in Visual studio, use Printf("%lX"), It shows same value too

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                That may be true, but sending those values to printf is not the same as casting to pointers. You can confirm it by printing out the values of the two pointers created in my two lines of code.

                1 Reply Last reply
                0
                • E econy

                  And I tested your code in Visual studio, use Printf("%lX"), It shows same value too

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  See http://publications.gbdirect.co.uk/c_book/chapter5/arrays_and_address_of.html[^] for a good explanation of this issue.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups