Thread: Noob troubles

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    33

    Noob troubles

    Hey everyone. I'm setting up the skeleton of my program right now. I've made a few structures, but I'm trying to make their prototypes and I can't figure out my issue. My code is below

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct struct_car
    {
        char color[20];
        char model[20];
        char brand[20];
        int year;
    } Car;
    
    
    typedef struct struct_person
    {
        char firstName[20];
        char lastName[20];
        int heightInInches;
    } Person;
    
    
    int main(int argc, char** argv) {
    
    
        return (EXIT_SUCCESS);
    }
    
    
    Car[]readCars(char* filename)
    {
        
    }
    
    
    Person[]readPeople(char* filename)
    {
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    C programming language does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    One option would be to pass the array (via pointer) to the function from the caller.

    Code:
    void readCars(Car *car_array, char* filename)
    {
        // ...
    }
    Another would be to dynamically allocate memory to a pointer in the function, and return that pointer.

    Code:
    Car *readCars(char *filename)
    {
        Car *car_ptr;
    
        /* allocate memory to "car_ptr" */
        /* load data to "car_ptr" */
    
        return car_ptr;
    }
    With the second option, you need to ensure the caller frees any allocated memory when it's done with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RPG troubles
    By MartinThatcher in forum Game Programming
    Replies: 3
    Last Post: 10-02-2009, 08:57 PM
  2. troubles in using GDB
    By leiming in forum C Programming
    Replies: 7
    Last Post: 04-18-2008, 01:54 AM
  3. GTK troubles...again...
    By cornholio in forum Linux Programming
    Replies: 4
    Last Post: 01-16-2006, 01:37 AM
  4. Having troubles
    By dison in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2005, 12:58 AM
  5. CD rom troubles...
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2003, 10:56 PM