A Simple C++ Question.
-
Have a look at the below code:
#include "stdafx.h"
#include "conio.h"int _tmain(int argc, _TCHAR* argv[])
{
//Snippet 1
char name[20] = "Some Name";
printf("%s\n", name);
name[1] = 'a';
printf("%s\n", name);//Snippet 2 char \* name2 = "Some Name"; printf("%s\\n", name2); name2\[1\] = 'a'; printf("%s\\n", name2); getch(); return 0;
}
Why we get Runtime error when we modify name2[1] = 'a';?
-
Have a look at the below code:
#include "stdafx.h"
#include "conio.h"int _tmain(int argc, _TCHAR* argv[])
{
//Snippet 1
char name[20] = "Some Name";
printf("%s\n", name);
name[1] = 'a';
printf("%s\n", name);//Snippet 2 char \* name2 = "Some Name"; printf("%s\\n", name2); name2\[1\] = 'a'; printf("%s\\n", name2); getch(); return 0;
}
Why we get Runtime error when we modify name2[1] = 'a';?
It all boils down to the difference between an array and a pointer. For
name
, you have set aside room for 20 characters (that are initialized to "Some Name"). Forname2
, you are pointing to a static piece of memory. That memory cannot be changed."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Have a look at the below code:
#include "stdafx.h"
#include "conio.h"int _tmain(int argc, _TCHAR* argv[])
{
//Snippet 1
char name[20] = "Some Name";
printf("%s\n", name);
name[1] = 'a';
printf("%s\n", name);//Snippet 2 char \* name2 = "Some Name"; printf("%s\\n", name2); name2\[1\] = 'a'; printf("%s\\n", name2); getch(); return 0;
}
Why we get Runtime error when we modify name2[1] = 'a';?
-
Have a look at the below code:
#include "stdafx.h"
#include "conio.h"int _tmain(int argc, _TCHAR* argv[])
{
//Snippet 1
char name[20] = "Some Name";
printf("%s\n", name);
name[1] = 'a';
printf("%s\n", name);//Snippet 2 char \* name2 = "Some Name"; printf("%s\\n", name2); name2\[1\] = 'a'; printf("%s\\n", name2); getch(); return 0;
}
Why we get Runtime error when we modify name2[1] = 'a';?
You are trying to modify a const char array (pointer). This is not C++. if you were writing proper C++, you would not get those problems (or a lot less likely).
std::string name = "some name";
std::cout << name << std::endl;std::string name2 = "some name";
std::cout << name2 << std::endl;name2[1] = 'a'; // need to check that the index is within the string size.
std::cout << name2 << std::endl;Watched code never compiles.
-
You are trying to modify a const char array (pointer). This is not C++. if you were writing proper C++, you would not get those problems (or a lot less likely).
std::string name = "some name";
std::cout << name << std::endl;std::string name2 = "some name";
std::cout << name2 << std::endl;name2[1] = 'a'; // need to check that the index is within the string size.
std::cout << name2 << std::endl;Watched code never compiles.
Technically, to pick nits, it *is* C++ as C++ is a superset of C. What you are really saying is that it isn't "Object Oriented Programming" which is true, but it is still within the definition of C++.
-
You are trying to modify a const char array (pointer). This is not C++. if you were writing proper C++, you would not get those problems (or a lot less likely).
std::string name = "some name";
std::cout << name << std::endl;std::string name2 = "some name";
std::cout << name2 << std::endl;name2[1] = 'a'; // need to check that the index is within the string size.
std::cout << name2 << std::endl;Watched code never compiles.
Agree with Chuck... technically it is C++.
-
Technically, to pick nits, it *is* C++ as C++ is a superset of C. What you are really saying is that it isn't "Object Oriented Programming" which is true, but it is still within the definition of C++.
-
Technically, to pick nits, it *is* C++ as C++ is a superset of C. What you are really saying is that it isn't "Object Oriented Programming" which is true, but it is still within the definition of C++.
-
Chuck O'Toole wrote:
Technically, to pick nits, it *is* C++ as C++ is a superset of C
Does the newest ANSI C++ standard incorporate the latest ANSI C standard as a subset? Last I heard that was not the case
The point is, C++ did *not* decommit the "char" data type nor pointers. std::string is *not* the only way to do string manipulation in C++. It may be the perferred way for some people but it's not the only way.
-
C++98 is not a superset of C90 - especially when you consider
const
and what happens when you cast awayconst
. C allows modification of literals (string and constants) through pointers while C++ doesn't or rather considers it undefined behaviour.The point is, C++ did *not* decommit the "char" data type nor pointers. std::string is *not* the only way to do string manipulation in C++. It may be the perferred way for some people but it's not the only way.
-
The point is, C++ did *not* decommit the "char" data type nor pointers. std::string is *not* the only way to do string manipulation in C++. It may be the perferred way for some people but it's not the only way.
Chuck O'Toole wrote:
The point is, C++ did *not* decommit the "char" data type nor pointer
I was responding to what appeared to be a general comment about the language in general and in its entirety and not just one small part. And that general comment, at this time (new standards), is wrong.
-
Chuck O'Toole wrote:
The point is, C++ did *not* decommit the "char" data type nor pointer
I was responding to what appeared to be a general comment about the language in general and in its entirety and not just one small part. And that general comment, at this time (new standards), is wrong.
Context dude, gotta read comments in the context of the thread
-
Context dude, gotta read comments in the context of the thread
-
It all boils down to the difference between an array and a pointer. For
name
, you have set aside room for 20 characters (that are initialized to "Some Name"). Forname2
, you are pointing to a static piece of memory. That memory cannot be changed."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
Thanks Crow. That is constant piece of memory.