Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

C++ Function call help


ghostraider100's Avatar
Member
0 0

How to call a function which consist of an array? For example if i write a function like

markupint matrix(int a[3][3])

how can i call this function in my main() program? I use Turbo c++ ver 5.0, it pops out with an error

cannot convert (int , ) to (*)

please post ur solutions.


ghost's Avatar
0 0

I'm going to take a guess here and say that you tried calling the function in a manner such as this: markupmatrix(arr[3][3]); rather than this: markupmatrix(arr);


ghostraider100's Avatar
Member
0 0

Some other suggestion plz. I'm not satisfied with this reply…


ghost's Avatar
0 0

ghostraider100 wrote: Some other suggestion plz. I'm not satisfied with this reply… Huh, what? Oh shite, I'm sorry, I did not realise that I was addressing his majesty, the grand emperor of the golden city himself. Let me change my previous reply to a more appropriate one. What I meant to say was naturally:

I'm going to take a guess here and say that you tried calling the function in a manner such as this: markupmatrix(arr[3][3]); rather than going and fucking yourself in your own grave. I sincerely hope that this little oversight on my part can be forgiven.

With deepest respect, your loyal subject, COM.


GTADarkDude's Avatar
Member
0 0

We're not here to satisfy you, you're here to get our help, remember that.

Especially when Google could have answered your question in a few seconds: http://cplusplus.com/doc/tutorial/arrays/

Anyhow:

#include <iostream>

const int SIZE = 3;

int matrix(int arr[][SIZE])
{
    //do something
    return 0; //return something
}

int main()
{
    int arr[SIZE][SIZE] = {{1,2,3},{4,5,6},{7,8,9}};
    cout << "And the result is... " << matrix(arr) << "!\n";
    return 0;
}

korg's Avatar
Admin from hell
0 0

ghostraider100 wrote: Some other suggestion plz. I'm not satisfied with this reply…

Stopped reading just about there.


j4m32's Avatar
Member
0 0

Since you didn't really give us much to work with, we have to infer how you are using your function.

Sometimes it's better to provide a more informative snippet with the function definition.

  1. Learn the difference between passing by reference and passing by value and you might that the answer given here is exactly the same as what COM said.

  2. It's clear you have no idea about addressing, it is pretty key in low level programming as well as C/C++.

You can use as COM said, just the variable name since C/C++ will automatically take the reference:

matrix(arr);

it knows that it's expecting a pointer from the function definition (according to your error).

OR you can explicitly tell to pass the reference to the very first element in the array:

matrix(&arr[0][0]);

Both "notations" are EXACTLY the same in terms of their addresses, since the address of "arr" is exactly the same as the first element/item in the array: arr[0][0]

Hope this clears things up, next time don't just dismiss what someone has said.

Jim,


ghostraider100's Avatar
Member
0 0

I'm extremely sorry which i wrote here esp. com


Arabian's Avatar
Member
0 0

ghostraider100 wrote: Some other suggestion plz. I'm not satisfied with this reply…

Gold. Pure, comedic gold.