C - struct                                                                         Home : www.sharetechnote.com

 

 

 

 

struct

 

 

 

 

defining and calling a struct =============================================

 

Example 01 >

 

#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 >

 

#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 >

 

#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 >

 

#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 >

 

#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 >

 

#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   =============================================

 

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   =============================================

 

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)