-1

I can get the code working without using a function, but as it is I get this error message:

error: expected expression before 'int'
is_authorized(x,int *authPorts[N]);

By researching this I think it might be something to do with passing an array to a function but everywhere I found something that seemed like it might be relevant had too much information that I didn't understand for me to use it.

Here's my code:

#include <stdio.h>
#define N 8

int is_authorized(int port,int *auth_ports[]);

int main(void) {  
  int x = 73;

  /* authorised ports: */
  int authPorts[N] = {20,73,60,80,212,434,2211,434};

  is_authorized(x,int *authPorts[N]);

  return 0;
}

int is_authorized(int port, int *auth_ports[]){
  int i;
  for (i = 0; i < N; i++){                    
      if (port == *auth_ports[i])
         printf("1");
      else
          printf("0");}
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • Can you change `is_authorized(x,int *authPorts[N]);` to `is_authorized(x, &authPorts);` – Nadir Mar 30 '16 at 15:03
  • @PaulRoub Right, edited comment, i mean `&authPorts` – Nadir Mar 30 '16 at 15:05
  • 1
    When the compiler says: fishy line `is_authorized(x,int *authPorts[N]);`, it might be a good idea to read that line twice. Looks like caused by copy/paste. Voting to close this as simple typo. – Lundin Mar 30 '16 at 15:06

3 Answers3

2

You are trying to define an array, inside an argument.

You can learn more on how to pass array to function here

To answer your question, change to

is_authorized(x, authPorts);

Hugo
  • 254
  • 2
  • 6
0

you do not need the asterix(*) for your array , whenever you want to pass an array to a function the compiler copy the address of the array and send it to the function without needing to tell him that it's called passing by refrence .

-1

fixed code

#include <stdio.h>
#define N 8

int is_authorized(int port,int *auth_ports);



int main(void) {

  int x = 73;

  /* authorised ports: */
  int authPorts[N] = {20,73,60,80,212,434,2211,434};


   is_authorized(x, authPorts);

  return 0;
}

int is_authorized(int port, int *auth_ports){
  int i;
  for (i = 0; i < N; i++){                    
      if (port == auth_ports[i])
         printf("1");
      else
          printf("0");}
  return 0;
}