Heap Sort in C With Simple Functions, Strings, and Variable Memory

heap sort in cC functions are used to pass values to reusable code and return a different value. The C language uses an array to work with strings, which are basically an array of characters that make up a word, sentence or paragraph. The following code is an example of how you define strings in C:

char mystring[] = "string";
char mystring[7] = "string";

Take note of the second example. There are only 6 characters in “string,” but the array has 7 indexes. The 7th index is for the null termination character, which is denoted in C as “”.

Get to know how to work with the heap and the stack

Now, you can define a function that returns a string. The following code defines a function that takes a string character array as a parameter and then returns a string of characters:

char[1000] hello(char greeting[], int length)

The “greeting[]” array is the input for your hello function. The length integer is used to determine the length of the character array when the function executes. The purpose is to allow you to input any length in the greeting array without statically defining the array and causing bugs in your program. The string returned, however, can only have a maximum length of 999 (999 characters plus the termination character).

It’s your job as the programmer to ensure that the string character length isn’t exceeded. If you accidentally exceed the maximum length, your program will return an error and crash.

Now, using the above function, you want to concatenate the string you pass to the greeting function and a message. The result string is then returned from the function. The following code is an example of concatenating a string:

char[1000] hello(char greeting[], int length)
return strcat (“Hello,”, greeting);

You can then use this function to get a string after passing a greeting message. For instance, the following code calls the function and returns “Hello, Fred”:

message = hell(“Fred”, 20);

You can then print out the result to the user’s screen. You print values to the screen using the printf function. The following code prints out “Hello Fred” to the user’s screen:

printf ("%s \n", message);

When you declare variables at the function level, these variables are only available from within the function itself. To get around this issue, you can create a global variable. The issue is with the memory allocation. You need to allocate memory space for this global variable to make it available from any location in your program. To perform a global variable allocation in memory, you use the malloc declaration. Malloc stands for “memory allocation function.” The malloc statement allocates space for your global string variables and stores it in a global memory area called the “heap.”

Understand C strings at Udemy.com

To understand programming better, you must understand the memory allocation areas called the “stack” and the “heap.” Any local variables declared inside a function have allocated memory in an area known as the stack. When you exit from a function, the variables on the stack are cleaned up. In other words, they no longer exist. So it’ s not safe to create strings inside a function and return those strings to code outside that function. To perform this function, you need to allocate globally available memory so that the strings continue to exist in memory both inside and outside the function. The malloc function allocates space in a part of your computer’s memory called the “heap.” Variables that are allocated on the heap continue to exist even after you’ve exited the function. The malloc function is specifically for this purpose.

Now, you have to tell malloc how much memory – that is, how many bytes – need to be allocated. It doesn’t matter too much if you allocate more memory than really needed, but it could be catastrophic if you allocate less memory than is required. The following code shows you how to use the malloc function to allocate 100 bytes of memory storage:

char  mystring[]  = (char)malloc(100);

Incidentally, you can also define variables with a null character value. This is beneficial if you want to initialize a string and don’t know what value you want to give it at first. You can do that before attempting to concatenate another string such as ‘hello’ to your existing variable. Null has the effect of making an empty string – one that contains no printable characters on the outset, but does have a ‘null’ terminator. Without that ‘null’ terminator, the array might contain random bits of data that happen to be lurking in computer memory. And that’s going to have completely unpredictable effects on your program.

To use the above allocated variables, you simply call the function and return the string result. However, because the mystring variable is declared globally, you can use it in the function and outside of the function. Basically, regardless of where you want to call the variable, it will always be available throughout your program.

What happens though if you forget to allocate memory for a global variable? Remove the malloc statement in your code and try to run it.  Your program will execute up until you try to use the variable outside of the function. The program crashes. If it’s a user running your program, the user would have to restart the program and try again.

You can also test your program to see what happens when you do not include the null termination character at the end of your strings. Your program will run and it won’t even crash when it concatenates your strings. Instead, it concatenates your string and returns “Hello, Fred” but you will see random characters at the end of the string. These random characters are from your computer’s memory. The termination character marks the end of your string, so it’s important to use this character to avoid garbage characters in your user output.

This is a tiny part of the C programming language, but it’s important to understand how strings work. Understanding strings is part of any C programming requirement for input and output. Get a comprehensive guide to C for beginners in 10 easy steps here.