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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:54:01+00:00 2026-06-08T05:54:01+00:00

I have a struct defined as struct _element; typedef struct _element Element; struct _element

  • 0

I have a struct defined as

struct _element;
typedef struct _element Element;

struct _element {

    char* StudentName;
    char* StudentID;
    int StudentMarks;
};

A pointer to an Element struct is declared globally as

Element * ePtr;

Now I have a function that returns a pointer to an Element struct. This is defined as shown below. The same ePtr which was declared globally is populated in this function and then returned.

Element * CreateElement(char * jName, char * jID, int jMarks)
{
    printf("CreateElement \n");
    puts(jName); puts(jID); printf("%d\n",jMarks);

    ePtr->StudentName = (char*)malloc(sizeof(char)*strlen(jName));

    strcpy(ePtr->StudentName, jName);

    printf("After Creation \n");
    puts(ePtr->StudentName);

    return ePtr;
}

I am calling this function using

ePtr = CreateElement(iName,iID,iMarks);

from another function. The values stored in the parameters are correct, as shown by puts and printf commands just below the function call line.

My problem is that I’m getting a segmentation fault at the

ePtr->StudentName = (char*)malloc(sizeof(char)*strlen(jName));

line. I checked the same using gdb.

  • 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-08T05:54:04+00:00Added an answer on June 8, 2026 at 5:54 am

    Are you allocating any memory for ePtr?

    Just declaring a pointer to this struct globally isn’t enough: you’ll need to malloc some memory for it also: ePtr = malloc(sizeof(Element);.

    Also be sure to add an extra slot in the malloc for your strings for the null terminator.

    Generally, always initialize your pointers to NULL – you can do that when you declare the global: Element *ePtr = NULL;. Furthermore, try to get your ePtr out of the global-scope, and, check for NULL before you use a pointer, as with ePtr in your CreateElement method.

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

Sidebar

Related Questions

I have a struct defined as: struct { char name[32]; int size; int start;
I have defined a struct ABC to contain an int ID, string NAME, string
I have defined the following struct in C: typedef struct point{ float x; float
In C, I have an array of structs defined like: struct D { char
Basically, I've defined and typedef'ed this struct: typedef struct{ void** elements; int numElements; int
I have this complicated structure thingie: #include <stdlib.h> typedef struct { int x; int
I have this structure : typedef struct xmlelem{ Char *Element_name; Char *Element_Text; pAttr_Element attr_arr[M];
I have a struct that's defined like this: struct Vec3 { float x, y,
I have a data structure defined as struct myDataStruct { int32_t header; int16_t data[8];
I have defined a custom struct which I need to send over to another

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.