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 8022387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:15:13+00:00 2026-06-04T22:15:13+00:00

I am writing a C project with pointers and structs, and right now facing

  • 0

I am writing a C project with pointers and structs, and right now facing this problem:
There is a structure

struct Customer
{
    char Name[80];
    char Address[40];
    int ID;
    int Pnumber;
};

and I am gonna make a dynamic array of this structs with *line_count* number of members. I use this code, but it crashes a program:

struct Customer* ph;
ph = (struct Customer*)malloc(line_count * sizeof(struct Customer));

What am I doing wrong?

  • 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-06-04T22:15:14+00:00Added an answer on June 4, 2026 at 10:15 pm

    Good:

    struct Customer* ph;
    ph = (struct Customer*)malloc(line_count * sizeof(struct Customer));
    

    Better:

    struct Customer* ph =
      (struct Customer*)malloc(line_count * sizeof(struct Customer));
    if (!ph) {
      <<error handling>>
      ...
    

    But frankly, it sounds like the problem is elsewhere in your code.

    There’s nothing fundamentally wrong with your malloc().

    Maybe “line_count” is bogus, maybe “malloc()” is failing (in which case, it should return “NULL”) … or maybe you’re accessing the struct incorrectly and/or failing to initialize it correctly.

    A stack traceback of the actual crash would be very useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a MVC3 project. Right now I have a table which has
I am writing a MVC3 project. Right now I have a table which has
We are writing a project where there is a client who generates xml requests,
I'm writing a media player framework for a project I'm working on. This depends
I'm writing a project structure and code analysis extension for visual studio at work.
I'm writing c++ project, which contains several classes. I created .h file named Position.h,
I am writing a project using Eclipse xtext framework. I want to make a
I have my project writing to two text files. one for input and one
I am writing a Flex project in which I need to communicate with Paypal
I'm writing a small project time management program for myself and have run into

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.