0

I'm a complete c newb. I'm trying to define a few functions in a header file and then implement them in a separate file. But when I try running gcc runtime.c I get the following error:

In file included from runtime.c:1:
runtime.h:7: error: expected identifier or ‘(’ before ‘[’ token

Here's the contents of runtime.h:

#ifndef HEADER
#define HEADER

/*given two arrays of ints, add them

 */
int[] * _addInts(int[] *x, int[] *y);

#endif

What's the error? I tried browsing header files but they started adding things like "extern" and "intern" and crazy ifdef's. Thanks for your help, Kevin

Kevin Burke
  • 61,194
  • 76
  • 188
  • 305

4 Answers4

3

You should just pass pointers (since if you pass arrays to a function, what;'s really passed is a pointer anyway). Also, you can't return an array - again, just return a pointer:

int* _addInts(int *x, int *y);  // equivalent to: int* _addInts(int x[], int y[]);

You'll also have to arrange for the number of elements to be passed in somehow. Something like the following might work for you:

int* _addInts(int *x, int *y, size_t count);

Also - do not fall into the trap of trying to use sizeof on array parameters, since they're really pointers in C:

int* _addInts(int x[], int y[])
{
     // the following will always print the size of a pointer (probably
     //     4 or 8):
     printf( "sizeof x: %u, sizeof y: %u\n", sizeof(x), sizeof(y));
}

That's one reason why I'd prefer having the parameters be declared as pointers rather than arrays - because they really will be pointers.

See Is there a standard function in C that would return the length of an array? for a macro that will return the number of elements in an array for actual arrays and will cause a compiler error (on most compilers) much of the time when you try to use it on pointers.

If your compiler is GCC, you can use Linux's trick: Equivalents to MSVC's _countof in other compilers?

Community
  • 1
  • 1
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • The OP isn't passing arrays to his function. He appears to be trying to pass pointers to arrays, which is probably not what he wants to do, and even if it is, the syntax is wrong. But +1 for solving the problem. – Chris Lutz Apr 25 '11 at 00:52
  • 1
    I'd just like to add that the problem you were encountering is that you were not declaring the arrays correctly. `int[]` is not how arrays are declared in C. – mdec Apr 25 '11 at 00:52
  • Thanks! About the size - why can't you just call sizeof(x) inside the runtime function? – Kevin Burke Apr 25 '11 at 00:52
  • @Kevin, you can call sizeof(x) but that will give you the size in bytes of the array, which will be correct as long as the elements are all 1 byte. A better method is to call sizeof(x)/sizeof(element of x). It's generally considered better style to pass the length in as Michael suggested though – mdec Apr 25 '11 at 00:54
  • @Kevin you cannot use `sizeof` on C array parameters because they're really pointers. For some details, see: http://stackoverflow.com/questions/2089071/array-length-in-c/2089263#2089263 – Michael Burr Apr 25 '11 at 00:57
  • @Kevin - The function doesn't know you have arrays - it only knows you have a pointer to the first element. When you pass an array to a function, the array decays to a pointer to the first element of the array. The function has know way of knowing how large the array is unless you tell it. You can always write `sizeof(arr)/sizeof(arr[0])` when you call the function, which will auto-compute the array length for you (or use a macro like `#define ARRLEN(arr) (sizeof(arr)/sizeof(0[arr]))` so you don't have to write it yourself every time) but the function can't do that on its own. – Chris Lutz Apr 25 '11 at 00:59
3

Get rid of each "[]"

As an array is a pointer, you only need to pass the pointers, like so:

int* _addInts(int* x, int* y);

EDIT: Also pass the size.

  • An array is not a pointer. Arrays decay to pointers, but `sizeof(char[7]) != sizeof(char *)` on any system I know of. – Chris Lutz Apr 25 '11 at 00:55
2

Use:

int* addInts(int* x, int* y, int size);
pic11
  • 14,267
  • 21
  • 83
  • 119
0

You have the [] in the wrong locations. The syntax for declaring arrays in "C" has the [] coming after the item you want to be an array, not between the type declaration and the item (like Java and C# use). I am not sure what you are trying to declare, but here are some options:

If you are trying to declare that you will be using a function named "_addInts()" that returns a pointer to an int, and takes as its parameters two separate arrays of pointers to integers named x and y --

int * _addInts(int *x[], int *y[]);

If you want to declare a function that returns an array of integer pointers:

int * _addInts(int *x[], int *y[])[];

If _addInts is a function that takes two arrays of int (as opposed to arrays of int *):

int * _addInts(int x[], int y[]);

Note that the following are (nearly) equivalent, and can be used interchangeably in declarations such as you are attempting.

int *x

and

int x[]

as are:

int **x

and

int *x[]
Jay Elston
  • 1,978
  • 1
  • 19
  • 38