Jump to content

How to pass and return arrays in c?

Redturtle098
Go to solution Solved by colonel_mortis,

You can't pass arrays by value in C, but you can pass a pointer to the array.

void sort(int* arr, int len) {
  // you can read and update arr[0]..arr[len-1] in here.
  // Simple bubblesort implementation:
  for (int end = len; end > 1; end--) {
    for (int i = 1; i < end; i++) {
      if (arr[i-1] > arr[i]) {
        int tmp = arr[i];
        arr[i] = arr[i-1];
        arr[i-1] = tmp;
      }
    }
  }
}

int main() {
  int nums[5] = {5, 8, 1, 3, 4};
  sort(nums, 5);
}

You can play with this code here:

(sorry, embedding repl.it is currently an admin-only feature, but I'm working on it).

Hey,

 

Does anyone know how to pass and return functions in c/c++?

I'm trying to create a function that alters/sorts the data in an array.

 

In pseudocode, it'd be something like this:

altering_function (array)

    #add number to array

    #sort array

    return(array)


main_function()

    array = {1,2,3,4}

    array = altering_function(array)

 

Any help on understanding this would be awesome!! ?
Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

33 minutes ago, Redturtle098 said:

(Hey,

 

Does anyone know how to pass and return functions in c/c++?

I'm trying to create a function that alters/sorts the data in an array.

 

In pseudocode, it'd be something like this:


altering_function (array)

    #add number to array

    #sort array

    return(array)


main_function()

    array = {1,2,3,4}

    array = altering_function(array)

 

Any help on understanding this would be awesome!! ?
Thanks!

In C, you could just pass a pointer to the start of the array + a number representing the size of the array.

In C++ you can store the data in a std::array and pass a reference to that array.

Adding to the array won’t be possible since it is fixed size (although you could add a dummy value when creating the array).

In C++ you can also use a std::vector instead, which does support growing (and shrinking) dynamically.

 

I don’t know about C, but sorting in C++ is easy:

std::sort(std::begin(vector), std::end(vector));

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to comment
Share on other sites

Link to post
Share on other sites

You can't pass arrays by value in C, but you can pass a pointer to the array.

void sort(int* arr, int len) {
  // you can read and update arr[0]..arr[len-1] in here.
  // Simple bubblesort implementation:
  for (int end = len; end > 1; end--) {
    for (int i = 1; i < end; i++) {
      if (arr[i-1] > arr[i]) {
        int tmp = arr[i];
        arr[i] = arr[i-1];
        arr[i-1] = tmp;
      }
    }
  }
}

int main() {
  int nums[5] = {5, 8, 1, 3, 4};
  sort(nums, 5);
}

You can play with this code here:

(sorry, embedding repl.it is currently an admin-only feature, but I'm working on it).

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

29 minutes ago, mathijs727 said:

In C, you could just pass a pointer to the start of the array + a number representing the size of the array.

In C++ you can store the data in a std::array and pass a reference to that array.

Adding to the array won’t be possible since it is fixed size (although you could add a dummy value when creating the array).

In C++ you can also use a std::vector instead, which does support growing (and shrinking) dynamically.

 

I don’t know about C, but sorting in C++ is easy:

std::sort(std::begin(vector), std::end(vector));

Thanks for the help! I think that I definitely might add a dummy value into the array in the future haha ?

Link to comment
Share on other sites

Link to post
Share on other sites

28 minutes ago, colonel_mortis said:

You can't pass arrays by value in C, but you can pass a pointer to the array.


void sort(int* arr, int len) {
  // you can read and update arr[0]..arr[len-1] in here.
  // Simple bubblesort implementation:
  for (int end = len; end > 1; end--) {
    for (int i = 1; i < end; i++) {
      if (arr[i-1] > arr[i]) {
        int tmp = arr[i];
        arr[i] = arr[i-1];
        arr[i-1] = tmp;
      }
    }
  }
}

int main() {
  int nums[5] = {5, 8, 1, 3, 4};
  sort(nums, 5);
}

You can play with this code here:

(sorry, embedding repl.it is currently an admin-only feature, but I'm working on it).

 

Thank you so much for your help! It really helps to have something interactive too. And that's okay, it worked fine haha. Thanks again!! ?

 

Link to comment
Share on other sites

Link to post
Share on other sites

Here is how a C++ implementation would look like:

#include <algorithm>
#include <vector>
#include <iostream>

void alteringFunction(std::vector<int>& v) {
    // Add number (42) to the array
    v.push_back(42);

    // Sort array
    std::sort(std::begin(v), std::end(v));

    // No need to return since we modified the vector in-place
}

int main()
{
    std::vector<int> data { 5, 8, 1, 3, 4 };
    alteringFunction(data);

    // Print output
    for (int v : data)
        std::cout << v << std::endl;
}

Note that C++ comes with a vector container that can dynamically grow (or shrink) unlike C.

It also ships with a set of useful algorithms including sort, which is much faster than bubble sort for larger arrays (required to have O(N log N) average case time complexity).

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, colonel_mortis said:

(sorry, embedding repl.it is currently an admin-only feature, but I'm working on it).

?

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×