problem with indexing a char pointer.

I have this char * in a class and I keep getting this error message:
invalid types 'char[int] for array subscript
on the following line of code:
 
c[firstemptyc()] = ts[j];

Where c is the char pointer and ts is a string and j is an integer index.

firstemptyc() is here:
1
2
3
4
5
6
7
    int firstemptyc()
    {
        int counter = 0;
        while(c[counter] == 0)
            ++counter;
        return counter;
    }


Through messing around and viewing examples on google I was able to come up with:
1
2
3
4
5
6
7
int* firstemptyc()
    {
        int* counter = 0;
        while(c[*counter] == 0)
            ++counter;
        return counter;
    }


and it was able to compile. But that seems hella unintuitive to me. Is that really correct?
Ok, I reproduce your error
1
2
char array;
array[42]; //invalid types 'char[int]' for array subscript 
The second try compiles because of pointer arithmetic.
array[index] == *(array+index) == *(index+array) == index[array]

I hope that your second function crash. Only Chuck Norris can dereference a null pointer.
thanks a bundle. but ytf does the first not compile in the first place? I'm indexing a char aray with an integer and it says invalid type for subscript? what else can you index a array with?
My guess is that you didn't declare an array but just one object.
I declared it as char pointer.
How long is the code; is it feasible to post more, most, or possibly all of it?
It would be easier for INTERPOL to issue an international arrest warrant for Bjarne Stroustrup.

I have read countless times that pointers can be accessed as arrays with an integer index. Now Please Mr Bjarne Strapon, give me an exhaustive list of all the situations in which an array can not be accessed with an integer index, and tell me what, if anything can be used instead.

Could I use an index of type 'dildo_widget'? And if not, where do you think I'm going to shove it?

ne555 wrote:

My guess is that you didn't declare an array but just one object.


No, I declared a char pointer. And not that I think that it would matter I allocated memory for it with new [] in the constructors.


I did find however that I needed to initialize the pointers to null and fixed the line in firstemptyc() to: while(!c[counter] == 0)

That however and now I'm getting an error same as before for an int pointer as well.

1
2
3
4
5
6
7
8
9
10
11
for (int i = 0; i < maximumsizeofarrays; ++i)
            {
                //i[i] = 0;
                c[i] = 0;
                b[i] = 0;
                d[i] = 0;
                f[i] = 0;
                s[i] = "";
                cp[i] = 0;
                index[i] = 0;
            }


That is from my constructors. It will compile as is because I have commented out i[i] = 0; I had been getting error: invalid types int[int] for array subscript, although the rest of the pointers seem to be behaving fine in this section.
Last edited on

Also immediately before the for loom I am able to access the integer pointer as follows:
 
i[0] = 0; i[1] = 0;


Etc. Please forgive my terminology but I think those would be called integer literals? Forgive me if I'm mistaken.


Ok never mind that, need to rename my counter variable from i to something else. lol

Seems ne555 you were right. I declared a local variable which was overriding the member variable. lol
Last edited on
Topic archived. No new replies allowed.