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. Passing an array as argument to a function (2)

Passing an array as argument to a function (2)

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
14 Posts 5 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.
  • C Calin Negru

    Just to clarify over on pointer/array as argument One way is to pass the array as pointer

    int Function( int * arrayargument)
    {
    for(int i =0; i < 10; i++)
    {
    arrayargument[i] = 1;
    }

    }

    int AnArray[10];
    //case1
    Function(AnArray);
    //case2
    Function(&AnArray[0]);

    could same result be achieved by calling it case2 mode?

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

    Yes, in case 1 the compiler assumes the name of the array actually means a pointer to the first element. In case 2 you have specifically requested "pass the address of the first element of the array".

    C 1 Reply Last reply
    0
    • C Calin Negru

      Just to clarify over on pointer/array as argument One way is to pass the array as pointer

      int Function( int * arrayargument)
      {
      for(int i =0; i < 10; i++)
      {
      arrayargument[i] = 1;
      }

      }

      int AnArray[10];
      //case1
      Function(AnArray);
      //case2
      Function(&AnArray[0]);

      could same result be achieved by calling it case2 mode?

      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #3

      I would also pass the size of the array to the function instead of hard-coding it at 10:

      int Function(int* arrayargument, size_t size)

      Robust Services Core | Software Techniques for Lemmings | Articles

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      C 1 Reply Last reply
      0
      • L Lost User

        Yes, in case 1 the compiler assumes the name of the array actually means a pointer to the first element. In case 2 you have specifically requested "pass the address of the first element of the array".

        C Offline
        C Offline
        Calin Negru
        wrote on last edited by
        #4

        Thanks When you can do the same thing in several ways something is not right :)

        K 1 Reply Last reply
        0
        • C Calin Negru

          Thanks When you can do the same thing in several ways something is not right :)

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #5

          fearless_ wrote:

          When you can do the same thing in several ways something is not right

          I can only imagine the following program will blow your mind:

          #include int main()
          {
          int a[] = { 0, 1, 2, 3, 4, 5, 6, 7};
          int i1 = a[2];
          int i2 = 2[a];

          printf("i1 = %d  i2 = %d\\n", i1, i2);
          
          return 0;
          

          }

          Keep Calm and Carry On

          1 Reply Last reply
          0
          • Greg UtasG Greg Utas

            I would also pass the size of the array to the function instead of hard-coding it at 10:

            int Function(int* arrayargument, size_t size)

            Robust Services Core | Software Techniques for Lemmings | Articles

            C Offline
            C Offline
            Calin Negru
            wrote on last edited by
            #6

            I was trying to keep the example as simple as possible. But since we`re here, what`s size_t? It`s the second time I see someone using it, is it an actual variable or just a custom?

            V 1 Reply Last reply
            0
            • C Calin Negru

              I was trying to keep the example as simple as possible. But since we`re here, what`s size_t? It`s the second time I see someone using it, is it an actual variable or just a custom?

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #7

              size_t is just a type. Like int, long, and so on

              C 1 Reply Last reply
              0
              • V Victor Nijegorodov

                size_t is just a type. Like int, long, and so on

                C Offline
                C Offline
                Calin Negru
                wrote on last edited by
                #8

                Thanks Victor you mean a built-in type in a c++ compiler? what makes it particular, with float the compiler will let you use the comma, char is a like a tiny int, what makes size_t special?

                V 1 Reply Last reply
                0
                • C Calin Negru

                  Thanks Victor you mean a built-in type in a c++ compiler? what makes it particular, with float the compiler will let you use the comma, char is a like a tiny int, what makes size_t special?

                  V Offline
                  V Offline
                  Victor Nijegorodov
                  wrote on last edited by
                  #9

                  [std::size_t - cppreference.com](https://en.cppreference.com/w/cpp/types/size\_t) [size_t - cppreference.com](https://en.cppreference.com/w/c/types/size\_t) [size_t c - Google Search](https://www.google.com/search?q=size\_t+c%2B%2B&oq=size\_t&aqs=chrome.1.69i57j0l7.14987j0j7&sourceid=chrome&ie=UTF-8)

                  C 1 Reply Last reply
                  0
                  • V Victor Nijegorodov

                    [std::size_t - cppreference.com](https://en.cppreference.com/w/cpp/types/size\_t) [size_t - cppreference.com](https://en.cppreference.com/w/c/types/size\_t) [size_t c - Google Search](https://www.google.com/search?q=size\_t+c%2B%2B&oq=size\_t&aqs=chrome.1.69i57j0l7.14987j0j7&sourceid=chrome&ie=UTF-8)

                    C Offline
                    C Offline
                    Calin Negru
                    wrote on last edited by
                    #10

                    oh so it`s a class object from a library not a built in type.

                    V 1 Reply Last reply
                    0
                    • C Calin Negru

                      oh so it`s a class object from a library not a built in type.

                      V Offline
                      V Offline
                      Victor Nijegorodov
                      wrote on last edited by
                      #11

                      fearless_ wrote:

                      oh so it`s a class object from a library not a built in type.

                      :zzz: :confused: :zzz: One more link: [size_t](https://www.viva64.com/en/t/0044/)

                      C 1 Reply Last reply
                      0
                      • V Victor Nijegorodov

                        fearless_ wrote:

                        oh so it`s a class object from a library not a built in type.

                        :zzz: :confused: :zzz: One more link: [size_t](https://www.viva64.com/en/t/0044/)

                        C Offline
                        C Offline
                        Calin Negru
                        wrote on last edited by
                        #12

                        Are you bored? don`t be, I`m learning

                        V 1 Reply Last reply
                        0
                        • C Calin Negru

                          Are you bored? don`t be, I`m learning

                          V Offline
                          V Offline
                          Victor Nijegorodov
                          wrote on last edited by
                          #13

                          Don't worry! Just learn! :) And sorry if I wrote something wrong here!

                          C 1 Reply Last reply
                          0
                          • V Victor Nijegorodov

                            Don't worry! Just learn! :) And sorry if I wrote something wrong here!

                            C Offline
                            C Offline
                            Calin Negru
                            wrote on last edited by
                            #14

                            ok

                            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