16.2 – More about struct

by subbu on September 14, 2014

In previous session we have discoursed the need of user defined type, how to define a user defined type using struct and how to instantiate (creating a variable) the struct type.
Before going further, let us discourse some more issues related to the struct

Unanimous struct

A struct without a name is called unanimous struct. You may be wonder how a data type can be defined without a name? Yes, it is possible but, must be instantiated while declaration because the type has no name, can’t be instantiated afterwards.

#include<stdio.h>
struct
{
 int x;
 int y;
}p;
int main()
{
 p.x=10;
 p.y=20;
 printf("x=%d\ny=%d",p.x,p.y);
 return 0;
}

Output
x=10
y=20

struct variable p

Here “p” is the variable belongs to a struct without a name but, has members “x” and “y”

Members of struct type:

Any variable belongs to primary type, an array and another struct can be defined as members but a function can’t be defined as a member

Array as struct member:

#include<stdio.h>
struct data
{
 int x;
 float y;
 int z[2];
};
int main()
{
 struct data a;
 a.x=10;
 a.y=12.25f;
 a.z[0]=12;
 a.z[1]=24;
 printf("x=%d",a.x);
 printf("\ny=%f",a.y);
 printf("\nArray elements %d\t%d",a.z[0],a.z[1]);
 return 0;
}

Output
x=10
y=12.250000
Array elements 12   24

Array as a struct member

In the above example an array “z” is declared as a struct member. It is allocated in every variable belongs to struct data.

Struct variable as struct member:

#include<stdio.h>
struct data
{
 int x;
  struct
  {
   int y;
   int z;
  }p;
};
int main()
{
 struct data a;
 a.x=10;
 a.p.y=20;
 a.p.z=30;
 printf("x=%d\ny=%d\nz=%d",a.x,a.p.y,a.p.z);
 return 0;
}

Output:
x=10
y=20
z=30

In the above example a struct variable “p” is defined as a member to “struct data”. A variable belongs to “struct data” would have a struct variable “p”

The above struct can be also defined as

struct point
{
 int y;
 int z;
};
struct data
{
 int x;
 struct point p;
};

Here “point” is defined as a separate type and its variable “p” is declared as a member in another struct “data”

The advantage with this is we can instantiate “struct point” separately

Assigning a struct variable:

In case of arrays, an array can’t be assigned to another array directly by using assigning operator

int x[]={10,20,30,40,25,42};
int y[6];
y=x;

In the code “y=x;” gives compiler error because an array is being assigned to another array directly using assigning operator.
The only way to copy an array onto another array is copying element by element using a loop like

for(i=0;i<6;i++)
  y[i]=x[i];

But incase of struct, a struct variable can be directly assigned to another struct variable of same type.

#include<stdio.h>
struct point
{
 int x;
 int y;
};
int main()
{
 struct point a,b;
 a.x=10;
 a.y=20;
 b=a;
 printf("x=%d\ny=%d",b.x,b.y);
 return 0;
}

Output:
x=10
y=20

Copying a struct variable

A struct variable “a” is directly assigned to “b” rather member by member

A struct can be defined within a function

User defined type struct can be defined within a function but its scope would be limited to the same function that is, struct type defined in one function can’t be used in another function.

#include<stdio.h>
int main()
{
  struct point
  {
    int x;
    int y;
  };
  struct point a;
  a.x=45;
  a.y=30;
  printf("x=%d\ny=%d",a.x,a.y);
  return 0;
}

Output
x=45
y=30

Here the struct point is defined within the function main(). It can be only instantiated within the main().

Previous post:

Next post: