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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:11:46+00:00 2026-05-29T07:11:46+00:00

I have two structs and I have an array of 30 pointer StudentType. I

  • 0

I have two structs and I have an array of 30 pointer StudentType.
I have a problem with malloc(). When I compile it it’s ok. But when I try to debug it, it shows “Segmentation Fault” in Dev c++.

In Eclipse, it shows up anything on console. I think that my mistakes are on these lines of code:

students[0]=(StudentType *)malloc(sizeof(StudentType)*NumOfStudents);
(*students[NumOfStudents]).firstName=(char*)malloc(sizeof(char[30]));
(*students[NumOfStudents]).lastName=(char*)malloc(sizeof(char[30]));

That’s a part of my code.

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

typedef struct{
    float firstAssignment;
    float secondAssignment;
    float midterm;
    float final;
    float finalMark;
}StudentRecordType;

typedef struct{
    char *firstName;
    char *lastName;
    int idNumber;
    StudentRecordType marks;
}StudentType;
StudentType *students[30];
char firstName[30];
char lastName[30];

int ReadFromFile();
int PrintAll();
int NumOfStudents;
int i;

int main(void)
{
    ReadFromFile();
}

int ReadFromFile()
{
    FILE *fp;
    fp=fopen("project2-askhsh2.dat","r");
    if(fp==NULL)
    {
        printf("Error opening file.\n");
    }
    else
    {
        printf("Successful open of project2-askhsh2.dat\n");
    }
    fscanf(fp,"%d",&NumOfStudents);
    printf("%d\n",NumOfStudents);
    students[0]=(StudentType *)malloc(sizeof(StudentType)*NumOfStudents);
    (*students[NumOfStudents]).firstName=(char*)malloc(sizeof(char[30]));
    (*students[NumOfStudents]).lastName=(char*)malloc(sizeof(char[30]));

    for(i=0;i<NumOfStudents;i++)
    {
        (*students[i]).idNumber=i;
        fscanf(fp,"%s %s", (*students[i]).firstName,(*students[i]).lastName);
        fscanf(fp,"%f %f %f %f",(*students[i]).marks.firstAssignment,(*students[i]).marks.secondAssignment,(*students[i]).marks.midterm,(*students[i]).marks.final);
        printf("%s",(*students[i]).firstName);//, students[i].lastName);
    }

}
  • 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-29T07:11:47+00:00Added an answer on May 29, 2026 at 7:11 am
    (*students[NumOfStudents]).firstName=(char*)malloc(sizeof(char[30]));
    

    I think you meant:

    students[some_index]->firstName = malloc(30);
    

    With some_index something below NumOfStudents.

    So your loop becomes:

    for(i=0; i < NumOfStudents ; i++)
        {
            /* students[] is an array of pointers
            ** students[i] is a pointer
            */
            students[ i ] = malloc(sizeof *students[ i ]);
            students[ i ]->idNumber=i;
    
            students[ i ]->firstName = malloc(30);
            students[ i ]->lastName= malloc(30);
    
            fscanf(fp,"%s %s"
                , students[i]->firstName
                , students[i]->lastName
                );
            fscanf(fp,"%f %f %f %f"
                , &students[i]->marks.firstAssignment
                , &students[i]->marks.secondAssignment
                , &students[i]->marks.midterm
                , &students[i]->marks.final
                );
            printf("%s",students[i]->firstName);//, students[i].lastName);
        }
    

    And using a global index named “i” can be considered bad style.

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

Sidebar

Related Questions

I have an array of structs. The struct has two function pointers. Each element
I have an array of structs where I perform two searches. First I search
So, I have two structs: struct coordinate { float x; float y; } struct
Hey I have a problem comparing the value of a CGPoint (struct with two
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of structs. array_of_structs1 array_of_structs2 The struct class looks like this,
I have 3 C++ structs as well as two methods and I want to
In my program I have a few structs and a char array that I
In a C++ program I have two classes (structs) like struct A { int
I have two window form applications written in C, one holds a struct consisting

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.