Skip to main content
deleted 260 characters in body
Source Link
Prashant Kumar
  • 23.2k
  • 14
  • 52
  • 64

I'm a bit of a newbie in C++ and recently stumbled upon a question I've not really found a decent answer too. What I'm trying to do is create a string array and then usinguse a pointer to modify it. I'm not quite sure how to declare the pointer since strings can vary in length, and I think this is what causes the error.

What I have so far isMy code looks something like this (Just cut out the important bits, if u want all source tell me and I'll edit it in):

#includes <string>
#includes <iostream>
using namespace std;

string *users = NULL;
int seatNum = NULL;
cin >> seatNum;
users = new string[seatNum];
string name;
cin >> name;
users[seatNum] = name;

It throws me an Write Access Violation when I try to change its value. From what I've read it's because strings are compiled as read-only, so my question is how would I/what would I do to change it? An "easy to understand" explanationEasy-to-understand explanations would be preferable as I'm not comfortable with C++ at the moment.

I'm a bit of a newbie in C++ and recently stumbled upon a question I've not really found a decent answer too. What I'm trying to do is create a string array and then using a pointer to modify it. I'm not quite sure how to declare the pointer since strings can vary in length, and I think this is what causes the error.

What I have so far is this (Just cut out the important bits, if u want all source tell me and I'll edit it in)

#includes <string>
#includes <iostream>
using namespace std;

string *users = NULL;
int seatNum = NULL;
cin >> seatNum;
users = new string[seatNum];
string name;
cin >> name;
users[seatNum] = name;

It throws me an Write Access Violation when I try to change its value. From what I've read it's because strings are compiled as read-only, so my question is how would I/what would I do to change it? An "easy to understand" explanation would be preferable as I'm not comfortable with C++ at the moment.

I'm trying to create a string array and use a pointer to modify it. I'm not sure how to declare the pointer since strings can vary in length, and I think this is what causes the error.

My code looks something like this:

#includes <string>
#includes <iostream>
using namespace std;

string *users = NULL;
int seatNum = NULL;
cin >> seatNum;
users = new string[seatNum];
string name;
cin >> name;
users[seatNum] = name;

It throws me an Write Access Violation when I try to change its value. From what I've read it's because strings are compiled as read-only, so my question is how would I/what would I do to change it? Easy-to-understand explanations would be preferable.

Source Link
PeppeJ
  • 643
  • 3
  • 11
  • 24

Dynamically allocated string array, then change it's value?

I'm a bit of a newbie in C++ and recently stumbled upon a question I've not really found a decent answer too. What I'm trying to do is create a string array and then using a pointer to modify it. I'm not quite sure how to declare the pointer since strings can vary in length, and I think this is what causes the error.

What I have so far is this (Just cut out the important bits, if u want all source tell me and I'll edit it in)

#includes <string>
#includes <iostream>
using namespace std;

string *users = NULL;
int seatNum = NULL;
cin >> seatNum;
users = new string[seatNum];
string name;
cin >> name;
users[seatNum] = name;

It throws me an Write Access Violation when I try to change its value. From what I've read it's because strings are compiled as read-only, so my question is how would I/what would I do to change it? An "easy to understand" explanation would be preferable as I'm not comfortable with C++ at the moment.