Go Down

Topic: Use 2D array as a parameter and return of function (Read 3363 times) previous topic - next topic

Nandika

Oct 27, 2015, 08:33 am Last Edit: Oct 27, 2015, 08:59 am by Nandika
I am new to C++.I have only a little few knowladge about C++ pointers.
According to C++ rules,If we pass an array to function,we need to use pointers.
Also,If we return an array we need to use pointers.
I haven't more clear idea about pointer passing to function and returning from function.
Please give me ideas to understand this problem.

I make this code with 2D array for process in Get10 function.This is working properly.
But I haven't trust about accuracy of this way like pointer system. :(
As I think pointer system is very accurate system for deal with arrays.
If someone can modify this using pointers,I will appreciate.

Code: [Select]


byte Marks[13][4];


void setup() {
  Serial.begin(9600);
  Get10(); //How to pass array argument???
  for(int i=0;i<13;i++){
    for(int j=0;j<4;j++){
      Serial.print(Marks[i][j]);
      Serial.print(" ");   
    }
    Serial.println();
  }
}

void loop() {

}

void Get10(){ //How to get argument and return it after processing???
  for(int i=0;i<13;i++){
    for(int j=0;j<4;j++){
      Marks[i][j]=10;
     
    }
  }
 
}


PaulS

Code: [Select]
  Get10(); //How to pass array argument???
Since the array you seem to want to pass is global, there is no need to pass is.

If you wanted to pass a local array,
Code: [Select]
   Get10(Marks);

Code: [Select]
void Get10(){ //How to get argument and return it after processing???
Since the array you seem to want to pass is global, there is no need to pass is.

If you wanted to pass a local array,
Code: [Select]
void Get10(int array[13][4])
{ // Down here where it belongs
The art of getting good answers lies in asking good questions.

johnwasser

I can understand why you might want to pass an array (by reference) to the function so you can fill multiple arrays with 10's but I don't understand what you want to return from the function.  The only logical return value I can see is the argument but since the caller provided the argument it probably doesn't need that value.

Array (and struct, and object) parameters are, by default, passed by reference:
Code: [Select]
byte Marks[13][4];

void setup() {
  Serial.begin(9600);

  Get10(Marks);
  for (int i = 0; i < 13; i++) {
    for (int j = 0; j < 4; j++) {
      Serial.print(Marks[i][j]);
      Serial.print(" ");
    }
    Serial.println();
  }
}

void loop() {
}

void Get10(byte parameter[13][4]) {
  for (int i = 0; i < 13; i++) {
    for (int j = 0; j < 4; j++) {
      parameter[i][j] = 10;
    }
  }
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Nandika

Thank both of you for reply.

Ok.If I make an array in function inside,How do I return it?

PaulS

Quote
If I make an array in function inside,How do I return it?
You can't. Functions can not return arrays. They can return one value. That value COULD be a pointer, but NOT a pointer to a local array.

If you want a function to operate on/populate an array, pass the array to the function.
The art of getting good answers lies in asking good questions.

Delta_G

You can't return a pointer to a local array because the local array in the function disappears as soon as the function returns and now the pointer you got back points to something that doesn't exist anymore.

You can return a pointer to a static local array, but be really careful because next time you go to that function and change that array everyone who has a pointer to it will see the changes and not the stuff they had before. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

lloyddean

#6
Nov 03, 2015, 11:59 pm Last Edit: Nov 04, 2015, 02:25 am by lloyddean
Code: [Select]

#define ARRAY_ENTRY_COUNT(ARRAY)    (sizeof(ARRAY) / sizeof(ARRAY[0]))
#define ARRAY_2D_ROWS(ARRAY)        ARRAY_ENTRY_COUNT(ARRAY)
#define ARRAY_2D_COLUMNS(ARRAY)     ARRAY_ENTRY_COUNT(ARRAY[0])

uint8_t marks[13][4];


uint8_t* get10(uint8_t* const address, size_t const ROWS, size_t const COLUMNS)
{
    // here address is the memory address of an unsigned 8 bit memory location
    for ( int i_row = 0; i_row < ROWS; i_row++ )
    {
        for ( int i_column = 0; i_column < COLUMNS; i_column++ )
        {
            *(address + ((i_row * COLUMNS) + i_column)) = 10;
        }
    }

    return address;
}

void loop()
{   }

void setup()
{
    Serial.begin(9600);

    size_t  ROWS    = ARRAY_2D_ROWS(marks);
    size_t  COLUMNS = ARRAY_2D_COLUMNS(marks);

    // arrays are passed by address (or location in memory)
    //
    // here we cast and pass the address of 'marks' as a memory address of an
    // unsigned 8 bit entity as the array 'marks[13][4]' is declared as an array
    // of uint8_t chunk of memory
    //
    // I prefer uint8_t to 'byte' as 'byte' is a platform dependent type and is
    // often unsigned vs signed from one platform to another!
    //
    // here I believe it is unsigned so I use 'uint8_t'
    //
    // we must pass the number of ROWS and COLUMNS as the [R][C] information
    // is otherwise lost as arrays are simply passed by addrees

    get10((uint8_t*)marks, ROWS, COLUMNS);
   
    for ( int i_row = 0; i_row < ROWS; i_row++ )
    {
        for ( int i_column = 0; i_column < COLUMNS; i_column++ )
        {
            Serial.print(marks[i_row][i_column]);
            Serial.print(" ");   
        }

        Serial.println();
    }
}

Nandika

Thanks all of you.I got further clear idea about pointers.
@lloyddean,Thanks for your code.I got several important point from It.

This is my problem regarding with array.
https://forum.arduino.cc/index.php?topic=357340.0

Go Up