Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

Developing a brand architecture strategy

Answered: 1 week ago