Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Can I Use the Struct Pointer as an Array of Structure Variables

sameergksameergk Member Posts: 2
[b]Is it Legal to do this?[/b]
/* Structure Declaratrion */
[green][b][italic]
typedef struct Point
{
int x;
int y;
}Pt;
[/green]
[/italic][/b]
[b][black]

/* Global variables */
Point *my_pt
int nPoints
[/black][/b]
[b][blue]

main()
{
/* Allocate for the My_pt */
my_pt = malloc(sizeof(Pt) * (nPoints));

my_pt[1]->.x = my_pt[1].y = 100;

/* ........
..........
...........*/
/* Free the Memory */
free(my_pt);
}
[/blue][/b]

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : [b]Is it Legal to do this?[/b]
    : /* Structure Declaratrion */
    : [green][b][italic]
    : typedef struct Point
    : {
    : int x;
    : int y;
    : }Pt;
    : [/green]
    : [/italic][/b]
    : [b][black]
    :
    : /* Global variables */
    : Point *my_pt
    : int nPoints
    : [/black][/b]
    : [b][blue]
    :
    : main()
    : {
    : /* Allocate for the My_pt */
    : my_pt = malloc(sizeof(Pt) * (nPoints));
    :
    : my_pt[1]->.x = my_pt[1].y = 100;
    :
    : /* ........
    : ..........
    : ...........*/
    : /* Free the Memory */
    : free(my_pt);
    : }
    : [/blue][/b]
    :
    :
    [blue]That is exactly how it should be done!

    Say, you need this:
    [code]
    Pt array [20];
    [/code]
    But, what if you do not know how much you need there? 20? Less ? More? The way is to do exactly how you did it.[/blue]

  • sameergksameergk Member Posts: 2
    : : [b]Is it Legal to do this?[/b]
    : : /* Structure Declaratrion */
    : : [green][b][italic]
    : : typedef struct Point
    : : {
    : : int x;
    : : int y;
    : : }Pt;
    : : [/green]
    : : [/italic][/b]
    : : [b][black]
    : :
    : : /* Global variables */
    : : Point *my_pt
    : : int nPoints
    : : [/black][/b]
    : : [b][blue]
    : :
    : : main()
    : : {
    : : /* Allocate for the My_pt */
    : : my_pt = malloc(sizeof(Pt) * (nPoints));
    : :
    : : my_pt[1]->.x = my_pt[1].y = 100;
    : :
    : : /* ........
    : : ..........
    : : ...........*/
    : : /* Free the Memory */
    : : free(my_pt);
    : : }
    : : [/blue][/b]
    : :
    : :
    : [blue]That is exactly how it should be done!
    :
    : Say, you need this:
    : [code]
    : Pt array [20];
    : [/code]
    : But, what if you do not know how much you need there? 20? Less ? More? The way is to do exactly how you did it.[/blue]
    :
    :
    [b][blue]Thanks for your reply.[/blue]
    You are Correct, since i dont know the exact number, till the run time.[/b]
Sign In or Register to comment.