Hi again, I have the following question,
I have the following problem:
I have an array and a function that receives the array, the problem is that the function is declared like this

void MyFunc(int **my_array)
{
for ( i=0; i<10; i++)
        (*my_array)[i] = 0;
}

And when I use the function I receive error:

int int_array[10];
MyFunc(int_array);

I must implement the function with "**" and I must use an array like int int_array[10];, it is possible to send the array to the function?
Any Ideas?
Thanks

Member Avatar for iamthwee

Hi again, I have the following question,
I have the following problem:
I have an array and a function that receives the array, the problem is that the function is declared like this

void MyFunc(int **my_array)
{
for ( i=0; i<10; i++)
        (*my_array)[i] = 0;
}

And when I use the function I receive error:

int int_array[10];
MyFunc(int_array);

I must implement the function with "**" and I must use an array like int int_array[10];, it is possible to send the array to the function?
Any Ideas?
Thanks

Can't you just pass the array like this:

pass(array)

void pass(int array[])

I must implement the function with "**" and I must use an array like int int_array[10];, it is possible to send the array to the function?
Any Ideas?
Thanks

Not possible. MyFun() expects a two dimensional array of integers, not a one dimensional like you are attempting to pass. Not only that, but MyFunc will crash big-time because you are not passing the max number of elements in each dimension of the array. When the array has fewer than 10 rows MyFunc() will be scribbling all over memory.

Sort of possible, but not recommended

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

void MyFunc(int **my_array)
{
    for ( int i=0; i<10; i++)
        (*my_array)[i] = i;
}

int main()
{
    int int_array[10];
    int *temp = int_array;
    MyFunc( &temp );    // was &int_array
    cout << int_array[0] << " " << int_array[5] << endl;
    return EXIT_SUCCESS;
}

The assignment to temp "washes away" the array size which would be part of the type if you naively did &int_array.

You don't want to lose type information if you can possibly avoid it.

I saw an example in the web with the following example:

int *int_array = new int [10];
MyFunc(&int_array);

The problem is that I can't use

int *int_array = new int [10];

So I'll try something else
Thanks

solution:

int* array = malloc(10 * sizeof(int));

	MyFunc(&array);
Member Avatar for iamthwee

solution:

int* array = malloc(10 * sizeof(int));

	MyFunc(&array);

Is it advisable using malloc in c++?

> The problem is that I can't use
We're not here to play 20 questions to figure out all the "rules" for your assignment. If you keep throwing answers back at us, we'll just get annoyed / bored with you.

> Is it advisable using malloc in c++?
No - malloc doesn't call constructors.

commented: Well said Mr.Salem -- ~s.o.s~ +7

what makes you think this is a c++ program? I don't see where the OP mentioned it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.