-4

why does this code does not compile?

int substring(char * text, int k, int m, (char *) patterns[]) {
//stuff
// and example return is -1, meaning the sub string was not found
}

I know that the parenthesis in the (char *) is the problem, but I can't seem to figure it out why? I try declaring making patterns as a string in C++ and C but it doesn't compiles.

the error message is "error: expected declaration specifiers or '...' before '(' token" . It's algorithm for my class and my professor gave us that function with those parameters. in addition "patterns is an array of k pointers to \0-terminated strings of length m. I would just like to know why the given function does not compiles even if you just write return -1.

  • 3
    What is the exact error message you get? – πάντα ῥεῖ Dec 02 '16 at 18:26
  • 2
    *"why does this code does not compile?"* Why would that compile, and what would it be supposed to do if it would compile? – Baum mit Augen Dec 02 '16 at 18:28
  • Please show how you are calling it, and the definition of the variable passed to the last argument. The syntax says you are passing an array of pointers. – Weather Vane Dec 02 '16 at 18:28
  • what is `(char*)patterns[]`? The compiler is expecting a type and a name. You've provided no type and a cast. It is just ill formed. You can declare it is a `void*` and cast `patterns` inside the function if you need the function to reinterpret it – RyanP Dec 02 '16 at 18:28
  • Randomly typing code and seeing if it is valid is not the way to go. It sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Dec 02 '16 at 18:30
  • 1
    You have the exact error message on the screen right in front of your face. There is **absolutely no valid reason** foryou to fail to include it in your question. It's an extremely relevant detail, and you can copy/paste it into the post directly without even having to type it. You're asking for **free help** to solve **your problem** - there is no excuse for not providing details we can use to provide that help. – Ken White Dec 02 '16 at 18:30
  • the error message is "error: expected declaration specifiers or '...' before '(' token" . It's algorithm for my class and my professor gave us that function with those parameters. in addition "patterns is an array of k pointers to \0-terminated strings of length m." – Armando024 Dec 02 '16 at 18:36
  • "Stuff". We might need a *little* more description to help you. – MD XF Dec 02 '16 at 19:18

2 Answers2

3

Type specifiers shall not be enclosed in parentheses in declarations. You declared a function. It is not a call of a function with arguments when you indeed could apply a casting.

There is no sense to reinterpret a two-dimensional character array or an array with elements of type char * containing strings as a one-dimensional array.

Take into account that this declaration of the parameter

char * patterns[]

is equivalent to

char ** patterns
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

char * is a type declaration, not a type cast (i.e. conversion).

Kirill Bulygin
  • 3,658
  • 1
  • 17
  • 23