I using a library and in it there are some lines I don't understand. What does this syntax do?Code://array of ints //index is an integer (*array)[index]
I using a library and in it there are some lines I don't understand. What does this syntax do?Code://array of ints //index is an integer (*array)[index]
If "array" is really an array of ints, then what you have isn't anything. For this to work, *array should be of pointer or array type (and therefore array itself needs to be pointer-to-array or pointer-to-pointer or array-of-pointer or array-of-array).
I wrote a simple program to show what is going on. Just remember to always ask yourself "What do I have and what is it that I want?" I don't know what "array" is in your program. What it's doing depends on what "array" is. This example is to clarify the notation for you.
Code:#include <iostream> int main(int argc, char* argv[]){ int *a[7]; int index = 4; (*a)[0] = 0; (*a)[1] = 1; (*a)[2] = 2; (*a)[3] = 3; (*a)[4] = 4; (*a)[5] = 5; (*a)[6] = 6; std::cout << "fourth element: " << (*a)[index] << std::endl; std::cout << "fourth element: " << *((*a)+4) << std::endl; return 0; }
Last edited by Meiryousa; 01-15-2014 at 01:18 PM.
OK, I got it. Thanks Meiyrusa!
Meiryousa: your example program is problematic. *a is the first pointer in the array, i.e., a[0]. (*a)[0] thus dereferences that pointer (equivalently a[0][0]), but the pointer was not given a value. Next, (*a)[1] treats that first pointer as if it were a pointer to the first element of an array, and then results in the second element of that array.
EDIT:
You could fix your example if you changed your declaration of a to something like:
Code:int numbers[7]; int (*a)[7] = &numbers;
Last edited by laserlight; 01-15-2014 at 01:05 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
laserlight:I was using an array of references and putting solid values in them rather than references. It isn't as much problematic as it is not making any sense. At least as far as I understand. Perhaps this would be a better example? Unless I am misunderstanding something.
Code:#include <iostream> int main(int argc, char* argv[]){ int *a[2]; //a is a reference each element is a reference. int a2[7]; //a2 is a reference each element is a value. int a3[4]; //same as a2 int index = 1; a2[0] = 0; a3[0] = 7; a2[1] = 1; a3[1] = 8; a2[2] = 2; a3[2] = 9; a2[3] = 3; a3[3] = 10; a2[4] = 4; a2[5] = 5; a2[6] = 6; a[0] = a2; a[1] = a3; std::cout << "a[0][5]: " << a[0][5] << std::endl; std::cout << "a[1][3]: " << a[1][3] << std::endl; /***************************** Showing various syntax *****************************/ std::cout << (*a) << " = " << a[0] << " = " << &a2 << std::endl; std::cout << (a)[index] << " = " << (*(a+index)) << " = " << a3 << std::endl; std::cout << *(a)[index] << " = " << *(*(a+index)) << " = " << *a3 << std::endl; std::cout << *(a)[index]+2 << " = " << *(*(a+index)+2) << " = " << *(a3+2) << std::endl; return 0; }
Your comments don't quite match the code. a is an array of pointers-to-int, not references-to-int. a2 is an array of ints, although when used by itself the name a2 can decay to a pointer-to-int (pointing specifically at the first member of the array).
tabstop:In my mind a pointer is just like any other variable except it is designed to hold a reference. This is why I find my wording to be more clear. The way you're saying it is more official I think. An array most of the time acts just like a const pointer. So a2 also holds a reference in my mind.
But pointers and references are two very distinct (but related) concepts in C++.
You need to clarify your terminology, otherwise you're going to run into problems at some stage - like for example trying to describe a problem to some other coder.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Ok, I see that my first post was missing information.
I guess this is a bit more informative.Code:(*hist) = (int *) malloc( ncolors * sizeof(int) ); // populate histo and nc ... (*permut_index) = (int *) malloc( n * sizeof(int) ); int pos; for(int j=0; j<n; ++j){ pos=0; for(int k=0; k<nc[j]; ++k){ pos += (*hist)[k]; } pos+= colcount[nc[j]]; (*permut_index)[pos]= j; ++colcount[nc[j]]; }
I have a sneaking suspicion that most of the arrays are declared wrong in the program. The extra layers of indirection aren't needed.
As people may have explained before int (*foo)[7] makes foo a pointer to an array of 7 ints. so the parentheses do matter here. (*foo) would dereference it, giving you the pointed to array object.Code:int *hist; int *permut_index; hist = (int *) malloc( ncolors * sizeof(int) ); // populate histo and nc ... permut_index = (int *) malloc( n * sizeof(int) ); int pos; for(int j=0; j<n; ++j){ pos=0; for(int k=0; k<nc[j]; ++k){ pos += hist[k]; } pos+= colcount[nc[j]]; permut_index[pos]= j; ++colcount[nc[j]]; }
Also you dereference in weird places. Don't call malloc() with (*pointer) or *pointer on the left side for now. The value of a pointer is a memory address, like how a whole number is the value of an int. When you dereference where you will store the address, you are indicating that the object being pointed to is another pointer. That is probably not the case.
Thanks to all, dazzling me with their knowledge... and to all eagerly pointing out things frustrating you in the code. I was just parallelizing some stuff and came across some lines I didn't get.