declaration Structure tagname left { right.$$ Datatype member1; Datatype member2; Datatype membern; $$left. right };
Here, struct - kewords Tagname - specifies name of structure Member1, member2 - - specifies the data items that make up structure. Example : Struct book left { right.$$ int pages; char author [30]; float price; $$left. right };
Structure variables
There are 3 ways of declaring structure variables
struct book
left { right.$$ int pages; char author[30]; float price; $$left. right }b;
2. struct left { right.
int pages; char author[30]; float price; Note : Tagname can be ignored if the variable is declared of the time of defining structure left. right }b;
3. struct book left { right.$$ int pages; char author[30]; float price; $$left. right };
Initialization and accessing of structures
The link between a member and a structure variable is established using member operator (or) dot operator
Initialization can be done in the following ways
struct book
left { right.$$ int pages; char author[30]; float price; $$left. right }b
=left { right.$$100, “balu”, 325.75$$left. right }
; 2. struct book left { right.$$ int pages; char author[30]; float price; $$left. right }
struct b =left { right.$$100, “balu”, 325.75$$left. right }
; 3. using member operator left { right.$$ int pages; char author[30]; float price; $$left. right };
Struct book b; b. pages = 100 strcpy (b.author, “balu”); b.price = 325.75; 4 . using scanf ( ) struct book left { right.$$ int pages; char author[30]; float price; $$left. right };
struct book b; scanf (“%d”, &b
.pages); scanf (“%s
”, b.author); scanf (“%f”, &b
. price); main ( ) left { right.$$ struct book b clrscr ( ); printf ( “enter no of pages, author, price of book”); scanf (“$$%d%s%f, &b$$.pages, b.author, $$&b$$.price); printf(“ Details of book are”); printf(“pages =$$%d$$, author =$$ %s$$, price = $$%f$$, b.pages, b.author, b.price); getch(); $$left. right }
Operations on Structures
There is a relatively small number of operations which C directly supports on structures. As we've seen, we can define structures, declare variables of structure type, and select the members of structures. We can also assign entire structures:
the expression c1 = c2
would assign all of c2 to c1 (both the real and imaginary parts, assuming the preceding declarations). We can also pass structures as arguments to functions, and declare and define functions which return structures. But to do anything else, we typically have to write our own code (often as functions).
For example, we could write a function to add two complex numbers: struct complex cpx_add(struct complex c1, struct complex c2) { struct complex sum; sum.real = c1.real + c2.real; sum.imag = c1.imag + c2.imag; return sum; } We could then say things like c1 = cpx_add(c2, c3) (Two footnotes: Typically, you do need a temporary variable like sum in a function like cpx_add above that returns a structure.) One more thing you can do with a structure is initialize a structure variable while declaring it. As for array initializations, the initializer consists of a comma-separated list of values enclosed in braces {}: struct complex c1 = {1, 2}; struct complex c2 = {3, 4}; Of course, the type of each initializer in the list must be compatible with the type of the corresponding structure member.
Uses of structures in C:
C Structures can be used to store huge data. Structures act as a database.
C Structures can be used to send data to the printer.
C Structures can interact with keyboard and mouse to store the data.
C Structures can be used in drawing and floppy formatting.
C Structures can be used to clear output screen contents.
C Structures can be used to check computer’s memory size etc.
Please Login For Further Details...
Q1 : How to declaring Structure Variable in C?
In C we can group some of the user defined or primitive data types together and form another compact way of storing complicated information is called as Structure.
Syntax:
struct tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};
Q2 : What are the Important Points Regarding Structure in C Programming ?
1. Struct keyword is used to declare structure.
2. Members of structure are enclosed within opening and closing braces.
3. Declaration of Structure reserves no space.
4. It is nothing but the “ Template / Map / Shape ” of the structure .
5. Memory is created , very first time when the variable is created / Instance is created.
Q3 : What is the difference between declaration and definition in C?
A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier.
A definition actually instantiates/implements this identifier. It's what the linker needs in order to link references to those entities.
0 Doubts's