Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4237178
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:44:21+00:00 2026-05-21T02:44:21+00:00

I am new to C, but not to programming. I have been roped into

  • 0

I am new to C, but not to programming. I have been roped into modifying a C program to make it gather multiple pieces of data and put them in an array. I am not allowed to post actual source code, so I have made the following example which illustrates what I am trying to do:

#include <windows.h>

typedef struct 
{
    int size;
    long rpm;
} ENGINE;


typedef struct 
{
    int doors;
    int wheels;
    ENGINE engine;
} CAR;

int newCar(CAR *car)
{
    ENGINE eng;
    eng.rpm=30000;
    eng.size=1600;
    car->doors=4;
    car->wheels=4;
    car->engine=eng;
    return 0;

}


int getCars(CAR *cars[], int n)
{
    int i = 0;
    for (i=0; i<n; i++)
    {
        newCar(cars[i]);
    }

    return 0;
}

int carCount(int *count)
{
    *count = 4;
    return 0;
}

int main()
{
    int n = 0;
    CAR *cars = (CAR*) malloc(sizeof(CAR));
    carCount(&n);

    cars = (CAR*)realloc(cars, n * sizeof(CAR));
    cars[1].doors = 2;
    getCars(&cars,n);

}

The code above compiles but fails when I try to set members of the car struct inside the newCar routine. I’m not sure whether my realloc on the cars array is doing what I want it to, I based it on some other posts on stackoverflow. Does it look ok?
How can I access the members of car from the newcar routine?
Is this a reasonable way of doing this?
Many thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:44:22+00:00Added an answer on May 21, 2026 at 2:44 am

    You don’t need double indirections!
    A simple pointer to CAR can point to different CARs.

    Create space for the number of CARs you need: ok

    A pointer to the first CAR in that space can easily be made to point to the other CARs.

        CAR *cars = malloc(sizeof(CAR));
    

    if malloc didn’t fail cars points to a space large enough to hold 1 CAR

        cars = realloc(cars, n * sizeof(CAR));
    

    if realloc didn’t fail cars now points to a space large enough to hold n cars
    pass that pointer to your functions, along with how many cars it points to

        getCars(cars, n);
    

    and use the pointer in the functions

    int getCars(CAR *cars, int n)
    {
        int i = 0;
        for (i=0; i<n; i++)
        {
            /* here, cars[0] is the first car; cars[1] is the second ... */
            /* we can pass the address with &cars[i]                     */
            /* or make arithmetic with the pointer itself:               */
            newCar(cars+i);
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Django and Web programming in general. Have googled but could not
I'm new to Python (I have been programming in Java for multiple years now
I'm pretty new to Perl but have been programming in java for several months
I am new to Android but not to programming. I have taken C++ in
I'm new to Java but not to programming. I'm reading the book Beginning Android
I'm not sure whether these are programming questions, but I'm sure lots of new
First, i'm new at Java-programming and my native lang is not english, but still
I am new to the R language, but not to the programming world. I
I've been programming in Java for a little while now but have never really
I am very new to programming, and have been banging my head against the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.