Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have the below code and I want to call this function in the main function but I am having trouble doing so and keep

I have the below code and I want to call this function in the main function but I am having trouble doing so and keep getting errors. Here is what the function is suppose to do...

kstring kstrfrom(const char *cstr)

Creates and returns a new kstring object that contains a copy of the contents of a null-terminated C string, including the null terminator.

The .length member of the returned kstring should be the length of cstr, plus one for the null terminator. The .data member should be a pointer to newly-allocated memory (using , into which you have copied the contents of cstr, including the null byte at the end.

If there is an error allocating memory, this function should call abort().

Here is the code for the function I want to call...

kstring kstrfrom(const char *cstr) { //first, find the length of the c-string int len = 0; while (cstr[len] != '\0') { len++; } len++; //null terminator counts in length //now allocate memory for the new kstring kstring result = kstralloc(len); //since we have the right length, save it in the kstring result.length = len; //copy the data over for (int i=0;i<(len-1);i++) { result.data[i] = cstr[i]; }

return result; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions