If you would try to pass a 1-D array to function like this then its perfect what you did, but in case you are trying to pass 2-D array then u need to pass the array with its valid syntax, which is not just like the 1-D
i.e. return_type function_name(dataType arrayName[100],int size)
in case you can also leave the subscripts ‘[]’ as blank for 1-D array ONLY,
but for 2-D array it would be
return_type function_name(dataType arrayName[100][100],int rowSize,int colSize)
however you can also leave the subscripts ‘[]’ to be blank here also but only the 1st [] and 2nd must have to contain some value in it.
actually it doesn’t matter what value you passed in 2nd subscripts, but some value should has to be there, just pass a value to which your 2-D array would not exceed.
And the syntax will keep going like this for further dimensions of array.
Hope this will work for you.
Loading
|
|
Here’s a snippet of code I’ve been working on for a program. Whenever I run I end up with a «invalid types ‘char[int]’ for array subscript» error. Could someone please enlighten me where I’ve gone wrong? Can you not loop through char arrays, or have I misunderstood? Welcome to any feedback.
Last edited on
You can absolutely loop through any array. You’ve passed string1 as a regular variable, and not an array. Passing an array would be either:
|
|
Last edited on
What you’ve done is assume that the string type is a null-terminated character array when it’s an actual object (non-primitive).
|
|
I do not recommend rcast’s method of doing it because I assume you’re already working with strings instead of char arrays and you don’t want to cast if you don’t have too.
Zaita wrote: |
---|
I do not recommend rcast’s method of doing it because I assume you’re already working with strings instead of char arrays and you don’t want to cast if you don’t have too. |
The OP is passing a char not a string Zaita. He even states he is passing char arrays…
Last edited on
@rcast Where does he say he is passing a char?
The error posted is a subscript error that occurs when you try to do a character comparison against a string exceeding it’s length. Which occurs in this example because he has assumed a string is null-terminated.
In his post original post he is passing char string1 and char string2. I was guessing that those were C-Strings.
|
|
zero117 wrote: |
---|
Can you not loop through char arrays, or have I misunderstood? Welcome to any feedback |
I’m pretty sure a subscript error can occur with character arrays as well. From what I know anything that is passed with data type char, is a char.
Last edited on
His whole function is messed up
Zaita wrote: |
---|
His whole function is messed up |
I believe the MyString::strcomp makes it a method not a function. :p
Last edited on
Hmmm true true
Last edited on
Driver file
|
|
This is in my driver program. I in no way am going to pretend I can fully keep up with arrays involving string literals and even though I have all the proper information actual usage is a bit new to me. As you can see here I was able to declare the strings in one of the accepted formats.
Class Header
|
|
My problem begins where I leave main and go into the function definitions as presented initially, then I can’t seem to make any use of what I’ve passed in. I included these extra pieces of code so anyone can explain to me if I’ve done something wrong.
Last edited on
Again let me state my unfamiliarity with these types of arrays. And moreover we were instructed to use array subscripting only for one version of each function, and pointer and pointer arithmetic for another. This is why I’m hesitant to use anything that doesn’t have brackets in this particular piece.
Try this:
|
|
You have nothing to return yet. If you will be comparing the two as the method name implies than you could return a bool if they are equal or unequal by defining the method as
|
|
or if you won’t be returning anything
|
|
Last edited on
|
|
is what I ended up using. As you can see we are manually recreating the functions that can be found in <cstring> or which ever library the compiler uses.
|
|
Is the result function the string comparison pointed to.
Again my main difficulty is just to understand what I can and can’t do with pointers( ofc besides not going out of the memory address and so forth) . Can I add something into any of my arrays without erasing what was there or are their sizes definitely set? I’m asking since I didn’t declare them const.
*
I obviously should change the comment on it since like the original function this one doesn’t compare individual characters but rather only the size of the array. However I’ll keep it for now to keep track of which function is supposed to stick to subscripts and which is to use pointer arithmetic.
Last edited on
Zaita wrote: |
---|
What you’ve done is assume that the string type is a null-terminated character array when it’s an actual object (non-primitive). |
You can use the subscript operator with std::string almost like a normal c-string. str[str.size()] should return ».
rcast wrote: |
---|
I believe the MyString::strcomp makes it a method not a function. :p |
In C++ it’s often called a member function, so calling it a function make sense.
zero117 wrote: |
---|
Can I add something into any of my arrays without erasing what was there or are their sizes definitely set? I’m asking since I didn’t declare them const. |
The size is fixed and can’t be changed. You will find that arrays has a lot of shortcomings which will lead you to learn and love std::vector. All in good time.
Last edited on
Oh, just asking because I have to recreate the connotate function. Well now I’m assuming I’ll just have to create a third array to act impartially, or find some way around. And another short coming was I noticed I misunderstood the comp function. I assumed it measured array length and not individual character codes. So now I have it counting if the characters match. If the number of matches is less than the second string it’ll return that first is less than the second and so forth.
*Also as you mentioned a friend already told me of the messy business with arrays.
Last edited on
I’ve gotten much headway and thanks all for any contribution. Can I ask if anyone is familiar with how strcat works? I’m trying to recreate it here, and as said before I don’t think I can change the size of string1 to include string 2, probably it uses some hidden coding. The best I’ve managed is to create a third array of size ( total of length of array 1 and array 2) and then place them in there ( ofc I added two extra spaces, one for a space in between and one for the null character).
|
|
here is one of the functions using the code I described above ( as mentioned earlier I create two, one using regular subscripting, the other using pointer arithmetic) . Again is there anyway to overwrite the value of the first array to include both.
*Edit
Apparently I can assign the value of the new array to array1 by doing
Last edited on
@rcast yes that is the function, but I’m replicating them, as per our instructions, we are to replicate the compare and concatenate functions that are in the string library. But I’m more fishing for ways how this are done. Again I’m always open to different approaches although I already have my current solution.
|
|
Ok I end up running this piece of code because i noticed some odd behaviour. Apparently it automatically fills in the array by combining both of my arrays even though I didn’t ask for it. Nor have I manually looped through assigning anything from the second string. Anyone have an explanation as to why this is happening? ( It’s not a bad thing per se, but I’d like some closure).
*Tested my previous function doing the same and no sign of this. Is seem to automatically take on the second word from the second string. I’m not sure if this is some kind of memory error.
Last edited on
okay I finished and here’s what I have, if someone can tell me why I have errors with the last two functions( they should only be joining the specified numbers or characters) , that would be very nice. Again welcome to any feed back. #include «mystring.h»
.cpp
|
|
Header
|
|
Driver
|
|
14 Years Ago
Compiler errors:
(In main)
Line 9:cannot convert char (*)[3] to char* for argument 1 to void showboard(char*, int, int)
(in showBoard)
Lines 16, 17, 21, 22, 26, 27:invalid types char[int] for array subscript
I don’t understand these errors. I am trying to make a tic tac toe game, I have to initialize the board with ‘*’s. Right now I am merely trying to display the board with the ‘*’s. I have searched the forums and although there are a lot of tic tac toe threads none apply to me. Any help would be greatly appreciated, thanks!
void showBoard(char[], int, int);
int main() {
const int row = 3;
const int col = 3;
char game[row][col] = {{ '*', '*', '*'}, { '*', '*', '*'}, { '*', '*', '*'}};
showBoard(game, row, col);
return 0;
}
void showBoard(char game[], int row, int col) {
cout << setw(5) << "1" << setw(6) << "2" << setw(6) << "n"
<< setw(8) << "|" << setw(6) << "|" << "n"
<< "A" << setw(3) << game[0][0] << setw(3) << "|" << setw(3) << game[0][1]
<< setw(3) << "|" << setw(3) << game[0][2] << "n"
<< setw(8) << "|" << setw(6) << "|" << "n"
<< " ------------------------------------n"
<< setw(8) << "|" << setw(6) << "|" << "n"
<< "B" << setw(3) << game[1][0] << setw(3) << "|" << setw(3) << game[1][1]
<< setw(3) << "|" << setw(3) << game[1][2] << "n"
<< setw(8) << "|" << setw(6) << "|" << "n"
<< " ------------------------------------n"
<< setw(8) << "|" << setw(6) << "|" << "n"
<< "C" << setw(3) << game[2][0] << setw(3) << "|" << setw(3) << game[2][1]
<< setw(3) << "|" << setw(3) << game[2][2] << "n"
<< setw(8) << "|" << setw(6) << "|" << "n"
}
Recommended Answers
You need to declare showBoard like this:
void showBoard(char[][3], int, int);
Note that this hardcodes the size of the second dimension, but that will be okay if the board is always 3 by 3 (or indeed anything by 3).
Jump to Post
All 3 Replies
nucleon
114
Posting Pro in Training
14 Years Ago
You need to declare showBoard like this: void showBoard(char[][3], int, int);
Note that this hardcodes the size of the second dimension, but that will be okay if the board is always 3 by 3 (or indeed anything by 3).
ArkM
1,090
Postaholic
14 Years Ago
In other words, you are trying to bind 2D array argument (line 9) with 1D array parameter declared in the line 13, then use 1D array parameter as 2D array in the function body (other lines).
Is it surprising that your compiler is surprised at these poetic licenses?
14 Years Ago
You need to declare showBoard like this:
void showBoard(char[][3], int, int);
Note that this hardcodes the size of the second dimension, but that will be okay if the board is always 3 by 3 (or indeed anything by 3).
That fixed it, thank you so much nucleon!
And thanks to ArkM for additional explanation!
Reply to this topic
Be a part of the DaniWeb community
We’re a friendly, industry-focused community of developers, IT pros, digital marketers,
and technology enthusiasts meeting, networking, learning, and sharing knowledge.
Загрузка…