Вы находитесь на странице: 1из 10

STRUCTURES-classes and

STRUCTURE VARIABLES - objects

#include <stdio.h>

#include <string.h>

int main( )

struct gospel

int num ;

int x[9];

char mess1[50] ;

char mess2[50] ;

}m;

m.num = 1 ;

for(int i = 0; i < 9; i++)

m.x[i] = i;

strcpy ( m.mess1, "If all that you have is hammer" ) ;

strcpy ( m.mess2, "Everything looks like a nail" ) ;

printf ( "\n%u %u %u %u", &m.num, m.x , m.mess1, m.mess2 ) ;

return 0;

OUTPUT: 6356608 6356612 6356648 6356698


The declaration of the structure reserves as much memory as required by the datatype. For int, it reserves four
bytes, for char one byte per character and so on. Thus, the structure sequentially assigns memory to each
structure on the basis of the datatype of the elements declared inside It.

struct gospel

int num ;

char mess1[50] ;

char mess2[50] ;

} m1 = { 2, "If you are driven by success",

"make sure that it is a quality drive"

};

int main( )

struct gospel m2, m3 ;

m2 = m1 ;

m3 = m2 ;

printf ( "\n%d %s %s", m1.num, m2.mess1, m3.mess2 ) ;

return 0;

OUTPUT : 2 If you are driven by success make sure that it is a quality drive

It is possible to assign one structure to another directly, all the elements get copied .

It is possible to assign the values to the structure elements in the same way as arrays.

The memory locations in which they are stored are not the same.

ERRORS::

1.

main( )
{
struct employee
{
char name[25] ;
int age ;
float bs ;
};
struct employee e ;
strcpy ( e.name, "Hacker" ) ;
age = 25 ;
printf ( "\n%s %d", e.name, age ) ;

While referring to elements of the structure, use structurename,elementname else it will return an error saying it
is the first undeclared use in the function.
e.age should be used.

2.
main( )
{
struct
{
char name[25] ;
char language[10] ;
};
struct employee e = { "Hacker", "C" } ;
printf ( "\n%s %d", e.name, e.language ) ;

return 0;

The name of the structure should be defined (like a class).

struct virus
{
char signature[25] ;
char status[20] ;
int size ;
}v[2] = {
"Yankee Doodle", "Deadly", 1813,
"Dark Avenger", "Killer", 1795
} ,a = {"name" , "ankita" , 34};
main()
{
int i ;
for ( i = 0 ; i <=1 ; i++ )
printf ( "\n%s %s",v[i].signature, v[i].status);
printf ( "\n%s %s",a.signature,a.status);

OUTPUT :
Yankee Doodle Deadly
Dark Avenger Killer
name Ankita

You can declare the structure variables by a comma-separated list of variables, before the semi-colon.

printf ( "\n%s %s",a.signature,a.status , a.size);.


This statement will not return an error , It is fine to include strings and not have sufficient number of input strings .

main()
{

int a, b,c,d,e;

scanf("%d%d%d" , &a, &b);


scanf("%d%d" , &c , &d, &e);
printf("%d %d %d %d %d " , a,b,c,d,e);

OUTPUT :
1
2
3
4
5
1 2 4 5 4200814

It skips the third input entirely and thus the value 4 will be assigned to the correct variable c, as e is unitialised it
will print garbage value.

BOTH THE UNMATCHED STATEMENTS WILL NOT RETURN AN ERROR.


4.
struct s
{
char ch ;
int i ;
float a ;
};
main( )
{
struct s var = { 'C', 100, 12.55 } ;
f ( var ) ;
g ( &var ) ;
}
f ( struct s v )
{
printf ( "\n%c %d %f", v -> ch, v -> i, v -> a ) ;
}

g ( struct s *v )
{
printf ( "\n%c %d %f", v.ch, v.i, v.a ) ;
}
BOTH THE FUNCTIONS WILL RETURN ERRORS.
If we want to call via a structure variable, then we must use . operator, and if we wish to call using a pointer, then
we must use -> operator.

Correct code:
struct s
{
char ch ;
int i ;
float a ;
};
main( )
{
struct s var = { 'C', 100, 12.55 } ;
f ( var ) ;
g ( &var ) ;
}
f ( struct s v )
{
printf ( "\n%c %d %f", v.ch, v.i, v.a ) ;
}

g ( struct s *v )
{
printf ( "\n%c %d %f", v->ch, v->i, v->a ) ;
}

OUTPUT :
C 100 12.550000
C 100 12.550000

struct s
{
int i ;
struct s *p ;//to declare the pointer to a structuress
};
main( )
{
struct s var1, var2 ;
var1.i = 100 ;
var2.i = 200 ;
var1.p = &var2 ;
var2.p = &var1 ;
printf ( "\n%d %d", var1.p -> i, var2.p -> i ) ;
}

THIS WILL NOT RETURN AN ERROR


200 100

THE NAME OF THE VARIABLES OUTSIDE THE STRUCTURE AND THAT OF THE MEMBERS MAY BE THE
SAME . THEY ARE REGARDED DIFFERENTLY.
TWO STRUCTURE VARIABLES MAY ALSO BE COMPARED FOR EQUALITY. THUS , CONSIDER THEM AS
PRIMITIVE DATATYPES. CALL BY VALUE IS IMPLEMENTED WHEN STRUCTURE VARIABLES ARE
PASSED.
The typedef keyword allows the programmer to create a
new data type name for an existing data type. No new data
type is produced but an alternate name is given to a known
data type. The general form of the declaration statement
using the typedef keyword is given as follows.
typedef <existing data type> <new data type ,….>;
The typedef statement does not occupy storage; it
simply defines a new type. typedef statements can be
placed anywhere in a C program as long as they come
prior to their first use in the code.
The following examples show the use of typedef.
typedef int id_number;
typedef float weight;
typedef char lower_case;
In the preceding example, id_number is the new data
type name given to data type int, while weight is the new
data type name given to data type fl oat and lower_case is
the new data type name given to data type char. Therefore,
the following statements
id_number vinay, komal, jaspal;
weight apples, pears, mangoes;
lower_case a,b,c;
mean that vinay, komal, and jaspal are variable names
that are declared to hold int data type. The new data type,
id_number, suggests that the data content of the variable
names vinay, komal, and jaspal are integers and that it
gives their identification number. The two other examples
shown also carry similar meanings. Therefore, by the
typedef keyword mechanism, the suggested use of the
type names can be understood easily. This is one of the
benefits of using the typedef keyword. Moreover, typedef
makes the code more portable.

#include <stdio.h>
#include <string.h>
//it is not necessary to name the structure in this case as we would not use it anywhere; however if we do we can
use it to declare structure variables in the normal way
typedef struct
{
int id;
char name[20];
float percentage;
} status;

int main()
{
status record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);

status e2;
e2.id=1;
strcpy(e2.name, "Raju");
e2.percentage = 86.5;
printf(" Id is: %d \n", e2.id);
printf(" Name is: %s \n", e2.name);
printf(" Percentage is: %f \n", e2.percentage);
return 0;
}

IT CAN BE USED TO CREATE ANY NUMBER OF STRUCTURE VARIABLES AS REQUIRED , AS WE USED


typedef TO REGARD IT AS A DATATYPE.

OUTPUT:
Id is: 1
Name is: Raju
Percentage is: 86.500000
Id is: 1
Name is: Raju
Percentage is: 86.500000

struct tag {
member 1;
member 2;
:
member m;
} var_1, var_2,…, var_n;
• In this form, tag is optional.

Estimating the Size of a Structure


• The “sizeof” for a struct variable is not always
equal to the sum of the “sizeof” of each individual
member
– Padding (extra space) can be added by the compiler to
avoid alignment issues
– Padding is only added when a structure member is
followed by a member with a larger size, or at the end
of the structure
• Exact convention may vary from one compiler to
Another

PADDING
#include <stdio.h>

int main()

struct A {

// sizeof(int) = 4

int x;

// Padding of 4 bytes

// sizeof(double) = 8

double z;

// sizeof(short int) = 2

short int y;

// Padding of 6 bytes

short int n;//24

};

printf("Size of struct: %ld", sizeof(struct A));

return 0;

CONSIDER BLOCKS OF 8 BYTES AND KEEP ON FILLING THEM UP.

IT SHOULD BE A RECTANGLE AT THE END.

Вам также может понравиться