Character pointer to string literal declaration – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#7506    siteicon   siteicon  

Character pointer to string literal declaration

Understanding how character pointer variable is initialised with a string litereal.

Learning Objectives

  • Understanding declaration of a character pointer type variable along with string literal initialisation.
  • Understanding manipulation of character pointers with string functions.
  • Reading character pointers like arrays.

Source Code

TC++ #7506


Source Code

Run Output


Code Understanding

const char *c=”Magic”;
Here c is a pointer type variable that has been initialised with a string literal. The const specifier is needed because from c++ 2003 version on wards string literals are immutable (constant values)

This will print M as value at 0th address is M treating as if string literal is a read only array.

int len=strlen(c);
Pointer variables allow string functions.

This will print length as 5.

This will print the last character as last character is denoted by len-1.

This has been commented as this can create problems in c++ 2003 version on wards. In previous version this can be done.

Pointer variable can be used directly to print whole string, treating them as null terminated string.


  • Documentation of most string functions will depict parameter to be passed as pointer variable only.

Suggested Filename(s): charptr.cpp, ptr2slit.cpp


CSKC| Created: 20-Sep-2019 | Updated: 20-Sep-2019|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues