Phipps Electronics

Order within the next 

FREE SHIPPING OVER $199

50,000+ ORDERS

WORLDWIDE SHIPPING

SSL SECURED

C Tutor: Struct Pointer As Function Parameter

Contents

Are you ready for advanced data handling concepts? Want to learn about struct pointer as function parameter? Read through this article to find out.

Introduction

Last time, you learned about basic pointersstructures, and function parameters. Now you can combine these concepts to add flexibility in accessing your data as well as on creating functions. You’ll see that structures mapped and accessed in memory can be useful for complex projects requiring multiple data sets. With this, you’ll make use of struct pointer as function parameter.

Structures and Pointers: Application in Functions Parameters

Structures can classify different kinds of data sets since the struct data type can have various data types within it. A good application of this scenario is when packets come from different sources. Imagine you have multiple peripherals acquiring data and you have to sort each one and  modify their data using only a single function. The next part will explain this example in detail.

 

Example Application

Suppose you’re working on various data sets from differrent sources like UARTCAN BUS, ADC, to name a few. You want a way to know where these data sets come from and how to work on them. With this, you include a type variable along with your data array on your data structure.

				
					typedef struct data_set_tag {
    int data[8];
    int type;
} data_set_t;
				
			

After this, you can declare each data set coming from your different peripherals as:

				
					data_set_t data_set_adc, data_set_uart, data_set_can;
				
			

You can declare the single function to work on these data sets below:

				
					void work_on_data_set(data_set_t *data_set)
{
    if(data_set->type == 1)
    {
        // do adc functions
        printf("Work on data coming from ADC\n");
        int i;
        for(i=0;i<8;i++)
        {
            data_set->data[i] = data_set->data[i] + 1;
            printf("Data = %i\n", data_set->data[i]);
        }
    } else {
        if(data_set->type == 2)
        {
            // do uart functions
            printf("Work on data coming from UART\n");
            int i;
            for(i=0;i<8;i++)
            {
                data_set->data[i]= data_set->data[i] + 2;
                printf("Data = %x\n", data_set->data[i]);
            }

        } else {
            if(data_set->type == 3)
            {
                // do can functions
                printf("Work on data coming from CAN\n");
                int i;
                for(i=0;i<8;i++)
                {
                    data_set->data[i] = data_set->data[i] +3;
                    printf("Data = %i\n", data_set->data[i]);
                }

            }
        }
    }
    printf("\n");
}
				
			

Note that the data_set parameter is declared as a pointer of type data_set_t. This means the  memory location pointed to by this pointer just needs to have as value a data_set_t type. You can readily pass-by-reference the data sets you declared in the beginning of this example as:

				
					    // work on the data set
    work_on_data_set(&data_set_adc);
    work_on_data_set(&data_set_uart);
    work_on_data_set(&data_set_can);
				
			

Code and Output

				
					#include <stdio.h>
#include <freertos\FreeRTOS.h>
#include <freertos\task.h>

typedef struct data_set_tag {
    int data[8];
    int type;
} data_set_t;

data_set_t data_set_adc, data_set_uart, data_set_can;

void work_on_data_set(data_set_t *data_set);

void app_main(void)
{
    // init data sets simulating data fetch on various devices

    data_set_adc.type = 1;
    data_set_adc.data[0] = 128;
    data_set_adc.data[1] = 128;
    data_set_adc.data[2] = 128;
    data_set_adc.data[3] = 128;
    data_set_adc.data[4] = 120;
    data_set_adc.data[5] = 120;
    data_set_adc.data[6] = 120;
    data_set_adc.data[7] = 120;

    data_set_uart.type = 2;
    data_set_uart.data[0] = 0x0A;
    data_set_uart.data[1] = 0x0B;
    data_set_uart.data[2] = 0x0C;
    data_set_uart.data[3] = 0x0A;
    data_set_uart.data[4] = 0x0B;
    data_set_uart.data[5] = 0x0C;
    data_set_uart.data[6] = 0x0A;
    data_set_uart.data[7] = 0x0B;

    data_set_can.type = 3;
    data_set_can.data[0] = 25;
    data_set_can.data[1] = 25;
    data_set_can.data[2] = 30;
    data_set_can.data[3] = 30;
    data_set_can.data[4] = 30;
    data_set_can.data[5] = 31;
    data_set_can.data[6] = 32;
    data_set_can.data[7] = 32;

    // work on the data set
    work_on_data_set(&data_set_adc);
    work_on_data_set(&data_set_uart);
    work_on_data_set(&data_set_can);


    while(1)
    {
        vTaskDelay(100);
    }

}

void work_on_data_set(data_set_t *data_set)
{
    if(data_set->type == 1)
    {
        // do adc functions
        printf("Work on data coming from ADC\n");
        int i;
        for(i=0;i<8;i++)
        {
            data_set->data[i] = data_set->data[i] + 1;
            printf("Data = %i\n", data_set->data[i]);
        }
    } else {
        if(data_set->type == 2)
        {
            // do uart functions
            printf("Work on data coming from UART\n");
            int i;
            for(i=0;i<8;i++)
            {
                data_set->data[i]= data_set->data[i] + 2;
                printf("Data = %x\n", data_set->data[i]);
            }

        } else {
            if(data_set->type == 3)
            {
                // do can functions
                printf("Work on data coming from CAN\n");
                int i;
                for(i=0;i<8;i++)
                {
                    data_set->data[i] = data_set->data[i] +3;
                    printf("Data = %i\n", data_set->data[i]);
                }

            }
        }
    }
    printf("\n");
}
				
			
				
					Work on data coming from ADC
Data = 129
Data = 129
Data = 129
Data = 129
Data = 121
Data = 121
Data = 121
Data = 121

Work on data coming from UART
Data = c
Data = d
Data = e
Data = c
Data = d
Data = e
Data = c
Data = d

Work on data coming from CAN
Data = 28
Data = 28
Data = 33
Data = 33
Data = 33
Data = 34
Data = 35
Data = 35
				
			

SUBSCRIBE FOR NEW POST ALERTS

Subscribe to be the first to know when we publish a new article!
List Subscriptions(Required)

POPULAR POSTS

Scroll to Top