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. How can I pass an array as a function parameter?

How can I pass an array as a function parameter?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
5 Posts 4 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 Offline
    C Offline
    Cristoff
    wrote on last edited by
    #1

    here is what I want to do: I want to swap the some elements of an array so I want to make a function like that: int arr[] // this array is initialized ... swap( int a[]) { //swap parameters } swap(arr); // use the function printarr(arr); // print the resault The question is: how should I pass the array to the function properly, so that it changes the values of arr outside of its body too?

    T W C C 4 Replies Last reply
    0
    • C Cristoff

      here is what I want to do: I want to swap the some elements of an array so I want to make a function like that: int arr[] // this array is initialized ... swap( int a[]) { //swap parameters } swap(arr); // use the function printarr(arr); // print the resault The question is: how should I pass the array to the function properly, so that it changes the values of arr outside of its body too?

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      void swap (int* array) { /*...*/ }

      1 Reply Last reply
      0
      • C Cristoff

        here is what I want to do: I want to swap the some elements of an array so I want to make a function like that: int arr[] // this array is initialized ... swap( int a[]) { //swap parameters } swap(arr); // use the function printarr(arr); // print the resault The question is: how should I pass the array to the function properly, so that it changes the values of arr outside of its body too?

        W Offline
        W Offline
        Waldermort
        wrote on last edited by
        #3

        Pass the array as a pointer.

        void SwapArrayVals(int iVals[],int size){
        // your code here
        }

        and to call the function

        int iData[2] = {11,22};
        SwapArrayVals(iData,2);

        or

        int iData[2] = {11,22};
        SwapArrayVals(&iData[0],2);

        Also, it's not always necassary to pass the array size, but it is good practice and avoids writing outside your array.

        1 Reply Last reply
        0
        • C Cristoff

          here is what I want to do: I want to swap the some elements of an array so I want to make a function like that: int arr[] // this array is initialized ... swap( int a[]) { //swap parameters } swap(arr); // use the function printarr(arr); // print the resault The question is: how should I pass the array to the function properly, so that it changes the values of arr outside of its body too?

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Also, it could be interesting to have a look at the containers (particularly the vector) of the standard template library (STL). Basically, it will help you to maintain arrays that can be resized easily, ...

          1 Reply Last reply
          0
          • C Cristoff

            here is what I want to do: I want to swap the some elements of an array so I want to make a function like that: int arr[] // this array is initialized ... swap( int a[]) { //swap parameters } swap(arr); // use the function printarr(arr); // print the resault The question is: how should I pass the array to the function properly, so that it changes the values of arr outside of its body too?

            C Offline
            C Offline
            Cristoff
            wrote on last edited by
            #5

            It's now working!

            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