J. Nav J. Nav - 25 days ago 6
C Question

C programming Sorting Function Error

I have this function. I'm working on for class and I need a little help about why I'm getting this error.

Below is a snippet of coding:

int testArray[] = {...};

void display(int testArray, int nItems)
{
int loopCounter;

printf("[");

for(loopCounter = 0; loopCounter < nItems; loopCounter++)
{
printf("%d", testArray[loopCounter]);
}

printf("]");
}


The error I'm getting is:

subscripted value is neither array nor pointer nor vector
if(testArray[loopCounter]);


However, this is exactly what the professor wanted us to do, so i'm a bit confused as to why I'm getting this error. Any ideas?

Answer Source

It seems you've used the wrong type for the array, the correct one is int *

int testArray[] = {...};

void display(int* testArray, int nItems)
{
   int loopCounter;

   printf("[");

   for(loopCounter = 0; loopCounter < nItems; loopCounter++)
   {
      printf("%d", testArray[loopCounter]);
   }

   printf("]");
}