Hi there,

As the title says, I'm having an issue passing an array of structures to a function.The structure reads and tokenises information from a text file. I then need to do things with that data using several different functions. Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
char carID[4];
char carType[8];
int engineSize;
char bodyType[9];
int numSeats;
char transType[9];
int mileage;
char available;
} cars_t;

int main()
    cars_t cars[8];
    char line[100];
    char line2[100];

    char *nextWordPtr;
    int count = 0;

    FILE *ptr_cars;
    ptr_cars = fopen("cars.txt", "r");
    while ( fgets ( line, sizeof line, ptr_cars ) != NULL )
        strcpy(line2, line);
        nextWordPtr = strtok(line2,";");
        strcpy(cars[count].carID, nextWordPtr);
        and so on for each struct variable...

    int choice = 0;
    while(choice != 7)
        scanf("%d", &choice);

        if(choice == 3) listrented(cars_t cars[]);

    return 0;

When choice 3 is entered, following function is called:

int listrented(cars_t cars2)

A simple as a function can be. It's in a separate .c file, maintained in the same project using CodeBlocks. The error that I get is:

error: expected ')' before 'cars2'

Huh? I'm not sure why that's appearing at all. Being able to pass this array of structures to a function would be great, and if anyone could help me out, I would definitely appreciate it.


4 Years
Discussion Span
Last Post by TarkiB

From what I can tell, you have a couple errors in your function definition and your function call.
Try this:
int listrented(cars_t cars2[])


I don't know how this would relate to your error, but it might help.


Number one. If your passing a pointer to an array then your function should accept a pointer to an array.

int listrented(cars_t * cars2)
  /*shoud return something here*/

Number two. When you call a function, you don't declare the varibles

> if(choice == 3) listrented(cars_t cars[]);

/*should be*/

> if(choice == 3) listrented(cars);

Edited by gerard4143


Thanks for the suggestions. I made those changes, plus a few others (had to make sure typedef was in every .c file), and I'm now no longer encountering the problem.

I am having a different problem though. I'm trying to cycle through an array of structures. The number of structures can change, so I can't define a number of times to cycle. So I tried this instead:

while(res2[x] != NULL)

Where x is an int that starts at 0 and increments after every cycle. When compiling, I get the following error:

error: invalid operands to binary != (have 'reservations_t' and 'void *')

reservations_t is a structure, and I assume the void is because it's in a void function.

Is this not a valid way to cycle through the array? Is there a better way to do it?


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.