0% found this document useful (0 votes)
8 views6 pages

Unit Iv

This document provides an overview of pointers, structures, and unions in C programming. It explains how to declare, initialize, and access pointers and structures, along with examples of their usage. Additionally, it highlights the differences between structures and unions, particularly in terms of memory allocation and data storage.

Uploaded by

jayanthi.alpula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Unit Iv

This document provides an overview of pointers, structures, and unions in C programming. It explains how to declare, initialize, and access pointers and structures, along with examples of their usage. Additionally, it highlights the differences between structures and unions, particularly in terms of memory allocation and data storage.

Uploaded by

jayanthi.alpula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT - IV

Pointer
The pointer is a variable which stores the address of another variable. This variable can be of type int,
char, array, function, or any other pointer. pointer is the derived data type.
 The & Operator − It is also known as the "Address-of operator". It is used for Referencing which
means taking the address of an existing variable (using &) to set a pointer variable.
 The * Operator − It is also known as the "dereference operator". Dereferencing a pointer is
carried out using the * operator to get the value from the memory address that is pointed by the
pointer.

Pointer Declaration
To declare a pointer, use the dereferencing operator (*) followed by the data type.
Syntax : Data_type *var-name;

EX: int *ip; /* pointer to an integer */


double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
Pointer Initialization
After declaring a pointer variable, you need to initialize it with the address of another variable using
the address of (&) operator. This process is known as referencing a pointer.

Syntax: pointer_variable = &variable;

Example: int x = 10;


int *ptr = &x;

Example Programme :
#include <stdio.h>
int main()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */

ip = &var; /* store address of var in pointer variable*/

printf("Address of var variable: %u\n", &var);


printf("Address stored in ip variable: %p\n", ip);
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
Output
Address of var variable: 0x7ffea76fc63c
Address stored in ip variable: 0x7ffea76fc63c
Value of *ip variable: 20

Array of Pointers :
pointer array is a collection of indexed pointer variables that are references to a memory location. It is
generally used .we want to point at multiple memory locations of a similar data type .We can access the
data by dereferencing the pointer pointing to it.

Syntax:
data_type *var_name [size_of_array];

Example: int *ptr[10];

#include<stdio.h>
#include<conio.h>

Void main()
{
Int x[4]={10,20,30,40};
Int *p[4];
Int i ;
For(i=0;i<=3;i++)
{
P[i]=&x[i];
}
Printf(“Array elements are:”);
For(i=0;i<=3;i++)
{
Printf(“%d”,*p[i]);
}
}

OUTPUT: 10, 20, 30, 40.

Structures
The structure is a user-defined data type that can be used to group items of possibly different
types into a single type. The struct keyword is used to define the structure.
(OR)
Structure in c is a user-defined data type that
enables us to store the collection of different
data types. Each element of a structure is
called a member.
Structure Declaration
Syntax :
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};

Example :

struct book{
char title[50];
char author[50];
double price;
int pages;
} book1;

Structure Variable Declaration

Syntax:
struct structure_name { struct structure_name variable1, variable2, .......;
data_type member_name1; (OR)
data_type member_name1;
....
....
}variable1, varaible2, ...;

Example: struct book book1;

Structure Initialization :

The initialization of a struct variable is done by placing the value of each element inside curly brackets.
Example: struct book book1 = {"Learn C", "Dennis Ritchie", 675.50, 325};

Accessing the Structure Members:


To access the members of a structure, first, you need to declare a structure variable and then use the dot
(.) operator along with the structure variable.
Example:

#include <stdio.h>

struct book{
char title[10];
char author[20];
double price;
int pages;
};

int main(){
struct book book1 = {"Learn C", "Dennis Ritchie", 675.50, 325};
printf("Title: %s \n", book1.title);
printf("Author: %s \n", book1.author);
printf("Price: %lf\n", book1.price);
printf("Pages: %d \n", book1.pages);
return 0;
}

OUTPUT: Title: Learn C


Author: Dennis Ritchie
Price: 675.500000
Pages: 325

Union
Union can be defined as a user-defined data type which is a collection of different variables of different
data types in the same memory location. The union can also be defined as many members, but only one
member can contain a value at a particular point in time.

Syntax :

union [union tag]{


member definition;
member definition;
...
member definition;
} [one or more union variables];

Example:
#include <stdio.h>
#include <string.h>

union Data{
int i;
float f;
};

int main(){
union Data data;

data.i = 10;
data.f = 220.5;
printf("data.i: %d \n", data.i);
printf("data.f: %f \n", data.f);
return 0;
}

OUTPUT: data.i: 1917853763


data.f: 4122360580327794860452759994368.000000

Difference Between Structure and Union


Point of
Structure Union
Difference
Memory Allocates memory only for the largest
Allocates memory for all its members.
Allocation member.
Total Size Sum of sizes of all members. Size of the largest member.
Can store values for all members Can store value for only one member at
Data Storage
simultaneously. a time.
When you want to group different data types When you want to save memory and
Use Case
and access all of them at once. only need one data type at a time.
struct example { union example {
int a; int a;
Example
float b; float b;
} }
Accessing Only the last stored member can be
All members can be accessed at any time.
Members accessed.
Modification Modifying a member doesn’t affect other Modifying one member may overwrite
Impact members. other members.

You might also like