I am able to modify a constant string

pankaj kushwaha

How come that output of following programm is "pankaj", I was expecting a compilation error like "can not modify a constant string"

#include<iostream>
using namespace std;

void fun(const char *a)
{
  a = "pankaj";
  cout  << a;
}

int main()
{
  const char *ptr = "GeeksforGeeks";
  fun(ptr);
  return 0;
}
deviantfan

You´re not modifying any string, just the parameter a.
First, it´s a pointer to the passed value, then it´s a pointer to a string constant in your program.
You can´t change the passed string data, but changing the pointer is fine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why am I able to dereference and modify an `int * const` pointer?

From Dev

I am not able to get the connection string for db file

From Dev

Why I am not able to access string individual chars using index

From Dev

Why I am able to change member of an object which is passed as constant reference?

From Dev

I am a beginner in programming in c,need help in sizeof() string constant?

From Dev

Why am I able to modify a List in a method that's out of scope, without returning it?

From Dev

Why am I able to modify a List in a method that's out of scope, without returning it?

From Dev

I am not able to install svnnotify

From Dev

Why am I not being able to split a String in java given that I have a string containing filename?

From Dev

Why am I not being able to split a String in java given that I have a string containing filename?

From Dev

(String instanceof Object) returns true but I am not able to find the Object.prototype in String.prototype chain

From Dev

How am I able to insert an ArrayList<String> into a constructor that accepts List<Integer>?

From Dev

Why am I not able to convert a string when using non-ASCII characters?

From Dev

How am I able to insert an ArrayList<String> into a constructor that accepts List<Integer>?

From Dev

Typescript: Why am I able to allocate a string value to my declared (interface) number object parrameter

From Dev

Why am I not Able to Construct This Regex

From Dev

Why am I not able to open a VS project

From Dev

Am I able to call a Enums constructor in Java?

From Dev

I am not able to click on button in protractor

From Dev

Why am I not able to wget these types of links?

From Dev

Why am I not able to change worksheet reference?

From Dev

I am not able to get the file path

From Dev

Why am I not able to add a JPanel to a JPanel?

From Dev

Why am I not able to use $this is an opencart helper?

From Dev

I am not able to use enumerate function properly

From Dev

I am not able to update TexStudio in Ubuntu 15.04

From Dev

I am not able to run this Python program

From Dev

I am not able to log on to the Windows Recovery Tool

From Dev

Why am I able to open this file?

Related Related

  1. 1

    Why am I able to dereference and modify an `int * const` pointer?

  2. 2

    I am not able to get the connection string for db file

  3. 3

    Why I am not able to access string individual chars using index

  4. 4

    Why I am able to change member of an object which is passed as constant reference?

  5. 5

    I am a beginner in programming in c,need help in sizeof() string constant?

  6. 6

    Why am I able to modify a List in a method that's out of scope, without returning it?

  7. 7

    Why am I able to modify a List in a method that's out of scope, without returning it?

  8. 8

    I am not able to install svnnotify

  9. 9

    Why am I not being able to split a String in java given that I have a string containing filename?

  10. 10

    Why am I not being able to split a String in java given that I have a string containing filename?

  11. 11

    (String instanceof Object) returns true but I am not able to find the Object.prototype in String.prototype chain

  12. 12

    How am I able to insert an ArrayList<String> into a constructor that accepts List<Integer>?

  13. 13

    Why am I not able to convert a string when using non-ASCII characters?

  14. 14

    How am I able to insert an ArrayList<String> into a constructor that accepts List<Integer>?

  15. 15

    Typescript: Why am I able to allocate a string value to my declared (interface) number object parrameter

  16. 16

    Why am I not Able to Construct This Regex

  17. 17

    Why am I not able to open a VS project

  18. 18

    Am I able to call a Enums constructor in Java?

  19. 19

    I am not able to click on button in protractor

  20. 20

    Why am I not able to wget these types of links?

  21. 21

    Why am I not able to change worksheet reference?

  22. 22

    I am not able to get the file path

  23. 23

    Why am I not able to add a JPanel to a JPanel?

  24. 24

    Why am I not able to use $this is an opencart helper?

  25. 25

    I am not able to use enumerate function properly

  26. 26

    I am not able to update TexStudio in Ubuntu 15.04

  27. 27

    I am not able to run this Python program

  28. 28

    I am not able to log on to the Windows Recovery Tool

  29. 29

    Why am I able to open this file?

HotTag

Archive