954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array Question

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

dev.cplusplus
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

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[])

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

dev.cplusplus
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

solution:

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

	MyFunc(&array);
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

solution:

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

	MyFunc(&array);

Is it advisable using malloc in c++?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

> 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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You