Derived Datatypes in C: An In Depth Explanation

Table of Contents


Derived Datatypes in C: Building On The Foundation

What are derived datatypes in C? This article will give an in depth explanation of what derived datatypes in C are and how you can use them. If you have not yet read my previous post on Starting With C Datatypes which covered the primitive datatypes I would highly recommend you do so! This article will cover the middle column as shown in the diagram below. Let’s get to it!

Return To Table of Contents


Array Datatypes in C:

Array datatypes are a collection of fixed sized sequential elements stored in memory that can be accessed with a given index. To declare an array in C three pieces of information are needed, the type, the name of the array and how many elements it can hold. Note that the square brackets are needed to declare it as an array [].

// An example of declaring an array.
unsigned char myArray[3];

Note: Keep in mind when declaring an array on the stack it is going to be filled with junk until it is initialized. There are a couple of ways you can initialize an array see below:

Return To Table of Contents


Array Initialization Method 1:

// Method number 1 for clearing out an array.
unsigned char myArray[3] = {0};

Note: This will zero out all elements of the array and is a bit of a shorthand.

Return To Table of Contents


Array Initialization Method 2:

// Method number 2 for initializing an array.
unsigned char myArray[3] = {1, 2, 3}; 

Note: This will initialize the elements of the array as 1, 2, 3. This is good for small arrays but can get quite tedious for larger arrays.

Return To Table of Contents


Array Initialization Method 3:

// Method number 3 for filling an array with a specific value.  
// In this case I chose the value of zero.
unsigned char myArray[3];
memset(myArray, 0, sizeof(unsigned char));

Note: This is using a standard C function called memset. The second parameter here is 0 which can be replaced with any value if desired.

Return To Table of Contents


Array Indexing:

To get the value of an array element, an index needs to be provided. It then needs to be assigned to a variable of the same type as the array element see the example below:

// An example of how to index an array.
#include <stdio.h>

int main(void)
{
  unsigned char myArray[3] = {7, 8, 9};
  unsigned char myValue = 0;

  // Assign 9 to the variable myValue.
  myValue = myArray[2];
  printf("My value is: %i\n", myValue);

  // Assign 8 to the variable myValue.
  myValue = myArray[1];
  printf("My value is: %i\n", myValue);

  // Assign 7 to the variable myValue.
  myValue = myArray[0];
  printf("My value is: %i\n", myValue);
  
  // Hold the console window open
  printf("Press any key to continue...\n");
  (void)getchar();
  return 0;
}

Note: In C, indices for arrays are zero based! This means that the first element starts counting at zero not one. A lot of new programmers get tripped up on this concept.

Return To Table of Contents

Pointer Datatypes in C:

pointer is a variable that references another variable by its address. To declare a pointer in C two pieces of information are needed, the type of data the pointer is pointing to and the name of the pointer. Note that the asterisk (*) character is needed to declare a variable as a pointer. Also, a good habit to form is to start pointer variables with the letter “p” as shown below.

// An example of declaring a pointer.
unsigned char* pMyValue;

Note: Keep in mind when declaring a pointer on the stack it is going to be filled with junk until it is initialized. It is best to initialize it to NULL or to the address of another variable.

Return To Table of Contents


Pointer Initialization Method 1:

The example below shows how to initialize the pointer to NULL.

// An example of NULL pointer initialization.
unsigned char* pMyValue = NULL;

Note: When a pointer is not being used it is always good practice to set it back to NULL. If this is not being done it can lead to undefined behavior by having what is called a dangling pointer or a pointer that is pointing to some memory that is no longer being referenced. Also, before using a pointer it is always good to check to make sure it is not NULL before using it otherwise again this can lead to undefined behavior.

Return To Table of Contents


Pointer Initialization Method 2:

The example below shows how to initialize a pointer to point to another variable.

// An example of initializing a pointer to another variable.
unsigned char myValue = 7;
unsigned char* pMyValue = &myValue; 

Note: In this example the variable myValue has been set to the value of 7. The pointer pMyValue is set to point to the address of myValue by using the & to get the address location of myValue.

Return To Table of Contents


Pointer Dereferencing:

One might ask I have my pointer variable pointing at the memory location of another variable but how do I get the actual value of this variable instead of its address? To do this we will need to dereference the pointer to get the actual value pointed to by the pointer variable. Note that the asterisk (*) character is needed to dereference a pointer to get the value it is actually pointing to. See the example program below to demonstrate this.

// An example of how to dereference a pointer.
#include <stdio.h>

int main(void)
{
  unsigned char myValue = 7;
  unsigned char* pMyValue = &myValue;
  
  // Print the address of myValue.
  printf("The address of myValue is: %p\n", &myValue);
  
  // Print the address pointed to by pMyValue.
  // This is the exact same address as myValue.
  printf("The address pointed to by pMyValue is: %p\n", pMyValue);
  
  // Print the value of myValue.
  printf("The value of myValue is: %i\n", myValue);
  
  // Dereference the pointer and print the value.
  // This is the exact value as myValue.
  printf("The value of myValue is: %i\n", *pMyValue);
  
  // Hold the console window open
  printf("Press any key to continue...\n");
  (void)getchar();
  return 0; 
}

Return To Table of Contents


Function Datatypes in C:

A function itself is also considered a datatype. The most noticeable function in the C programming language is main(). The main function is the first function of a program that executes and the last function to exit (Note: This function may be named different for embedded platforms, however it should be known there is a starting function regardless). A function when compiled will resolve to an address in the computer’s memory. To declare a function in C three pieces of information are needed, the return type, the name of the function and the parameter list.

int myFunctionExample(unsigned char  input1, 
                      signed   short input2, 
                               float input3);

Note: In this example, the return type is of type int the name of the function is myFunctionExample and the parameter list has 3 inputs unsigned char input1, signed short input2 and float input3.

Return To Table of Contents


Derived Datatype Recap:

In this post we have learned that derived datatypes come from primitive datatypes and can be used to help in the design and implementation of writing code. Please, keep in mind that there are a lot more advanced topics yet to be covered. With that being said, we need to learn to walk before we run and I want to make sure you have a solid grasp of these fundamental concepts before getting deeper in the weeds.

If you have any questions or comments please feel free to comment below or send me a message. Also, don’t forget to subscribe to receive the latest updates via email from Tsunami Code!

Until next time!

-Brandon Sloat

Return To Table of Contents

2 thoughts on “Derived Datatypes in C: An In Depth Explanation”

  1. Sometimes it’s good to go back to the basics and learn something you did not know, or never thought about, inspite of it being there in front of you.
    Looking forward to your next blog!

    1. Hey there Neeraj thanks for taking the time to read my post! I agree it is always good to go back and review the fundamentals. There is always something you can look at a little differently and get something useful out of it! Thanks again!

Comments are closed.