0% found this document useful (0 votes)
22 views

Unit4 Material

u 3 notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Unit4 Material

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

UNIT –IV

POINTERS & USER- DEFINED STRUCTURES & UNION

1)Explain about Structures and Unions.

Structure is a collection of variables of different data types. Structure name or structure tag
should always precede with the keyword "struct". Structure declaration always enclosed
between 2 braces{}(curly braces). The data types declared inside a structure are known as
members of structure.

Syntax:
struct struct_name
{
data type membername1;
data type membername2;
}variable1, variable2;

Example:
struct student
{
char stud_name[30];
int roll_number;
float percentage;
};

The memory is allocated when we create the variable of a particular structure. The size of
memory allocated is equal to the sum of memory required by individual members of that
structure.

Initialization of Structures:
A structure can be initialized by assigning values to the members within structure.
Struct structure_name var = {value1, value2...};
C Program to demonstrate Structures
#include<stdio.h>
struct item
{
int x;
int y;
};
int main( )
{
struct item it;
it.y = 20;
it.x = 12;
printf("size of item is %d \n",sizeof(it));
printf("%d\n", it.x);
printf("%d\n", it.y);
return 0;
}
Output:
size of item is 8
12
20

Unions:
----------
C Union is also like structure, i.e. collection of different data types which are grouped
together. Each element in a union is called member. Union and structure in C are same in
concepts, except allocating memory for their members. Structure allocates storage space for
all its members separately, whereas, union allocates one common storage space for all its
members.

We can access only one member of union at a time. We can’t access all member values at the
same time in union. But, structure can access all member values at the same time.

Synatx:
union name
{
data type membername1;
data type membername2;
};
Example:
unioin student
{
char stud_name[30];
int roll_number;
float percentage;
};

The size of memory allocated is equal to the maximum memory required by an individual
member among all members of that union.

C Program to demonstrate unions:


#include<stdio.h>
union item
{
int x;
int y;
};
int main( )
{
union item it;
it.y = 20;
it.x = 12;
printf("size of item is %d \n",sizeof(it));
printf("%d\n", it.x);
printf("%d\n", it.y);
return 0;
}
Output:
size of item is 4
12
12

Difference between structure and union:


--------------------------------------------------

2)What is a pointer?Explain with example?


A pointer is a memory variable that stores a memory address of another variable. denoted by
an asterisk (*) operator.

Features of pointers:
• Pointers save the memory space.
• Execution time with pointer is faster because data is manipulated with the address, i.e.,
direct access to memory location.
• Pointers are used to allocate memory dynamically.
• The memory is accessed efficiently with the pointers. The pointer assigns the memory space
and also releases it.
• Pointers are used for file handling.

Pointer Declaration:
Pointer variables are declared as follows:
Syntax:
datatype* identifier;

Example: int* a;
float* f;
char* y;

In the first statement ‘x’ is an integer pointer and it tells the compiler that it holds the address
of any integer variable. In the same way, ‘f’ is a float pointer which holds the address of any
float variable and ‘y’ is character pointer which holds the address of any character variable.
• The indirection operator (*) is also called the dereferencing operator. When a pointer is
dereferenced, the value at that address stored by the pointer is retrieved.
• The indirection operator (*) is used in 2 different ways with pointers, declaration and
dereference.

• When a pointer is declared, an asterisk (*) indicates that it is a pointer and not a normal
variable.
• When a pointer is dereferenced, the indirection operator (*) indicates that the value at that
memory location stored in the pointer is to accessed rather than the address itself.
#include<stdio.h>
int main()
{
int a;
int* pa;
a = 10;
pa = &a;
printf(“\n %u”, pa);
printf(“\n %u”, &a);
printf(“\n %d”, a);
printf(“\n %d”, *pa);
printf(“\n %d”, *(&a));
printf(“\n %u”, &pa);
}
Output:
2000
2000
10
10
10
3000

3)Explain about Dynamic memory allocation functions?


C provides 2 options to reserve memory location for an object: static allocation and dynamic
allocation.
Static memory allocation requires that the declaration and definition of memory be fully
specified in the source program.
Dynamic memory allocation uses predefined functions to allocate and release memory for
data while the program is running.
Memory allocation functions:
Four memory management functions are used with dynamic memory. Three of them, malloc,
calloc and realloc are used for memory allocation. The fourth, free(), is used to return
memory when it is no longer needed. All the memory management functions are found in the
standard library file (stdlib.h).
Block Memory allocation (malloc):
The malloc function allocates a block of memory that contains the number of bytes specified
in its parameter. The malloc function declaration is shown below:
Pointer = (data_type*) malloc(size);
Example: ptr = (int*) malloc(20);
Here, in this declaration 20 bytes are allocated to pointer variable ptr of type int and base
address is returned to pointer ptr.
Contiguous Memory allocation (calloc):
The second memory allocation function, calloc, is primarily used for allocating multiple
blocks of memory. It is used to allocate memory for arrays and structures.
The calloc function declaration is shown below:
Pointer = (data_type*) calloc(count, size);
Example: ptr = (int*) calloc(4, 2);
The above declaration allocates four blocks of memory; each block of containing 2 bytes.
The base address is stored in the integer pointer.
Reallocation of memory (realloc):
This function reallocates the main memory. This is needed as and when allocated memory is
different from the required memory. It returns the address of the reallocated block, which can
be differen
t from the original address. If the block cannot be reallocated or the size is 0, realloc() returns
NULL.
The realloc function declaration is shown below:
Pointer = (data_type*) realloc(void* ptr, size);
Example: str = (char*) realloc(str, 30);
In the above declaration, str is a pointer. The realloc() function reallocates the memory
previously allocated by pointer variable str to the new size 30.
Releasing memory (free): When memory allocations allocated by malloc, calloc are no
longer needed, they should be freed using free() function. It is an error to free memory with a
null pointer, a pointer to other than the first element of an allocated block, a pointer that is
different type than the pointer that allocated the memory. It is also an error to refer to the
memory after it has been released.
The free function declaration is shown below:
void free(void* ptr);
Example: free(ptr); In the above declaration, the free() function releases the memory
occupied by the pointer variable ptr.

4. Explain about Pointer Arithmetic?


. The C pointer arithmetic operations are slightly different from the ones that we generally
use for mathematical calculations. These operations are:
1. Increment/Decrement of a Pointer
2. Addition of integer to a pointer
3. Subtraction of integer to a pointer
4.Subtracting two pointers of the same type

#include <stdio.h>

// pointer increment and decrement


//pointers are incremented and decremented by the size of the data type they point to
int main()
{
int a = 22;
int *p = &a;
printf("p = %u\n", p); // p = 6422288
p++;
printf("p++ = %u\n", p); //p++ = 6422292 +4 // 4 bytes
p--;
printf("p-- = %u\n", p); //p-- = 6422288 -4 // restored to original value

float b = 22.22;
float *q = &b;
printf("q = %u\n", q); //q = 6422284
q++;
printf("q++ = %u\n", q); //q++ = 6422288 +4 // 4 bytes
q--;
printf("q-- = %u\n", q); //q-- = 6422284 -4 // restored to original value

char c = 'a'; char *r = &c;

printf("r = %u\n", r); //r = 6422283


r++;
printf("r++ = %u\n", r); //r++ = 6422284 +1 // 1 byte
r--;
printf("r-- = %u\n", r); //r-- = 6422283 -1 // restored to original value

return 0;
} Output

p = 1441900792
p++ = 1441900796
p-- = 1441900792
q = 1441900796
q++ = 1441900800
q-- = 1441900796
r = 1441900791
r++ = 1441900792
r-- = 1441900791

5. Explain Pointer to Pointer?


A pointer to a pointer is a form of multiple indirection, or a chain of pointers. Normally, a
pointer contains the address of a variable. When we define a pointer to a pointer, the first
pointer contains the address of the second pointer, which points to the location that contains
the actual value as shown below.

Each level of pointer indirection requires a separate indirection operator when it is


dereferenced.
Example:
#include<stdio.h>
int main()
{
// Local Declarations
int a;
int* p;
int ** q;
a=2;
p = &a;
q = &p;
printf(“\n Value of a = %d Address of a = %u”,a,&a);
printf(“\n Through *p Value of a = %d Address of a = %u”,*p,p);
printf(“\n Through **q Value of a = %d Address of a = %u”,**q,*q);
}
Output:
Value of a = 2 Address of a =4056
Through *p Value of a = 2 Address of a =4056
Through **q Value of a = 2 Address of a =4056
Explanation:
In the above program variable p is declared as a pointer. The variable q is declared as a
pointer to another pointer. Hence, they are declared as ‘*p’ and ‘**q’ respectively. The
address of variable ‘a’ is assigned to pointer ‘p’. The address of pointer ‘p’ is assigned to ‘q’.
The variable ‘q’ contains the address of pointer variable. Hence ‘q’ is a pointer to pointer.
The program displays the value and address of variable ‘a’ using variable ‘a’ itself and
pointers ’p’ and ‘q’.

6. Array of Pointers:
Array of pointers is nothing but a collection of addresses. Here, we store the addresses of
variables for which we have to declare the array as pointer. This structure is especially
helpful when the number of elements in an array is variable.
Example:
#include<stdio.h>
int main()
{
int a[5] = {0,1,2,3,4}, i;
int* p[5];
for(i = 0; i<5; i++)
p[i] = a + i;
for(i = 0; i<5; i++)
{
printf(“\n\t%d at location”, *(*p+i));
printf(“\n\t%u at location”, *(p+i));
printf(“%u at location”, p+i);
}
}
Output:
0 at location 4036 at location 4046
1 at location 4038 at location 4048
2 at location 4040 at location 4050
3 at location 4042 at location 4052
4 at location 4044 at location 4054
Explanation:
In the above program, the first for loop assigns the addresses of the elements o integer array
to pointer the array. The first printf() statement prints elements, the second displays addresses
of the element and the third displays addresses of the address i.e., address of the pointer.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy