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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:30:38+00:00 2026-05-26T09:30:38+00:00

I am having trouble with moving my pointer in a dynamically changing structure. I

  • 0

I am having trouble with moving my pointer in a dynamically changing structure.
I have created my code where you can malloc more memory and this seems to be working.
The problems that I am running into is how to add to the structure, how to free memory and how to move from structure to structure and print all items.

I am trying to test add and print (the delete function that is there does not seem to work, segfaults)

When I add to the struct and then print the struct I get a segfault from the values that I have added. I don’t know if I am moving from the first struct to the next struct correctly.

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

/********************************************
Creates more memory for size (strut * rec+1)
*********************************************/

employee *create(int record){
employee *new_employee = malloc(sizeof(employee) * (record+1));

return new_employee;    
}

/********************************************
Copies the data from one structure to a new structure with 
size "structure" multipled by rec+1
***********************************************/
employee *copy(employee *data, int record){
    employee *new_employee = create(record);
int i;
    for(i = 0; i<record;i++){
        new_employee->first = data->first;
        new_employee->last = data->last;
        new_employee->start_date = data->start_date;
        new_employee->sal = data->sal;
        data++;
    }
    /********************
    Needs to free the old struct
    *********************/
    //deleteData(data, record);

return new_employee;
}
/********************************************
Function prints everything in the struct
*********************************************/
void printStruct(employee *data, int record){
int i;

    for(i = 0; i<record; i++){
        printf("\nEntry: %d\n", i+1);           
        printf("The employee's name is %s %s\n", data->first, data->last);
        printf("The employee was hired on: %s\n", data->start_date);
        printf("The employee make $%f\n\n", data->sal); 
        data++;     
    }
}
/******************************************
Function frees the old data base
*******************************************/
void deleteData(employee *data, int record){
int i;
    for(i = 0; i<record; i++){
        free(data->first);
        free(data->last);
        free(data->start_date);
        data++;
    }
    free(data);
}
/******************************************
Adds an employee to the new structure
*******************************************/
employee *add(employee *data,char *fname, char *lname, char *date, float salary, int record){
employee *employeeDB = create(record);
employeeDB = copy(data, record);
int i;
    employeeDB++;
    employeeDB->first = fname;
    employeeDB->last = lname;
    employeeDB->start_date = date;
    employeeDB->sal = salary;

return employeeDB;
}




/**************************
Starts of the main function
***************************/

int main(void){
    //Keeps track of the number of records that are in the structure
int rec = 0;
    //Keeps the number of accesses to the structure. Even with the one entry   the structure has not been accessed. 
int acc = 0;
    //Holds the input information for the menu
int input;
    //holds the information for inputing user first name
char *fname;
    //holds the information for inputing user last name
char *lname;
    //holds the information for for the startdate
char *start;
    //holds the information for the salary;
float sal;
/*********************************
This next section adds an employee to the record

************************************/
//This creates the first entry to the dynamic structure.
employee *first_employee = create(rec);
first_employee->first = "FIRST";
first_employee->last = "LAST";
first_employee->start_date = "June-20th-2006";
first_employee->sal = 55555.55;
//increase the number of records    
rec = rec+1;

employee *new_employeeDB = add(first_employee, "fname", "lname", "JUNE-20th-2010", 55555.55, rec);
rec = rec + 1;
printStruct(new_employeeDB, rec);


printf("%d\n", (sizeof(employee)* rec));


}
  • 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-26T09:30:39+00:00Added an answer on May 26, 2026 at 9:30 am

    First problem: Ok… you didn’t include the declaration of employee type.
    I guess first and last are declared as char pointers, and this is an error.
    You need them to be fixed size char array or this will never work.
    Choose a maximum length for the string and declare the structure like this.

    typedef struct
    {
        char name[50];
        char surname[50];
        ....
    } employee;
    

    Previous post:

    Well i must admit i don’t like much this way of implementing the thing.
    I would use a more stable approach.

    First of all, since it is just a list of items, you can use a doubly linked list.
    This will allow you to add and remove elements with an O(1) complexity. Quite good, indeed.

    This will also allow you to iterate all items from the first to the last.

    For doing that i would use a more OOP oriented approach 🙂 I know, we are in C but listen to this idea.

    typedef struct
    {
        MyList* OwnerList;
        Employee* Previous;
        Employee* Next;
    
        char name[50];
        int age;
    } Employee;
    
    typedef struct
    {
        Employee* First;
        Employee* Last;
        int Count;
    } MyList;
    
    MyList* AllocList() { return calloc(sizeof(MyList), 1); }
    
    void DeleteList(MyList* list)
    {
        Employee* current;
        Employee* next;
        for (current = list->First; current != NULL; current = next)
        {
            next = current->Next;
            free(current);
        }
        free(list);
    }
    
    int GetCount(const MyList* list)
    {
        return list->Count;
    }
    
    Employee* AddAmployee(MyList* list)
    {
        Employee* result = calloc(sizeof(Employee), 1);
        Employee* last = list->Last;
        if (last != null)
            last->Next = result;
        else
            list->First = result;
        result->Previous = last;
        list->Last = result;
        ++list->Count;
        result->OwnerList = list;
        return result;
    }
    
    void RemoveEmployee(Employee* employee)
    {
        /* i leave removal for you as exercise :) look for doubly linked list */
        free(employee);
    }
    

    Now, to iterate all items is simple:

    Employee* current;
    for (current = list->First; current != null; current = current->Next)
        printf("%s %d\n", current->name, current->age);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just learning Clojure and am having trouble moving my code into different
I have tried translating (moving) a canvas but I'm having trouble with the timers.
Having a bit of trouble moving objects I've created into an array. So what
I'm having trouble moving a website from one server to another. It seems to
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
I having trouble in dividing the HTML frames. I have been using the following
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
I am having trouble believing the following code is the most efficient way to

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.