C/C++

 

 

 

 

struct

 

struct is a special data type that is used to package multiples of related data (data type) into single variable. I would say it would be most useful data type in C, but when you just start learning a programming it may not be obvious on why you need to use this data type and when / how you use it.  But as you spend more time and write more program, you would gradually 'FEEL' how useful it is and why I need this.

 

 

defining and calling a struct

 

 

Example 01 >

 

In this example, I packed two varible x and y into one package called Point.  You may write the same program without using struct. You may define two separate variable float PointX, float PointY. It is up to you on whether you use struct or independent variable, but to me it is more meaningful for me to pack these two variable into one package because these two variables are related to represent a position in a coordinate system.

 

#include <stdio.h>

 

struct Point {

    

    float x;

    float y;

    

};

 

int main()

{

    

    struct Point pt;

    

    pt.x = 1.2;

    pt.y = 2.1;

    

    printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);

    

    return 1;

}

 

Result :

The coordinate of pt is = (1.200000,2.100000)

 

 

Example 02 >

 

This example is not to show about how to define/use a struct. The difference between this example and previous example is to secure the memory space before you use it.  You may not use this way all the time, but it would be good practice to do this if you are handling a struct that takes up large memory.

 

#include <stdio.h>

 

struct Point {

    

    float x;

    float y;

    

};

 

int main()

{

    

    struct Point *pt;

    

    pt = (struct Point*) malloc(sizeof(struct Point));

    

    pt->x = 1.2;

    pt->y = 2.1;

    

    printf("The coordinate of pt is = (%f,%f)", pt->x, pt->y);

    

    return 1;

}

 

Result :

The coordinate of pt is = (1.200000,2.100000)

 

 

Example 03 >

 

This example is just to show you how to define and use a struct with typedef.

 

#include <stdio.h>

 

typedef struct POINT {

    

    float x;

    float y;

    

} Point;

 

int main()

{

    

    Point pt;

    

    pt.x = 1.2;

    pt.y = 2.1;

    

    printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);

    

    return 1;

}

 

Result :

The coordinate of pt is = (1.200000,2.100000)

 

 

Example 04 >

 

This is another example to show you how to define and use a struct with typedef with a little bit different way comparting to example 03.

 

#include <stdio.h>

 

struct POINT {

    

    float x;

    float y;

    

};

 

typedef struct POINT Point;

 

int main()

{

    

    Point pt;

    

    pt.x = 1.2;

    pt.y = 2.1;

    

    printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);

    

    return 1;

}

 

 

Result :

The coordinate of pt is = (1.200000,2.100000)

 

 

Example 05 >

 

This is another example to show you how to define and use a struct with typedef with a little bit different way comparting to example 03 and 04.

 

#include <stdio.h>

 

typedef struct{

    

    float x;

    float y;

    

} POINT;

 

typedef POINT Point;

 

int main()

{

    

    Point pt;

    

    pt.x = 1.2;

    pt.y = 2.1;

    

    printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);

    

    return 1;

}

 

 

Result :

The coordinate of pt is = (1.200000,2.100000)

 

 

Example 06 >

 

This is an example of using a struct as a pointer. The important thing to note is that I am using malloc() to secure a memory for the struct before I use (assign value to it).

 

#include <stdio.h>

#include <malloc.h>

 

typedef struct POINT {

    

    float x;

    float y;

    

} Point;

 

int main()

{

    

    Point *pt;

    pt = (Point*) malloc(sizeof(Point));

    

    pt->x = 1.2;

    pt->y = 2.1;

    

    printf("The coordinate of pt is = (%f,%f)", pt->x, pt->y);

    

    return 1;

}

 

Result :

The coordinate of pt is = (1.200000,2.100000)

 

 

 

 

creating struct array

 

 

Example 01 >

 

#include <stdio.h>

 

struct Point {

    

    float x;

    float y;

    

};

 

int main()

{

    

    struct Point pt[5];

    int i = 0;

    

    for(i = 0; i < 5; i++){

    

        pt[i].x = i;

        pt[i].y = 10*i;

    

        printf("The coordinate of pt[%d] is = (%f,%f)\n", i, pt[i].x, pt[i].y);

    

    

    }

    

    

    return 1;

}

 

Result :

The coordinate of pt[0] is = (0.000000,0.000000)

The coordinate of pt[1] is = (1.000000,10.000000)

The coordinate of pt[2] is = (2.000000,20.000000)

The coordinate of pt[3] is = (3.000000,30.000000)

The coordinate of pt[4] is = (4.000000,40.000000)

 

 

Example 02 >

 

#include <stdio.h>

 

struct Point {

    

    float x;

    float y;

    

};

 

int main()

{

    

    struct Point *pt;

    int i = 0;

    

    pt = (struct Point*) malloc(5 * sizeof(struct Point));

    

    for(i = 0; i < 5; i++) {

            (pt+i)->x = i;

            (pt+i)->y = 10*i;

    

            printf("The coordinate of (pt+%d) is = (%f,%f)\n", i, (pt+i)->x, (pt+i)->y);

    }

    

    return 1;

}

 

 

Result :

The coordinate of (pt+0) is = (0.000000,0.000000)

The coordinate of (pt+1) is = (1.000000,10.000000)

The coordinate of (pt+2) is = (2.000000,20.000000)

The coordinate of (pt+3) is = (3.000000,30.000000)

The coordinate of (pt+4) is = (4.000000,40.000000)

 

 

Example 03 >

 

#include <stdio.h>

#include <malloc.h>

 

struct Point {

    

    float x;

    float y;

    

};

 

int main()

{

    

    struct Point *pt;

    int i = 0;

    

    pt = (struct Point*) malloc(5 * sizeof(struct Point));

    

    for(i = 0; i < 5; i++) {

            pt[i].x = i;

            pt[i].y = 10*i;

    

            printf("The coordinate of (pt+%d) is = (%f,%f)\n", i, pt[i].x, pt[i].y);

    }

    

    return 1;

}

 

 

Result :

The coordinate of (pt+0) is = (0.000000,0.000000)

The coordinate of (pt+1) is = (1.000000,10.000000)

The coordinate of (pt+2) is = (2.000000,20.000000)

The coordinate of (pt+3) is = (3.000000,30.000000)

The coordinate of (pt+4) is = (4.000000,40.000000)

 

 

passing a struct into a function

 

Personally this is the most common case/reason that I am using a struct. When I need to pass multiple number of variables into a function and when those variables are related to a certain purpose/use case, I tend to pack those variables into a stuct and pass the struct into the function. By this way, it is easy to define / use the function. Sometimes there are cases where I don't know exactly how many of the individual varaibles to pass it to the function at the time of defining the function. If I define a struct and pass it to the function, I don't need to change the prototype of the function even if the number of variable would change. I only add or remove the member function of the struct as I want.

 

Example 01 >

 

#include <stdio.h>

 

struct POINT{

    

    float x;

    float y;

    

} ;

 

void printPoint(struct POINT pt)

{

    printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);

}

 

int main()

{

    

    struct POINT pt1;

    

    pt1.x = 1.2;

    pt1.y = 2.1;

    

    printPoint(pt1);

    

    return 1;

}

 

Result :

The coordinate of pt is = (1.200000,2.100000)

 

 

Example 02 >

 

#include <stdio.h>

 

struct POINT{

    

    float x;

    float y;

    

} ;

 

void printPoint(struct POINT pt)

{

    printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);

}

 

int main()

{

    

    struct POINT pt1;

    

    pt1.x = 1.2;

    pt1.y = 2.1;

    

    printPoint(pt1);

    

    return 1;

}

 

Result :

The coordinate of pt is = (1.200000,2.100000)

 

 

Example 03 >

 

#include <stdio.h>

#include <malloc.h>

 

typedef struct POINT{

    

    float x;

    float y;

    

} Point ;

 

void printPoint(Point *pt)

{

    printf("The coordinate of pt is = (%f,%f)\n", pt->x, pt->y);

}

 

int main()

{

    

    Point pt1;

    Point *pt2;

    

    pt2 = (Point*) malloc(sizeof(Point));

    

    pt1.x = 1.2;

    pt1.y = 2.1;

    

    pt2->x = 12;

    pt2->y = 21;

    

    printPoint(&pt1);

 

    printPoint(pt2);

    

    return 1;

}

 

Result :

The coordinate of pt is = (1.200000,2.100000)

The coordinate of pt is = (12.000000,21.000000)

 

 

 

returning a struct from a function  

 

This is another common case where I use a struct. In C, basically you can return only one value from a function. When I want to return multiple values from a function, I define a struct with multiple member values in it and return that the struct from the function.

 

 

Example 01 >

 

#include <stdio.h>

 

typedef struct POINT{

    

    float x;

    float y;

    

} Point ;

 

Point filloutPoint(float x, float y)

{

    Point pt;

    

    pt.x = x;

    pt.y = y;

    

    return pt;

}

 

int main()

{

    

    Point rPt;

    

    rPt = filloutPoint(1.2,2.4);

    

    printf("The coordinate of rPt is = (%f,%f)", rPt.x, rPt.y);

    

    return 1;

}

 

 

Result :

The coordinate of rPt is = (1.200000,2.400000)

 

 

Example 02 >

 

#include <stdio.h>

#include <malloc.h>

 

typedef struct POINT{

    

    float x;

    float y;

    

} Point ;

 

Point * filloutPoint(float x, float y)

{

    

    Point *pt;

    pt = (Point*) malloc(sizeof(Point));

    

    pt->x = x;

    pt->y = y;

        

    return pt;

}

 

int main()

{

    

    Point *rPt;

    

    rPt = filloutPoint(1.2,2.4);

    

    printf("The coordinate of rPt is = (%f,%f)", rPt->x, rPt->y);

    

    return 1;

}

 

 

Result :

The coordinate of rPt is = (1.200000,2.400000)

 

 

Example 03 >

 

#include <stdio.h>

#include <malloc.h>

 

typedef struct POINT{

    

    float x;

    float y;

    

} Point ;

 

Point * filloutPoint(float *x, float *y,int n)

{

    

    Point *pt;

    pt = (Point*) malloc(n * sizeof(Point));

    int i = 0;

    

    for( i = 0; i < n; i++){

        

        (pt+i)->x = x[i];

        (pt+i)->y = y[i];

    

    }

        

    return pt;

}

 

int main()

{

    

    Point *rPt;

    float x[5] = {1.0, 2.0, 3.0, 4.0, 5.0};

    float y[5] = {10.0, 20.0, 30.0, 40.0, 50.0};

    int n = 5;

    int i = 0;

    

    rPt = filloutPoint(x,y,n);

    

    for( i = 0; i < n; i++){

        

        printf("The coordinate of rPt[%d] is = (%f,%f)\n", i, (rPt+i)->x, (rPt+i)->y);

    

    }

    

    

    return 1;

}

 

Result :

The coordinate of rPt[0] is = (1.000000,10.000000)

The coordinate of rPt[1] is = (2.000000,20.000000)

The coordinate of rPt[2] is = (3.000000,30.000000)

The coordinate of rPt[3] is = (4.000000,40.000000)

The coordinate of rPt[4] is = (5.000000,50.000000)