How do I get the size of a character array of unspecified size?
-
This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this:
char *cArray1 = new char[]; cin >> cArray1;
and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following.size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1);
(I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :) -
This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this:
char *cArray1 = new char[]; cin >> cArray1;
and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following.size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1);
(I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :)Use
strlen
function (don't forget to include )! David Z wrote: I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4 cArray1 is a pointer, not an array. It points at the first element of an array (null-terminated string). It is 4, because in Win32 pointers are 32-bit (4 * 8 bits - that's why you get 4)!Ñ There is only one MP Ð
-
Use
strlen
function (don't forget to include )! David Z wrote: I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4 cArray1 is a pointer, not an array. It points at the first element of an array (null-terminated string). It is 4, because in Win32 pointers are 32-bit (4 * 8 bits - that's why you get 4)!Ñ There is only one MP Ð
Maciej Pirog wrote: (don't forget to include )! Don't forget to include
<string.h>
of course! :-OÑ There is only one MP Ð
-
This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this:
char *cArray1 = new char[]; cin >> cArray1;
and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following.size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1);
(I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :)David Z wrote: sizeof() function always returns 4 because that's the size of a pointer. -c
For men use, if they have an evil turn, to write it in marble: and whoso doth us a good turn we write it in dust. -- Sir Thomas More
-
This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this:
char *cArray1 = new char[]; cin >> cArray1;
and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following.size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1);
(I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :)The only reason you get 4 (4 bytes) for sizeof
(cArray1)
is that sizeof actually measures the size of the cArray1, which is a pointer. And as you probably know.... a size of a pointer is 4 bytes (in your compiler anyway...). char*cArray1 =
new char[]; //very bad idea! No array size specified. instead, use fixed sized buffer: charcArray1[ARR_SIZE];
This one you can measure: size_tiArraySize =
sizeof(cArray1)
/sizeof(*cArray1)
;**--BlackSmith--
**/*The roof is on fire, we don't need no water, let the MF burn*/. BHG.
-
This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this:
char *cArray1 = new char[]; cin >> cArray1;
and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following.size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1);
(I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :)David Z wrote: I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? Use std::vector and its size() member instead. Tomasz Sowinski -- http://www.shooltz.com
** Putt knot yore thrust inn spel chequers. **
-
This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this:
char *cArray1 = new char[]; cin >> cArray1;
and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following.size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1);
(I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :) -
This is a newbie question but I somehow cannot get this part of my code to work. I have the user input into a character array of unspecified size like this:
char *cArray1 = new char[]; cin >> cArray1;
and then when I try to calculate how many elements is in the array, the sizeof() function always returns 4 for some reason. I'm calculating the number of elements by the following.size_t iArraySize = sizeof(cArray1)/sizeof(*cArray1);
(I also tried int as the datatype instead of size_t but no luck) I always get 4 as the return cause sizeof(cArray1) always gives me 4 and 4/1=4. I think sizeof() cannot be used with dynamically allocated arrays, so is there anyways to do this? :confused: Please give me some advice how so solve this question... Thanks! :)Well... Your array is actually a pointer, so the function sizeof() will always return 4 because that's the size of a pointer. Getting the number of elements in an array is a different thing. Now, I think you should do this some other way. First of all, why would you want to give the user unlimited input length? Just specify a limit and save yourself a headache. Second, maybe it could help to know the length of the array before allocating any memory... but, well, what is it you want to do? Aritosteles