A
Amrit Kohli
Hello.
I have the following code, to do a simple operation by copying the elements
of a vector of strings into an array of char pointers. However, when I run
this code, the first element in the char array strarr[0] holds garbage
characters. For some reason, every time the strcpy function is called to
copy the string in temp to the array, it fills the 0th element in the strarr
array with garbage characters. Can someone try to compile this code and
debug it and see if they get the same results that I'm getting? Any insight
into why this might be happening? I'm really stumpped. The code works for
every other element in strarr except for the 0th element. I've even tried
directly setting the strarr[0]th element to the pointer of temp with this
statement:
strarr = temp; // where i = 0
and the pointer value gets set, but the characters are still garbage. When
I look at the value of strarr[0] in the debugger, no matter how I assign the
temp string to the array index, it always comes out as garbage, even when
the pointer is equal to the value of temp. The temp variable holds the
correct string, but for some reason, it won't properly copy into the 0th
element of the array.
Any help on this would be greatly appreciated.
Thanks -
#include <iostream>
#include <string>
#include <vector>
#include <bitset>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
vector<string> str;
string s;
while(cin >> s)
str.push_back(s);
const size_t array_size = 100;
char *strarr[array_size];
int i = 0;
for(vector<string>::iterator iter = str.begin();iter !=
str.end();++iter, ++i)
{
size_t len = (*iter).length() + 1;
strarr = new char[len];
char *temp = new char[len];
strcpy(temp, (*iter).c_str());
strcpy(strarr, temp);
}
return 0;
}
I have the following code, to do a simple operation by copying the elements
of a vector of strings into an array of char pointers. However, when I run
this code, the first element in the char array strarr[0] holds garbage
characters. For some reason, every time the strcpy function is called to
copy the string in temp to the array, it fills the 0th element in the strarr
array with garbage characters. Can someone try to compile this code and
debug it and see if they get the same results that I'm getting? Any insight
into why this might be happening? I'm really stumpped. The code works for
every other element in strarr except for the 0th element. I've even tried
directly setting the strarr[0]th element to the pointer of temp with this
statement:
strarr = temp; // where i = 0
and the pointer value gets set, but the characters are still garbage. When
I look at the value of strarr[0] in the debugger, no matter how I assign the
temp string to the array index, it always comes out as garbage, even when
the pointer is equal to the value of temp. The temp variable holds the
correct string, but for some reason, it won't properly copy into the 0th
element of the array.
Any help on this would be greatly appreciated.
Thanks -
#include <iostream>
#include <string>
#include <vector>
#include <bitset>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
vector<string> str;
string s;
while(cin >> s)
str.push_back(s);
const size_t array_size = 100;
char *strarr[array_size];
int i = 0;
for(vector<string>::iterator iter = str.begin();iter !=
str.end();++iter, ++i)
{
size_t len = (*iter).length() + 1;
strarr = new char[len];
char *temp = new char[len];
strcpy(temp, (*iter).c_str());
strcpy(strarr, temp);
}
return 0;
}