Dheeraj Sudan Meenu Hinduja How to pass arrays by reference?

Hi,

I’m Dheeraj Sudan from the UK. I’m a software developer and co-run a business with my wife, Meenu Hinduja. Can anyone suggest How to pass arrays by reference?

Thanks, Regards

Dheeraj Sudan Meenu Hinduja

I moved your topic to an appropriate forum category @dheerajsudan.

In the future, when creating a topic please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

Hi Dherraj and Meenu,

It is nice to know so many details about you, but it's not really necessary to provide such details in order to get answers to questions like this. Most people are more circumspect about freely giving away so much detail, especially on a public forum, because of the potential for identity theft, for example.

It's your question related to Arduino? Or is it a more general C/C++ related question?

1 Like

just pass it as a pointer

output

disp:
1
339
88

code


int array [] = {1, 339, 88 };
const int Narray = sizeof(array)/sizeof(int);

void disp (
    int *p,
    int  N )
{
    Serial.println ("disp:");
    for (int n = 0; n < N; n++)
        Serial.println (*p++);
}

void setup ()
{
    Serial.begin (9600);
    disp (array, Narray);
}

void loop ()
{
}
1 Like

A rather unusual question for a software developer. I assume you mean by C++ Reference:

template<typename T, size_t N>
void passByArrayReference(T (&myArray)[N]) {
  Serial.print("ARRAY Length:");
  Serial.println(N);
  for (auto x : myArray) {
    Serial.print(x);
    Serial.print("  ");
  }
  Serial.println();
}

void setup() {
  Serial.begin(115200);
  delay(1000);

  int array_1[] = {1, 2, 3, 4, 5, 6, 7};
  float array_2[] = {3.14, 4.5, 5.1, 6.0};
  const char *array_3[] = {"Hello", "World"};

  passByArrayReference(array_1);
  Serial.println();
  passByArrayReference(array_2);
  Serial.println();
  passByArrayReference(array_3);
}

void loop() {
}

Output:

ARRAY Length:7
1  2  3  4  5  6  7  

ARRAY Length:4
3.14  4.50  5.10  6.00  

ARRAY Length:2
Hello  World  
2 Likes

Thanks for your assistance

In most languages, arrays are already passed by reference , meaning the function can modify the original array’s contents. For example, in languages like JavaScript, Python, or Java, when you pass an array to a function, you’re passing a reference to it, so changes inside the function affect the original array. However, in languages like C or C++, you need to explicitly pass by reference (e.g., using pointers in C or & in C++). If you want to avoid modifying the original array, you would typically pass a copy instead. The exact syntax depends on the language you’re using, so it would help to know which one you’re working with.

1 Like

Thanks for the clear explanation! That really helps me understand how array references work across different languages. Appreciate it!