1

I'm hoping this will turn out to be a simple question. I'm looking at some C++ code where it's passing an array of structs to a method, example:

dosomething(arrayOfStructs + n);

The array was initialized at 256 items, and prior to calling the example method, it performs some work which populates the array with some items. The variable n, in this context, represents the number of items currently in the array.

I have no idea what the n variable does for the method being called, or even possibly to the array itself. The only thing I can think of is the number following the array is resetting the array size somehow. I could be totally off on that one.

I'm trying to follow the C++ code and try something similar in JavaScript. I am stuck on this n variable following the array.

6
  • 4
    arrayOfStructs decays to a pointer to the 1st element. Adding n to this pointer will point to the nth element. Commented Aug 14, 2020 at 17:10
  • 3
    It moves the pointer to the end of the array, basically to the nonexisting element arrayOfStructs[n] (since you said n is the number of items in the array). Or maybe you meant the number of items already filled in, in this case it's just pointing to the first element not yet filled in (again, arrayOfStructs[n]). We would need a bit more context of both the calling code and the dosomething function to understand what a suitable representation in JavaScript would be, because those concepts cannot be translated 1:1 as JS doesn't have pointers. But you can imagine arr + x as &arr[x]. Commented Aug 14, 2020 at 17:10
  • 1
    This is called pointer arithmetic. Please lookup the subject. Better yet, find a good textbook and familiarize yourself with the fundamentals of the language. Commented Aug 14, 2020 at 17:11
  • CherryDT, thanks for the input. You pretty much got what I was trying to do. 'n' is the number of items currently filled in, and not the overall size per se. I got it. And R Sahu, thanks for the input as well. I definitely will look at the pointer arithmetic. My goal is not to leanr C++ per se but some things I do need to understand to translate some of it to JavaScript. I figured out a lot of thins already over to JS so thanks for the input everyone. Commented Aug 14, 2020 at 17:19
  • There's no Javascript equivalent to pointer arithmetic, you're going to have to understand and then rewrite the code. Presumably you could pass arrayOfStructs and n as separate variables to the function. Commented Aug 14, 2020 at 17:19

1 Answer 1

1

I suppose that n has an integer type.

In the expression used as an argument of this call

dosomething(arrayOfStructs + n);

the array designator arrayOfStructs is implicitly converted to pointer to its first element. Adding the integer value n the pointer to the first element of the array is moved to the n + 1 element of the array (if to count elements starting from 1). This is called the pointer arithmetic.

You can consider this call like

dosomething( &arrayOfStructs[n]);

To make it more clear consider this simple demonstrative program.

#include <iostream>

void display( const int *p )
{
    std::cout << *p << ' ';
}

int main() 
{
    int a[] = { 1, 2, 3 };
    const size_t N = sizeof( a ) / sizeof( *a );
    
    for ( size_t i = 0; i < N; i++ )
    {
        display( a + i );
    }
    
    std::cout << '\n';
    
    for ( size_t i = 0; i < N; i++ )
    {
        display( &a[i] );
    }

    std::cout << '\n';

    return 0;
}

Its output is

1 2 3 
1 2 3 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Vlad for the info

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.