-1

Given a pointer to the first element of an array, how can I get the size of the array? For example, if I were given a but not arr here:

int arr[]={1,2,3};
int* a=arr;

Also could anyone explain a bit about the properties of "\0" in the array?

  1. My understanding of "\0" is that it marks the end of the array. Is that correct? And is there more I should pay attention to?
  2. Does "\0" work for all types of arrays or only char type? If it works only for char type, are there any similar symbols for other types of arrays?
  3. Will '\0' be automatically inserted at the end of the array? Or it will be automatically inserted only under some specific conditions?
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Beluga
  • 9
  • 2
    Sorry, but C++ does not work this way. A pointer is a pointer, and does not indicate how many values it's pointing to. The number of values a pointer is pointing to must be tracked separately. The trailing `'\0'` character is a convention that's used to denote the length of C-style strings only. – Sam Varshavchik Jan 05 '22 at 01:46
  • 2
    Searching "c++ size of array from pointer" should be informative. For instance, https://stackoverflow.com/questions/19894686/getting-size-of-array-from-pointer-c – TheUndeadFish Jan 05 '22 at 01:49
  • 1
    `'\0'` is a `char` with the value 0. Nothing more. In a C-style string it's used to mark the end of the string, and string literals (like `"abcd"`) have a `'\0'` as their final character so that the various C-style string functions can be applied to them. But once you get outside the realm of `char` types there's no magic; if you want to treat some `int` value as a delimiter, go for it, but you'll have to write the code yourself. It's usually not a good idea. – Pete Becker Jan 05 '22 at 01:53
  • 1
    *Will '\0' be automatically inserted at the end of the array? Or it will be automatically inserted...* -- Why should the programmer have to pay the cost of having 0's put into the data automatically if they don't want 0's? That's the philosophy of C++ -- if you want 0's in the array, *you* have to put them there, not the compiler. – PaulMcKenzie Jan 05 '22 at 01:56
  • The simplest is `1[&arr]-arr` – QuentinUK Jan 05 '22 at 02:04
  • 1
    @QuentinUK `std::size(arr)` is simpler and easier to understand. But the premise of the question was how to get the size without being given `arr`. – eerorika Jan 05 '22 at 02:05
  • 1
    Useful reading: [What is the difference between 'a' and "a"?](https://stackoverflow.com/questions/11279126/what-is-the-difference-between-a-and-a) Then come back to your question -- when you wrote `"\0"`, did you mean `'\0'`? – JaMiT Jan 05 '22 at 02:12

2 Answers2

2

Given:

int arr[]={1,2,3};
int* a=arr;

you cannot get the size of the array given just the value of a. The implicit conversion from int[3] to int* is lossy; it discards any information about the size of the array, and yields a pointer to the array's initial element.

The null character '\0' marks the end of a C-style string (a null-terminated character string) and can be used to determine the length of the string -- not the size of the array that contains it.

There is no such convention for arrays of non-character types. Of course you can define such a convention in your own code, but you're going to have problems if 0 is a valid element type.

If you need to do this kind of thing, you're better off using C++'s standard library. For example, the type std::vector<int> hold a sequence of int values and keep track of its length for you. If you really need to write C-style code that deals with arrays via pointers to their elements, you'll need to track the array length yourself.

String literals do automatically provide a terminating null character, but again that applies only to strings, not to arrays in general.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

I wonder given a pointer to the first element of an array, how can I get the size of the array? eg. int arr[]={1,2,3};int* a=arr, given a(not arr), how to get the size of the array?

You generally cannot get the size using only the pointer.

1.my understanding of "\0" is that it marks the end of the array, is that correct?

You can choose any value to represent the end of an array.

"\0" isn't typically used to represent the end of an array.

'\0' i.e. the null character is often used to represent the end of a character string. Such string is called "null terminated string".

  1. Does "\0" work for all types of arrays or only char type?

"\0" is a string literal. Strings are arrays of chars. An element of an array of chars is not an array of chars, but a char.

  1. Will '\0' be automatically inserted at the end of the array?

No, '\0' will not be inserted at the end of the array. Not all arrays even contain elements of a type that a char could be converted into. However, there is an implicit '\0' at the end of every string literal.

eerorika
  • 232,697
  • 12
  • 197
  • 326