## RE: [Mingw-users] pointer

 RE: [Mingw-users] pointer From: Gary L. Sun - 2001-10-24 16:19:03 ```Pointers in c and C++ are addresses. Depend on the CPU, they could be 32-bit or 64-bit or bigger. Regardless, pointer math can be used, but they need to treated as unsigned int, unsigned long, or unsigned char. So, (ie+ib) >> 1 will give the correct result, if ie and ib are made as unsigned something before the math. When ie and ib are used later on as usual as pointers they will be and still are pointers. e.g. im = ie + m; im will point to the location ie added by offset m, depends m is positive or negative, im will be advanced or moved backwards. However, the word, char, byte, long, long long, boundary alignment is the responsibility of the coder. Hope this help. Gary #-----Original Message----- #From: mingw-users-admin@... #[mailto:mingw-users-admin@...]On Behalf Of #Gerald W. Shapiro #Sent: Wednesday, October 24, 2001 9:40 PM #To: Lloyd Dupont #Cc: Lorenzo; mingw #Subject: Re: [Mingw-users] pointer # # #When you add to a C pointer, the addition is understood by the compiler to #mean "go this many locations past the pointer". So ie + 1 would not add #one to the numerical value of the address, but would compute the address #of the next location that would store an integer. # #You maybe could cast the addresses as an integer type and do math on them, #but be careful of overflow. # #Better solution is to pass only a pointer to the head of the list, and #integer offsets within the list. Do math on the offsets. So if ie is an #int and ib is an int, and head is int* to the first in the list, then # #m = (ie + ib)/2; #im = head + m; # #gerald # #On Wed, 24 Oct 2001, Lloyd Dupont wrote: # #> Lorenzo wrote: #> #> > now i use: #> > im = (ib + ((ie - ib) / 2)); #> > #> > Is the best way? #> #> yes ! #> hum, but #> im = ib + (ie - ib) / 2; #> is the same with less parenthesis. #> #> it is your choice. #> #> #> _______________________________________________ #> MinGW-users mailing list #> MinGW-users@... #> #> You may change your MinGW Account Options at: #> https://lists.sourceforge.net/lists/listinfo/mingw-users #> # # # #_______________________________________________ #MinGW-users mailing list #MinGW-users@... # #You may change your MinGW Account Options at: #https://lists.sourceforge.net/lists/listinfo/mingw-users ```

 AW: [Mingw-users] pointer From: - 2001-10-24 12:24:30 ```I think this is true for all progamming languages with pointers to = elements: pointer - pointer =3D> number of elements between the pointers (not = bytes) pointer + offset =3D> pointer to the element offset after pointer all other is not allowed To get the middle element use something like pa + (pe - pa) / 2 R=FCdiger > -----Urspr=FCngliche Nachricht----- > Von: Lorenzo [SMTP:conte0@...] > Gesendet am: Mittwoch, 24. Oktober 2001 12:32 > An: mingw > Betreff: [Mingw-users] pointer >=20 > Hi all, > if i try to comile this program: >=20 > #include >=20 > using namespace std; >=20 > int *BinarySearch(int *ib, int *ie, int value); >=20 > int main(int argc, char **argv) > { > const int MAXSIZE =3D 10; > int array[MAXSIZE]; > for (int i =3D 0;i < MAXSIZE;++i) > array[i] =3D i; >=20 > int *i =3D BinarySearch(array, array + MAXSIZE, 1); >=20 > cout << "position: " << (i - array) << endl; > return 0; > } >=20 > int *BinarySearch(int *ib, int *ie, int value) > { > int *im; > while (ib !=3D ie - 1) > { > im =3D (ib + ie) / 2; <-- invalid operands `int *' and `int *' to = binary > `operator +' >=20 > if (*im < value) > ib =3D im; > else if (*im > value) > ie =3D im; > else > return im; > } > return ie; > } >=20 > I get an error, how can I resolve the problem? > I in advance thank everybody. >=20 > = ------------------------------------------------------------------------= -- > -- > --- > If debugging is the process of removing bugs, > then programming must be the process of putting them in. > = ------------------------------------------------------------------------= -- > -- > --- >=20 >=20 >=20 > _______________________________________________ > MinGW-users mailing list > MinGW-users@... >=20 > You may change your MinGW Account Options at: > https://lists.sourceforge.net/lists/listinfo/mingw-users ```
 Re: [Mingw-users] pointer From: Lorenzo - 2001-10-24 12:56:16 ```> pointer - pointer => number of elements between the pointers (not bytes) > pointer + offset => pointer to the element offset after pointer > all other is not allowed This is right! > To get the middle element use something like > pa + (pe - pa) / 2 i try: im = (ib + (ie - ib)) / 2; end get error: invalid operands `int *' and `int' to binary `operator /' other ideas to resolve the problem of "/ 2"? thanks ---------------------------------------------------------------------------- --- If debugging is the process of removing bugs, then programming must be the process of putting them in. ---------------------------------------------------------------------------- --- ----- Original Message ----- From: "Dehmel, Rüdiger" To: "'Lorenzo'" ; "mingw" Sent: Wednesday, October 24, 2001 2:24 PM Subject: AW: [Mingw-users] pointer I think this is true for all progamming languages with pointers to elements: pointer - pointer => number of elements between the pointers (not bytes) pointer + offset => pointer to the element offset after pointer all other is not allowed To get the middle element use something like pa + (pe - pa) / 2 Rüdiger > -----Ursprüngliche Nachricht----- > Von: Lorenzo [SMTP:conte0@...] > Gesendet am: Mittwoch, 24. Oktober 2001 12:32 > An: mingw > Betreff: [Mingw-users] pointer > > Hi all, > if i try to comile this program: > > #include > > using namespace std; > > int *BinarySearch(int *ib, int *ie, int value); > > int main(int argc, char **argv) > { > const int MAXSIZE = 10; > int array[MAXSIZE]; > for (int i = 0;i < MAXSIZE;++i) > array[i] = i; > > int *i = BinarySearch(array, array + MAXSIZE, 1); > > cout << "position: " << (i - array) << endl; > return 0; > } > > int *BinarySearch(int *ib, int *ie, int value) > { > int *im; > while (ib != ie - 1) > { > im = (ib + ie) / 2; <-- invalid operands `int *' and `int *' to binary > `operator +' > > if (*im < value) > ib = im; > else if (*im > value) > ie = im; > else > return im; > } > return ie; > } > > I get an error, how can I resolve the problem? > I in advance thank everybody. > > -------------------------------------------------------------------------- > -- > --- > If debugging is the process of removing bugs, > then programming must be the process of putting them in. > -------------------------------------------------------------------------- > -- > --- > > > > _______________________________________________ > MinGW-users mailing list > MinGW-users@... > > You may change your MinGW Account Options at: > https://lists.sourceforge.net/lists/listinfo/mingw-users ```
 Re: [Mingw-users] pointer From: Lorenzo - 2001-10-24 13:11:16 ```now i use: im = (ib + ((ie - ib) / 2)); Is the best way? ---------------------------------------------------------------------------- --- If debugging is the process of removing bugs, then programming must be the process of putting them in. ---------------------------------------------------------------------------- --- ```
 Re: [Mingw-users] pointer From: Lloyd Dupont - 2001-10-24 13:22:44 ```Lorenzo wrote: > now i use: > im = (ib + ((ie - ib) / 2)); > > Is the best way? yes ! hum, but im = ib + (ie - ib) / 2; is the same with less parenthesis. it is your choice. ```
 Re: [Mingw-users] pointer From: Gerald W. Shapiro - 2001-10-24 13:39:55 ```When you add to a C pointer, the addition is understood by the compiler to mean "go this many locations past the pointer". So ie + 1 would not add one to the numerical value of the address, but would compute the address of the next location that would store an integer. You maybe could cast the addresses as an integer type and do math on them, but be careful of overflow. Better solution is to pass only a pointer to the head of the list, and integer offsets within the list. Do math on the offsets. So if ie is an int and ib is an int, and head is int* to the first in the list, then m = (ie + ib)/2; im = head + m; gerald On Wed, 24 Oct 2001, Lloyd Dupont wrote: > Lorenzo wrote: > > > now i use: > > im = (ib + ((ie - ib) / 2)); > > > > Is the best way? > > yes ! > hum, but > im = ib + (ie - ib) / 2; > is the same with less parenthesis. > > it is your choice. > > > _______________________________________________ > MinGW-users mailing list > MinGW-users@... > > You may change your MinGW Account Options at: > https://lists.sourceforge.net/lists/listinfo/mingw-users > ```
 RE: [Mingw-users] pointer From: Gary L. Sun - 2001-10-24 16:19:03 ```Pointers in c and C++ are addresses. Depend on the CPU, they could be 32-bit or 64-bit or bigger. Regardless, pointer math can be used, but they need to treated as unsigned int, unsigned long, or unsigned char. So, (ie+ib) >> 1 will give the correct result, if ie and ib are made as unsigned something before the math. When ie and ib are used later on as usual as pointers they will be and still are pointers. e.g. im = ie + m; im will point to the location ie added by offset m, depends m is positive or negative, im will be advanced or moved backwards. However, the word, char, byte, long, long long, boundary alignment is the responsibility of the coder. Hope this help. Gary #-----Original Message----- #From: mingw-users-admin@... #[mailto:mingw-users-admin@...]On Behalf Of #Gerald W. Shapiro #Sent: Wednesday, October 24, 2001 9:40 PM #To: Lloyd Dupont #Cc: Lorenzo; mingw #Subject: Re: [Mingw-users] pointer # # #When you add to a C pointer, the addition is understood by the compiler to #mean "go this many locations past the pointer". So ie + 1 would not add #one to the numerical value of the address, but would compute the address #of the next location that would store an integer. # #You maybe could cast the addresses as an integer type and do math on them, #but be careful of overflow. # #Better solution is to pass only a pointer to the head of the list, and #integer offsets within the list. Do math on the offsets. So if ie is an #int and ib is an int, and head is int* to the first in the list, then # #m = (ie + ib)/2; #im = head + m; # #gerald # #On Wed, 24 Oct 2001, Lloyd Dupont wrote: # #> Lorenzo wrote: #> #> > now i use: #> > im = (ib + ((ie - ib) / 2)); #> > #> > Is the best way? #> #> yes ! #> hum, but #> im = ib + (ie - ib) / 2; #> is the same with less parenthesis. #> #> it is your choice. #> #> #> _______________________________________________ #> MinGW-users mailing list #> MinGW-users@... #> #> You may change your MinGW Account Options at: #> https://lists.sourceforge.net/lists/listinfo/mingw-users #> # # # #_______________________________________________ #MinGW-users mailing list #MinGW-users@... # #You may change your MinGW Account Options at: #https://lists.sourceforge.net/lists/listinfo/mingw-users ```
 Re: [Mingw-users] pointer From: Lloyd Dupont - 2001-10-24 13:18:21 ```> i try: > im = (ib + (ie - ib)) / 2; mmhh.. divide an address by 2 is unauthorised.. (ib / 2) and is often meaningless, even in ASM.... BTW you mistook and certainly woulod write this (correct) line: im = ib + (ie - ib) / 2; ```