Variation of Pointers

Similar documents
Technical Questions. Q 1) What are the key features in C programming language?

Dynamic Memory Allocation

PROGRAMMAZIONE I A.A. 2017/2018

Pointers, Dynamic Data, and Reference Types

Programming in C - Part 2

[0569] p 0318 garbage

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Homework #3 CS2255 Fall 2012

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

Language comparison. C has pointers. Java has references. C++ has pointers and references

Lab 3. Pointers Programming Lab (Using C) XU Silei

Procedural programming with C

CSCI 171 Chapter Outlines

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Memory and C++ Pointers

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

2.2 Pointers. Department of CSE

DECLARAING AND INITIALIZING POINTERS

Computer Programming Unit 3

Example: Pointer Basics

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

by Pearson Education, Inc. All Rights Reserved.

Class Information ANNOUCEMENTS

Introduction to C. Systems Programming Concepts

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

Pointers. 10/5/07 Pointers 1

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Procedural Programming

IV Unit Second Part STRUCTURES

Dynamic memory allocation (malloc)

Arrays and Pointers (part 1)

First of all, it is a variable, just like other variables you studied

Pointers. Memory. void foo() { }//return

Algorithms & Data Structures

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

Arrays and Pointers (part 1)

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Problem Solving and 'C' Programming

Laboratory 10 POINTERS I. FUNDAMENTALS

Pointers! Arizona State University 1

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Intermediate Programming, Spring 2017*

Structures and Pointers

Tutorial 10 Pointers in C. Shuyue Hu

High Performance Programming Programming in C part 1

CSC 211 Intermediate Programming. Arrays & Pointers

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

EM108 Software Development for Engineers

Programming for Engineers Pointers

More about BOOLEAN issues

What is an algorithm?

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Content. In this chapter, you will learn:

Procedural Programming & Fundamentals of Programming

POINTER & REFERENCE VARIABLES

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

edunepal_info

Fundamental of Programming (C)

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Lecture 2: C Programm

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Computers Programming Course 5. Iulian Năstac

OBJECTIVE QUESTIONS: Choose the correct alternative:

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

C-Refresher: Session 06 Pointers and Arrays

Practice Sheet #07 with Solutions

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Computers Programming Course 6. Iulian Năstac

School of Science and Technology

C Pointers. 7.2 Pointer Variable Definitions and Initialization

M1-R4: Programing and Problem Solving using C (JAN 2019)

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

int marks[10]; // fixed size and fixed address No change in Memory address.

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Quick review pointer basics (KR ch )

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

MYcsvtu Notes LECTURE 34. POINTERS

Engineering program development 6. Edited by Péter Vass

C: How to Program. Week /Apr/23

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

In Java we have the keyword null, which is the value of an uninitialized reference type

MODULE V: POINTERS & PREPROCESSORS

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Transcription:

Variation of Pointers A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is type *var-name; What is a pointer in programming? In computer science, a pointer is a programming language object, whose value refers to (or points to ) another value stored elsewhere in the computer memory using its memory address. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type. The unary or monadic operator & gives the address of a variable. The indirection or dereference operator * gives the contents of an object pointed to by a pointer. The general form of a pointer variable declaration is type *var-name; type is the pointer s base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Take a look at some of the valid pointer declarations int *ip; /* pointer to an integer */ double *dp; /* pointer to a double */ float *fp; /* pointer to a float */ char *ch /* pointer to a character */ What is a pointer in C programming? Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type like char,int, float, double or user defined data type like function, pointer etc. or derived data type like array, structure, union, enum. Examples: int *ptr; int (*ptr)(); int (*ptr)[2];

Types of Pointers in C Programming NULL Pointer Dangling Pointer Generic Pointers Wild Pointer Complex Pointers Near Pointer Far Pointer Huge Pointers List of Pointers In C Programming 1. Null Pointer NULL Pointer is a pointer which is pointing to nothing. NULL pointer points the base address of segment. In case, if you don t have address to be assigned to pointer then you can simply use NULL Pointer which is initialized with NULL value is considered as NULL pointer. NULL is macro constant defined in following header files stdio.h alloc.h mem.h stddef.h stdlib.h Defining NULL Value #define NULL 0 Visual Representation

Below are some of the variable representations of NULL pointer. float *ptr = (float *)0; char *ptr = (char *)0; double *ptr = (double *)0; char *ptr = '\0'; int *ptr = NULL; Example of NULL Pointer #include <stdio.h> int *ptr = NULL; printf("the value of ptr is %u",ptr); Output : The value of ptr is 0 2. Dangling Pointer Dangling pointers arise when an object is deleted or de-allocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. In short pointer pointing to non-existing memory location is called dangling pointer. Examples of Dangling Pointer There are different ways where Pointer acts as dangling pointer. Way 1 : Using free or de-allocating memory #include<stdlib.h> char *ptr = malloc(constant_value);......... free (ptr); /* ptr now becomes a dangling pointer */ We have declared the character pointer in the first step. After execution of some statements we have de-allocated memory which is allocated previously for the pointer.

As soon as memory is de-allocated for pointer, pointer becomes dangling pointer Way 2 : Out of Scope #include<stdlib.h> void main() char *ptr = NULL;...... char ch; ptr = &ch;... /* dp is now a dangling pointer */ Character Pointer is Declared in the first Step. Pointer Variable ptr is pointing to Character Variable ch declared in the inner block. As character variable is non-visible in Outer Block, then Pointer is Still Pointing to Same Invalid memory location in Outer block, then Pointer becomes Dangling 3. Generic Pointers When a variable is declared as being a pointer to type void it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first. Hence the term Generic pointer. This is very useful when you want a pointer to point to data of different types at different times. Example of Generic Pointer int main() int i; char c; void *the_data; i = 6; c = 'a'; the_data = &i; printf("the_data points to the integer value %d\n", *(int*) the_data); the_data = &c;

printf("the_data now points to the character %c\n", *(char*) the_data); 4. Wild Pointer A Pointer in C that has not been initialized till its first use is known as Wild pointer. A wild pointer points to some random memory location. Example of Wild Pointer int *ptr; /* Ptr is a wild pointer, as it is not initialized Yet */ printf("%d", *ptr); How to do avoid wild pointers? We can initialize a pointer at the point of declaration wither by the address of some object/variable or by NULL; int val = 5; int *ptr = &val; /* Initializing pointer */ /* Ptr is not a wild pointer, it is pointing to the address of variable val */ printf("%d", *ptr); 5. Complex Pointers Precedence : Operator precedence describes the order in which C reads expressions Associativity : Order operators of equal precedence in an expression are applied We need to assign the priority to the pointer declaration considering precedence and associative according to following table.

Where (): This operator behaves as bracket operator or function operator. []: This operator behaves as array subscription operator. *: This operator behaves as pointer operator not as multiplication operator. Identifier: It is not an operator but it is name of pointer variable. You will always find the first priority will be assigned to the name of pointer. Data type: It is also not an operator. Data types also includes modifier (like signed int, long double etc.) 6. Near Pointer The pointer which can points only 64KB data segment or segment number 8 is known as near pointer. That is near pointer cannot access beyond the data segment like graphics video memory, text video memory etc. Size of near pointer is two byte. With the help of keyword near, we can make any pointer as near pointer. Example of Wild Pointer #include<stdio.h> int x=25; int near* ptr; ptr=&x; printf( %d,sizeof ptr);

Output: 2 7. Far Pointer The pointer which can point or access whole the residence memory of RAM i.e. which can access all 16 segments is known as far pointer. Size of far pointer is 4 byte or 32 bit. Example of Far Pointer #include<stdio.h> int x=10; int far *ptr; ptr=&x; printf("%d",sizeof ptr); Output : 4 8. Huge Pointer The pointer which can point or access whole the residence memory of RAM i.e. which can access all 16 segments is known as huge pointer. Size of far pointer is 4 byte or 32 bit. Example of Huge Pointer #include<stdio.h> char huge * far *p; printf("%d %d %d",sizeof(p),sizeof(*p),sizeof(**p)); Output : 4 4 1 Explanation: p is huge pointer, *p is far pointer and **p is char type data variable. Advantages of Pointers In C Programming We can dynamically allocate or deallocate space in memory at run time by using pointers. Using pointers we can return multiple values from a function. We can pass arrays to a function as call by Reference.

Pointers are used to efficiently access array elements, as array elements are stored in adjacent memory locations. If we have a pointer pointing to a particular element of array, then we can get the address of next element by simply incrementing the pointer. Pointers in C are used to efficiently implement dynamic Data Structures like Queues, Stacks, Linked Lists, and Trees etc. The use of pointers results into faster execution of program.