Is the int * parameter exactly the same as the int [] parameter

Are the two following functions essentially the same function?

those. is int*exactly the same as a int[]?

int myFunction(int* xVals, int* yVals, int nVertices);
int myFunction(int xVals[], int yVals[], int nVertices);

How can I use the 1st function? That is, how can I pass arrays in parameters? Is it right / right?

int xVals[5], yVals[5], zVals[5];
myFunction(xVals, yVals, zVals, 5);

// or should it be..
myFunction(&xVals[0], &yVals[0], &zVals[0], 5);
+5
source share
4 answers

In the parameter list, function declarations of functions are equivalent:

int myFunction(int* xVals, int* yVals, int nVertices);
int myFunction(int xVals[], int yVals[], int nVertices);

However, this is not easy to generalize. Inside the function, there is a big difference between:

int AnotherFunction(void)
{
    int array[] = { 0, 1, 2, 3 };
    int *iptr = &array[0];
    ...
}

And in the functional interface, there is a big difference between the two types of parameters:

int arrays_vs_pointers(int **iptrptr, int array[][12]);

You also ask (fixed):

int xVals[5], yVals[5];
myFunction(xVals, yVals, 5);

// or should it be..
myFunction(&xVals[0], &yVals[0], 5);

These calls are valid and equivalent to each other.


" int * , int []?" .

, , , .

" int * , int []?" !

+8

...

true, , int [] int *.

( ), , , , ,

int myFunction(int *xVals, int *yVals, int nVertices);

int myFunction(int xVals[], int yVals[], int nVertices);

.

+2

. , count :

int myFunction(int* xVals, int* yVals, int nVertices);

myFunction(xVals, yVals, zVals, 5);

myFunction 3 , 4 .

+1

(?) :

  • , , , : f(int x[5]) ala int x[] = { 1, 2}; f(x); int x; f(&x);

  • , , : f(int (&x)[5]) , 5 ( - )

  • , : template <size_t N> void f(int (&x)[N]) { ...can use N in here... }

  • , - -to- int... ,

  • , , ... smidge, , , void f(int[]);, - , ( - ) , ( )

0

All Articles