Thread: [Dev-C++] Number of elements of a multidimensional
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Samuel K. <big...@ya...> - 2002-11-28 16:00:22
|
Isn't there a keyword sizeof in C/C++? It seems like I remember one but I've never had to use it. I think there is one and I think it returns the number of bytes in the array of objects. All you would have to do is divide by the number of bytes that is in just one object. I may be off on this. Like I said, I haven't really had to use this before. __________________________________________________ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com |
From: Carlos d. M. <cg...@wo...> - 2002-11-28 16:57:14
|
The sizeof applied to a array will give you the size of a pointer, never the size of the array. Say char *array; or char array[]; Then sizeof(array) =3D 4. (at least in my intelIII x86, with win2k 4 is the size for any kind of pointer). The only solution is to have some special value for the end of the array. Which for strings is '\0', and you can try to set another depending on which kind of data you're working with and which range of values you can find, so you can then choose on of the values which are out of the range to set as that special value. So you can reach for it and you can then now, with the approppriate functions you can write the total size of the array and the size of each row of the multidimensional array. Samuel Keding escribi=F3: > Isn't there a keyword sizeof in C/C++? It seems like I > remember one but I've never had to use it. I think > there is one and I think it returns the number of > bytes in the array of objects. All you would have to > do is divide by the number of bytes that is in just > one object. I may be off on this. Like I said, I > haven't really had to use this before. > > __________________________________________________ > Do you Yahoo!? > Yahoo! Mail Plus - Powerful. Affordable. Sign up now. > http://mailplus.yahoo.com > > ------------------------------------------------------- > This SF.net email is sponsored by: Get the new Palm Tungsten T > handheld. Power & Color in a compact size! > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |
From: Per W. <pw...@ia...> - 2002-11-28 18:08:43
|
Not true that sizeof never returns size of array. It depends on what declaration the compiler sees when processing the sizeof operator. void func(int *array) { // sizeof(array) =3D sizeof(int*) } void func(int array[]) { // sizeof(array) =3D sizeof(int*) } void func() { int array[4]; // sizeof(array) =3D 4*sizeof(int) } Big gotcha about this. char buf[100]; write(fd,buf,sizeof(buf)); converted to: char *buf =3D malloc(100); write(fd,buf,sizeof(buf)) would fail miserably, because the second version would only write 4 bytes in a 32-bit machine... /Per W On Thu, 28 Nov 2002, Carlos Garc=EDa del Monte wrote: > The sizeof applied to a array will give you the size of a pointer, never > the size of the array. > Say char *array; or char array[]; > Then sizeof(array) =3D 4. (at least in my intelIII x86, with win2k 4 is > the size for any kind of pointer). >=20 > The only solution is to have some special value for the end of the > array. Which for strings is '\0', and you can try to set another > depending on which kind of data you're working with and which range of > values you can find, so you can then choose on of the values which are > out of the range to set as that special value. So you can reach for it > and you can then now, with the approppriate functions you can write the > total size of the array and the size of each row of the multidimensional > array. >=20 > Samuel Keding escribi=F3: >=20 > > Isn't there a keyword sizeof in C/C++? It seems like I > > remember one but I've never had to use it. I think > > there is one and I think it returns the number of > > bytes in the array of objects. All you would have to > > do is divide by the number of bytes that is in just > > one object. I may be off on this. Like I said, I > > haven't really had to use this before. > > > > __________________________________________________ > > Do you Yahoo!? > > Yahoo! Mail Plus - Powerful. Affordable. Sign up now. > > http://mailplus.yahoo.com > > > > ------------------------------------------------------- > > This SF.net email is sponsored by: Get the new Palm Tungsten T > > handheld. Power & Color in a compact size! > > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en > > _______________________________________________ > > Dev-cpp-users mailing list > > Dev...@li... > > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >=20 >=20 >=20 > ------------------------------------------------------- > This SF.net email is sponsored by: Get the new Palm Tungsten T > handheld. Power & Color in a compact size! > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >=20 |
From: Ioannis V. <no...@ho...> - 2002-11-29 13:25:34
|
> -----Original Message----- > From: dev...@li...=20 > [mailto:dev...@li...] On Behalf=20 > Of Carlos Garc=EDa del Monte > Sent: Thursday, November 28, 2002 6:54 PM > To: dev-cpp-users-list > Subject: Re: [Dev-C++] Number of elements of a multidimensional >=20 >=20 > The sizeof applied to a array will give you the size of a=20 > pointer, never > the size of the array. > Say char *array; or char array[]; As a function argument, because only then the second is a pointer. Inside the scope of its definition, a char array[]=3D"12345678901234567890"; size_t x=3Dsizeof(array); x will always be 21 (the '\0' included). However in void f(char array[]); is equivallent to void f(char *); so you can do array++; for example because all tthese are ponters. You can do that even in the case: void f(char array[20]) { /* The !=3D'\0' is not needed */ while(*array!=3D'\0') *array++=3D'a'; } All "array" arguments of functions are pointers but we can use the array syntax for notational purposes. > Then sizeof(array) =3D 4. (at least in my intelIII x86, with win2k 4 = is > the size for any kind of pointer). >=20 > The only solution is to have some special value for the end of the > array. Which for strings is '\0', and you can try to set another > depending on which kind of data you're working with and which range of > values you can find, so you can then choose on of the values which are > out of the range to set as that special value. So you can reach for it > and you can then now, with the approppriate functions you can=20 > write the > total size of the array and the size of each row of the=20 > multidimensional > array. Yes he has to "remember" the size of the array when he passes it across functions, even by passing the sizes as additional arguments, or other tricks. Ioannis Vranos =20 * Programming pages: http://www.noicys.freeurl.com * Alternative URL: http://run.to/noicys |