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. C / C++ / MFC
  4. array -- foo, &foo and &foo[0]

array -- foo, &foo and &foo[0]

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
9 Posts 3 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.
  • G Offline
    G Offline
    George_George
    wrote on last edited by
    #1

    Hello everyone, I think if we define foo as char array, for example, char foo [32]; then foo, &foo and &foo[0] should be the same, right? For example, the following 3 statements are the same, strcpy (foo, goo); strcpy (&foo, goo); strcpy (&foo[0], goo); Any comments? I am very interested in how C treats foo and &foo and make them the same? thanks in advance, George

    J I 2 Replies Last reply
    0
    • G George_George

      Hello everyone, I think if we define foo as char array, for example, char foo [32]; then foo, &foo and &foo[0] should be the same, right? For example, the following 3 statements are the same, strcpy (foo, goo); strcpy (&foo, goo); strcpy (&foo[0], goo); Any comments? I am very interested in how C treats foo and &foo and make them the same? thanks in advance, George

      J Offline
      J Offline
      jimfisher
      wrote on last edited by
      #2

      I think that you will find this to be compiler dependent. foo is the array, so C/C++ treats the array name as a pointer to the array and thus it is a pointer of the same value as &foo[0]. With some compilers however, I have seen where the compiler stores a pointer to the array (like &foo[0] and then &foo is a pointer to that pointer... so I would stay away from this syntax. Jim

      G 1 Reply Last reply
      0
      • J jimfisher

        I think that you will find this to be compiler dependent. foo is the array, so C/C++ treats the array name as a pointer to the array and thus it is a pointer of the same value as &foo[0]. With some compilers however, I have seen where the compiler stores a pointer to the array (like &foo[0] and then &foo is a pointer to that pointer... so I would stay away from this syntax. Jim

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #3

        Thanks Jim, I have tested that on Visual Studio, foo and &foo are the same. I think you mean on some other compiler, they may be different. Here is my test program in Visual Studio. int main (int argc, char** argv) { char foo [1024]; unsigned int p; unsigned int q; p = foo; q = &foo; return 0; } I am interested in the cases when "&foo is a pointer to that pointer". I am wondering what is wrong if I still treat foo and &foo the same in this situation? Could you provide more information and analysis please? regards, George

        J 1 Reply Last reply
        0
        • G George_George

          Hello everyone, I think if we define foo as char array, for example, char foo [32]; then foo, &foo and &foo[0] should be the same, right? For example, the following 3 statements are the same, strcpy (foo, goo); strcpy (&foo, goo); strcpy (&foo[0], goo); Any comments? I am very interested in how C treats foo and &foo and make them the same? thanks in advance, George

          I Offline
          I Offline
          Iain Clarke Warrior Programmer
          wrote on last edited by
          #4

          I was a little intrigued by your question, and made up a little test...

          UINT p, q;
          
          char foo \[100\];
          
          p = (UINT)foo;
          q = (UINT) (&foo);
          
          ASSERT(p == q);
          
          char \*bar = foo;
          
          p = (UINT) bar;
          q = (UINT) (&bar);
          
          ASSERT(p == q);
          

          And you were indeed right. foo and &foo were the same value, but bar and &bar were not. Part of the problem is that we're both cheating and looking at the variables as raw numbers (which I find useful to do conceptually), but the compiler has the advantage of looking at them knowing there type. So foo is a number, but is known to be an array, while &foo happens through an implementation detail you can't rely on to be true on another day of the week to be the same number, but is treated differently as it has a different type (pointer to an array). Hard casting like you did can be useful, but it can also be dangerous. Short answer: compiler dependent. If you rely on it, prepare to be shot in the foot. Iain.

          G 1 Reply Last reply
          0
          • I Iain Clarke Warrior Programmer

            I was a little intrigued by your question, and made up a little test...

            UINT p, q;
            
            char foo \[100\];
            
            p = (UINT)foo;
            q = (UINT) (&foo);
            
            ASSERT(p == q);
            
            char \*bar = foo;
            
            p = (UINT) bar;
            q = (UINT) (&bar);
            
            ASSERT(p == q);
            

            And you were indeed right. foo and &foo were the same value, but bar and &bar were not. Part of the problem is that we're both cheating and looking at the variables as raw numbers (which I find useful to do conceptually), but the compiler has the advantage of looking at them knowing there type. So foo is a number, but is known to be an array, while &foo happens through an implementation detail you can't rely on to be true on another day of the week to be the same number, but is treated differently as it has a different type (pointer to an array). Hard casting like you did can be useful, but it can also be dangerous. Short answer: compiler dependent. If you rely on it, prepare to be shot in the foot. Iain.

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #5

            Thanks Iain, So, the best practice is not to use &foo? :-) regards, George

            I 1 Reply Last reply
            0
            • G George_George

              Thanks Iain, So, the best practice is not to use &foo? :-) regards, George

              I Offline
              I Offline
              Iain Clarke Warrior Programmer
              wrote on last edited by
              #6

              There's nothing wrong with using &foo - but not as a number. Use it as a "pointer to an array" and let the compiler worry about location details. "Is it wrong to do..." is not something I can possibly answer in the general case. But taking your strcpy example...

              char foo [100];

              strcpy (foo, "iain"); is fine
              strcpy (&(foo[0]), "iain"); is fine, but ugly
              strcpy (&foo, "iain"); is syntactically wrong, and I'm surprised you don't get a warning at least. It just happens to work on this compiler.

              char *bar = new char [100];

              strcpy (bar, "iain"); is fine
              strcpy (&(bar[0]), "iain"); is fine, but ugly
              strcpy (&bar, "iain"); will fail - and mostlikely crash stuff.

              As you see, the only difference between "getting away with it", and "failing" is what's written a few lines up, relying on &array == array is a really bad idea, even if you will always get away with it. Imagine you come back in 3 years, your boss says "this needs to be longer - make it dynamically sized", and your code will crash with you being puzzled. Iain.

              G 1 Reply Last reply
              0
              • I Iain Clarke Warrior Programmer

                There's nothing wrong with using &foo - but not as a number. Use it as a "pointer to an array" and let the compiler worry about location details. "Is it wrong to do..." is not something I can possibly answer in the general case. But taking your strcpy example...

                char foo [100];

                strcpy (foo, "iain"); is fine
                strcpy (&(foo[0]), "iain"); is fine, but ugly
                strcpy (&foo, "iain"); is syntactically wrong, and I'm surprised you don't get a warning at least. It just happens to work on this compiler.

                char *bar = new char [100];

                strcpy (bar, "iain"); is fine
                strcpy (&(bar[0]), "iain"); is fine, but ugly
                strcpy (&bar, "iain"); will fail - and mostlikely crash stuff.

                As you see, the only difference between "getting away with it", and "failing" is what's written a few lines up, relying on &array == array is a really bad idea, even if you will always get away with it. Imagine you come back in 3 years, your boss says "this needs to be longer - make it dynamically sized", and your code will crash with you being puzzled. Iain.

                G Offline
                G Offline
                George_George
                wrote on last edited by
                #7

                Great comments, thanks Iain! regards, George

                1 Reply Last reply
                0
                • G George_George

                  Thanks Jim, I have tested that on Visual Studio, foo and &foo are the same. I think you mean on some other compiler, they may be different. Here is my test program in Visual Studio. int main (int argc, char** argv) { char foo [1024]; unsigned int p; unsigned int q; p = foo; q = &foo; return 0; } I am interested in the cases when "&foo is a pointer to that pointer". I am wondering what is wrong if I still treat foo and &foo the same in this situation? Could you provide more information and analysis please? regards, George

                  J Offline
                  J Offline
                  jimfisher
                  wrote on last edited by
                  #8

                  I do not recall where I have seen it. It was several years ago on an embedded compiler... seems like green hills or perhaps wind river. If you are using microsoft, I don't think you will have a problem treating foo and &foo as equals. Jim Fisher

                  G 1 Reply Last reply
                  0
                  • J jimfisher

                    I do not recall where I have seen it. It was several years ago on an embedded compiler... seems like green hills or perhaps wind river. If you are using microsoft, I don't think you will have a problem treating foo and &foo as equals. Jim Fisher

                    G Offline
                    G Offline
                    George_George
                    wrote on last edited by
                    #9

                    Thanks all the same, Jim. regards, George

                    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