kylejw
kylejw

Reputation: 29

Pass back array as a parameter

So I need to be able to pass back this struct array I created inside the function as a parameter, not the return value and I am at a loss. Can someone show me the optimal way of doing this? Thank you!

int calcHistogram(char** arr, Histogram* h,  int count) {

  Histogram* hist = (Histogram*)malloc(count*sizeof(Histogram));
  int hCount = 0;

  // Code that works
  
  h = hist;
  return hCount;
}

Upvotes: 0

Views: 87

Answers (1)

fanfly
fanfly

Reputation: 493

Maybe you need a function like this:

int createHistogram(char **arr, Histogram **h, int count) {
    Histogram *hist = (Histogram *)malloc(sizeof(Histogram) * count);
    int hCount = 0;

    // Code that works

    *h = hist;
    return hCount;
}

Then you can declare a pointer

Histogram *h;

and then call

createHistogram(arr, &h, count);

to store the address of the array of histograms into h.

Upvotes: 4

Related Questions