Can you please explain what this does ?Code:char (*programs)[31][21];
Then explain what this does ?
get methodCode:&data.get(programs[lp][0],20);
Im finding this whole thing abit hard to grasp...
Printable View
Can you please explain what this does ?Code:char (*programs)[31][21];
Then explain what this does ?
get methodCode:&data.get(programs[lp][0],20);
Im finding this whole thing abit hard to grasp...
lmfaoQuote:
take it easy on the little guy. he's just a kid
char (*programs)[31][21];
this allocates a pointer for an array.
this loads each word(divided by spaces) of up to 20 characters into the beginning of each array part.
after that(not shown here) i check to see if it's blank if it is, then i make that part of the array "EMPTYSLOT ".
How's that?
This is your answer
This is what cat wrote earlier in this threadQuote:
char (*programs)[31][21];
this allocates a pointer for an array.
I dont think you understand pointers properly.Quote:
char* programs[30][21];
This creates 630 pointers (presumably for strings of some kind) but allocates no memory for the strings themselves. Were you trying to allocate memory for 30 strings that are 21 characters long? That isn't what happened.
Taken from pointer tutorial on this site.Quote:
The keyword new is used to initialize pointers with memory from free store (a section of memory available to all programs). The syntax looks like the example:
Example:
int *ptr = new int;
this is how i created the array/pointer
and I try to allocate space like this...Code:char proglist[30][21];
char (*programs)[30];
I get errors saying that: cannot convert from 'char *' to 'char[21]' for the line when i allocate the space.Code:for(int lp=0;lp<=21;lp++)
{
programs[lp]=new char[21];
}
This should work ..
Code:int main()
{
char proglist[30][21];
char *programs[30];
for(int lp=0;lp<=21;lp++)
{
programs[lp]=new char[21];
}
system("PAUSE");
return 0;
}
these are the errors(same problem, can't convert):Code:int main()
{
char proglist[31][21];
char *programs[31];
for(int lp=0;lp<=30;lp++)
{
programs[lp]=new char[21];
}
programs=&proglist;
ifstream data("proglist.txt");
for(lp=0;lp<=30;lp++)
{
data.get(programs[lp], " ");
if(charcheck(programs[lp][0]," ")==true)
{
cout << programs[1];
strcpy(programs[lp][0],&"EMPTYSLOT ");
}
}
data.close();
cout << programs[1];
getch();
return 0;
}
Code:D:\Programs\Projects\Corewars\main.cpp(45) : error C2440: '=' : cannot convert from 'char (*)[31][21]' to 'char *[31]'
There are no conversions to array types, although there are conversions to references or pointers to arrays
D:\Programs\Projects\Corewars\main.cpp(49) : error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::get(char *,int)' : cannot convert parameter 2 from 'char [2
]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
D:\Programs\Projects\Corewars\main.cpp(50) : error C2664: 'charcheck' : cannot convert parameter 1 from 'char' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
D:\Programs\Projects\Corewars\main.cpp(53) : error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
Corewars.exe - 4 error(s), 0 warning(s)
just looking at that part of your code, this works for me:
Code:int main()
{
//char proglist[31][21];
char *programs[31];
for(int lp=0;lp<=30;lp++)
{
programs[lp]=new char;
}
programs[1]="hhh";
ifstream data("proglist.txt");
cout << programs[1];
int x;
cin>>x;
return 0;
}
Try this then...
Code:int main()
{
char *programs[5];
ifstream data("proglist.txt");
for(int lp=0;lp<=5;lp++)
{
cout << "programs["<<lp << "] points to : ";
programs[lp]=new char[21];
data.getline(programs[lp], 20, ' ');
cout << programs[lp] << endl;
}
for (int x=0; x <= 5; x++)
delete programs[x];
delete[] programs;
system("PAUSE");
return 0;
}
I get the same errors as before... could this be a compiler problem after 4 pages?
Btw, sorry, I had to go to walmart that was like 10 miles away
edit: waaaait... hold on
Now the program crashes before I can output anything, heres the updated code, fully commented and organized:
I'm getting an Illegal operation.Code:int main()
{
//Load the file proglist under the name data
ifstream data("proglist.txt");
//Create then allocate space to the pointer programs
//Looping through each individual piece of the array
char *programs[30];
for(int lp=0;lp<=30;lp++)
{
programs[lp]=new char[20];
}
//Loop through each peace of the array and load the next word in data to it
//Then close the file
for(lp=0;lp<=30;lp++)
{
data.get(programs[lp],20,' ');
//Print some parts of the array
cout << "Programs[" << lp << "] = " << programs[lp] << endl;
}
data.close();
//Loop through and free up the space from the array and then
//delete the array
for (lp=0; lp <= 30; lp++)
{
delete programs[lp];
}
delete[] programs;
//Wait for a keystroke and then end
getch();
return 0;
}
laasunde you there? salem?
Watch his post count soar!
I wasn't necessarily referring to your knowledge of pointers before, just your knowledge in general, you clearly have no idea what-so-ever about this program. Try something simpler.
Not sure why you get an illegal operation maybe someone else can spot the problem.
I do however think you should change your code from
toCode:data.get(programs[lp],20,' ');
it runs fine on my computer anyways.Code:data.getline(programs[lp], 20, ' ');
You should try to comment out part of your code so that you can narrow down what code segment thats causing your problem. You could probably start with the middle for loop, just a guess...
I think I pointed it out earlier that it always crashes at the call of data.get(programs[lp],20,' ')
Use getline instead of get as laasunde suggested. Your illegal operation is from here, no doubt:
delete[] programs;
You're trying to delete memory that is not yours to delete.