Question: my malloc is declared incorrectly. can some one help with this please. help me fix declare it correctly please. #include #include #include #include assignment 2

my malloc is declared incorrectly. can some one help with this please. help me fix declare it correctly please. #include
#include
#include
#include "assignment2.h"
int main(int argc, char *argv[]){
// Check if command-line arguments are provided correctly
if (argc <4){
printf("Usage: %s
", argv[0]);
return 1;
}
// Allocate memory for the personalInfo structure
personalInfo *pi =(personalInfo *)malloc(sizeof(personalInfo));
if (pi == NULL){
printf("Error: Memory allocation failed
");
return 1;
}
// Populate the firstName and lastName fields
strncpy(pi->firstName, argv[1], sizeof(pi->firstName)-1);
pi->firstName[sizeof(pi->firstName)-1]='\0'; // Ensure null-termination
strncpy(pi->lastName, argv[2], sizeof(pi->lastName)-1);
pi->lastName[sizeof(pi->lastName)-1]='\0'; // Ensure null-termination
// Assign student ID (replace 123456789 with your actual student ID)
pi->studentID =12345678;
// Populate the level field (you can choose appropriate value based on your level)
pi->level = SENIOR;
// Populate the languages field (include at least three languages)
pi->languages = KNOWLEDGE_OF_C | KNOWLEDGE_OF_JAVA;
// Copy the message from the third command-line argument
strncpy(pi->message, argv[3], sizeof(pi->message)-1);
pi->message[sizeof(pi->message)-1]='\0'; // Ensure null-termination
// Debugging: Print the populated personalInfo structure
printf("Personal Info:
");
printf("First Name: %s
", pi->firstName);
printf("Last Name: %s
", pi->lastName);
printf("Student ID: %d
", pi->studentID);
printf("Level: %d
", pi->level);
printf("Languages: %d
", pi->languages);
printf("Message: %s
", pi->message);
// Free the allocated memory
free(pi);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!