Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Pointer & Structure

This document discusses pointers and structures in C. It defines a struct called student with member variables sapid and name. It declares instances s1 and s2, where s2 is a pointer. It shows how to access members using s1 and the pointer ptr, which points to s1. It then discusses dynamic memory allocation using malloc() and calloc(), explaining that malloc() allocates uninitialized memory while calloc() allocates initialized memory with all bits set to zero. It provides examples of the syntax for each function.

Uploaded by

Shado H
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Pointer & Structure

This document discusses pointers and structures in C. It defines a struct called student with member variables sapid and name. It declares instances s1 and s2, where s2 is a pointer. It shows how to access members using s1 and the pointer ptr, which points to s1. It then discusses dynamic memory allocation using malloc() and calloc(), explaining that malloc() allocates uninitialized memory while calloc() allocates initialized memory with all bits set to zero. It provides examples of the syntax for each function.

Uploaded by

Shado H
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Pointers in C

POINTER & STRUCTURE

28‐Jun‐20 ARJ 1
Structure and Pointer
Consider the following declaration…
struct student
{
int sapid;
char name[20];
}s1, *s2;

struct student *ptr;


ptr = &s1;

28‐Jun‐20 ARJ 3
Accessing members
s1.sapid; s1.sapid s1.name
10 raj
s1.name; 1001 1005
ptr
ptrsapid; 1001

ptrname; 5000

C:\TurboC++\Disk\TurboC3\BIN\PTR14.C
How one can access members using s2?

28‐Jun‐20 ARJ 4
Dynamic Memory Allocation
A memory from heap is allocated.
Two standard library functions…
malloc();
calloc();
Memory is allocated on fly i.e. at run time….

28‐Jun‐20 ARJ 5
Syntax
malloc() calloc()
• Takes one argument. • Takes two arguments.
• (datatype *) malloc(sizeof(datatype)); • (datatype *) calloc(no-of-elements,
sizeof(datatype));
• Does not initialize the allocated • Initializes all allocated space bits with
memory. zero.
• Eg. (int *) malloc(sizeof(int)); • Eg. (char*)calloc(10, sizeof(char));

28‐Jun‐20 ARJ 6
Program 15
C:\TurboC++\Disk\TurboC3\BIN\ptr15.cpp

28‐Jun‐20 ARJ 7

You might also like