I am trying to create a structure that can be dynamically added to with malloc and free. There are three functions that I need to implement. I need to be able to print the current structure and move to the next structure and print it as well (Needs to loop through and print each struct).
This part give me an error that goes as follow:
No more issues occured look at what I edited. I cannot submit my own answers
Employee.c:27: warning: assignment makes integer from pointer without a cast
Line is printf(\n The Employee's name is:%s, employee->fullName);
Employee.c:29: warning: assignment makes integer from pointer without a cast
printf("\nThe Employee started on %s", employee->startdate);
Here’s some of my source:
#include <stdio.h>
#include <stdlib.h>
#include "Employee1.h"
/// PRINT RECORDS ///
void printRecords(myEmployee * emp)
{
myEmployee *employee;
for (employee = emp; employee != NULL; employee = employee->next) {
printf("\nThe Employee's Name is: %s", employee->fullName);
printf("\nThe Employee makes is a year $ %f", employee->salary);
printf("\nThe Employee started on %s", employee->startdate);
printf("\n\nThe Next Employee:\n");
}
}
//// CREATERECORD ////
myEmployee *createRecord(char *fullname, char *date, float sal)
{
myEmployee *newEmployees = malloc(sizeof(myEmployee));
if (newEmployees != NULL) {
newEmployees->fullName[MAXSIZE] = fullname;
newEmployees->salary = sal;
newEmployees->startdate[MAXSIZE] = date;
newEmployees->next = NULL;
}
return newEmployees;
}
These are two of the three functions that I have implemented.
This is the header file that is included:
#include <string.h>
#define MAXSIZE 200
typedef struct employee {
char *fullName;
float salary;
char *startdate;
struct employee *next;
} myEmployee;
void printRecords(myEmployee * emp);
myEmployee *createRecord(char *fullname, char *date, float sal);
myEmployee *addRecord(char *fullname, char *date, float sal);
void deleteRecord();
Can anyone help point out what is causing the error or what it means when you have a pointer without cast and how to go about making line 32 a compatible pointer type.
Solution:
In the createRecord function is deleted the “[MAXSIZE]” and the code ran with no issues
Structures with variable-sized data inside can be tricky. There are two primary tricks; one is to place your variable-length data as the very last member of the
struct, and account for its space when you allocate your structures. This works for exactly one variable-length data element. (Well, you can do more complicated tricks, but it surely isn’t worth it most of the time.)In your case, your structures are actually a fixed size — which is a good thing — but it does mean that you also have to allocate memory separately for your char pointers:
Your
->fullNamepointer is garbage at this point. I don’t know what it points to, butMAXSIZEbytes after, it attempts to store a single character. (Which is where your type error comes from — but fixing it takes much more work than you might guess.)Who is in charge of the
char *fullnamethat you have passed into this function? Is it a long-lived piece of memory that you havemalloc(3)ed elsewhere? Or was it a stack-allocated variable in whatever function calledcreateRecord()?If the variable was stack-allocated (or otherwise intended for a short lifespan), then you need to allocate a place to store a copy:
If the variable was allocated with
malloc(3)(or otherwise intended for a long lifespan), then you can simply update pointers:So the right answer depends upon how you wish to approach the long-term storage of your objects. Which functions are in charge of allocating and de-allocating the members of your functions? I strongly recommend the first approach — that way, you can write the allocation routine here and the de-allocation routine in
destroyRecord()(or whatever the paired function is) and be certain you do not have memory leaks.