Character pointer to string literal declaration – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



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

M
5
c
Magic

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)

cout<<c[0]<<endl;
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.

cout<<len<<endl;
This will print length as 5.

cout<<c[len-1]<<endl;
This will print the last character as last character is denoted by len-1.

//c[4]=’x’;
This has been commented as this can create problems in c++ 2003 version on wards. In previous version this can be done.

cout<<c<<endl;
Pointer variable can be used directly to print whole string, treating them as null terminated string.

Notes

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


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



Share

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


Back