Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C++ Void Pointer and Null Pointer

By Sripriya R | on October 12, 2007 |
C++ Tutorials

In this C++ tutorial, you will learn about two interesting types of pointers; void pointers and Null Pointer. These pointers will be discussed in conjunction with syntax, usage and example.

Pointer to Void

General Syntax:

void* pointer_variable;

Void is used as a keyword.

Referring back to pointer definitions and usage, it is known that the data type the pointer variable defines is the same as the data type the pointer points to. The address placed in a pointer must have the same type as the pointer.

For example:

  1. int i; 
  2. float f; 
  3. int* exf; 
  4. float* test; 
  5. then 
  6. exf=&i;

Is correct because the address of integer variable is stored in an integer pointer.

If a user writes the statement:

exf=&f;

Then this statement produces an error. The address of the float variable is stored in an integer pointer that is incorrect.

Similarly, if the programmer tries to place the address of an integer variable to a float pointer, such as:

test=&i;

The above statement will also show an error.

The Pointer to Void is a special type of pointer that the programmer can use to point to any data type.

Using the above example, the programmer declares pointer to void in this manner:

void* sample;

Using the above example’s definition and assigning the pointer to void to the address of an integer variable is perfectly correct.

sample=&i;

Using the above example to define the pointer to void and assign the pointer to void to the address of a float variable as below is also perfectly correct.

sample=&f;

Pointer to void, or a void pointer, is a special type of pointer that has a great facility of pointing to any data type. There are limitations in the usage of void pointers that are explained below.

The concept of dereferencing using the operator * has been explained in an earlier section of this tutorial. The programmer must note that void pointers cannot be de-referenced in the same manner. Direct dereferencing of void pointer is not permitted. The programmer must change the pointer to void as any other pointer type that points to valid data types such as, int, char, float and then dereference it. This conversion of pointer to some other valid data type is achieved by using the concept of type-casting (refer to type-casting section of this tutorial).

NULL Pointer:

The concept of NULL pointer is different from the above concept of void pointer. NULL pointer is a type of pointer of any data type and generally takes a value as zero. This is, however, not mandatory. This denotes that NULL pointer does not point to any valid memory address.

For example:

int* exforsys;

exforsys=0;

The above statement denotes exforsys as an integer pointer type that does not point to a valid memory address. This shows that exforsys has a NULL pointer value.

The difference between void pointers and NULL pointers:

A Void pointer is a special type of pointer of void and denotes that it can point to any data type. NULL pointers can take any pointer type, but do not point to any valid reference or memory address. It is important to note that a NULL pointer is different from a pointer that is not initialized.

For example, if a programmer uses the program below:

  1.  #include <iostream>
  2. using namespace std;
  3. int *exforsys=NULL;
  4. void main()
  5. {
  6.   *exforsys=100;
  7. }

The output of the above program is NULL POINTER ASSIGNMENT, which will result in:

The above program will result in a runtime error. This means that the pointer variable exforsys is not assigned any valid address and, therefore, attempting to access the address 0 gives the above error message.

« « Service Orientation and Interoperability
C++ Memory Management Operators » »

Author Description

Avatar

Free Training

RSSSubscribe 392 Followers
  • Popular
  • Recent
  • C++ Multidimensional Arrays

    September 4, 2007 - 0 Comment
  • C++ Variables Scope in Functions

    October 17, 2006 - 0 Comment
  • How to Access C++ Class Members

    August 21, 2007 - 0 Comment
  • C++ Memory Management Operators

    October 12, 2007 - 0 Comment
  • C++ Structure

    August 19, 2007 - 0 Comment
  • C++ Inheritance

    August 19, 2007 - 0 Comment
  • C++ Standard Input Output Stream

    August 24, 2007 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ String Representation and Handling

    September 8, 2007 - 0 Comment
  • C++ Static Functions

    February 12, 2007 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Memory Management Operators

    October 12, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Static Functions

    October 1, 2007 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Inline Functions

    September 25, 2007 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment
  • C++ Storage Classes

    September 10, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C++ Dereference Operator
  • C++ Memory Management Operators
  • C++ Pointers
  • C++ Static Functions
  • C++ Friend Functions

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT