C++ writing to myArray[0] and setting myInt = myFloat, why is this working?

Viewed 108

I'm playing with a function to get used to some C++ syntax. Now I think, I might have misunderstood:

I'm writing to a static (?) array I had defined as myArray[0] for experimenting.

So it seems NOT to be static, but sizeof(myArray) always returns 0 (?) but I can find mem address for each item (while I have no idea, how to get the number of items this way).

The other thing I don't understand, is why I can write myInt = myFloat?

So, what IS a static array? And should I better use <vector> for an array of undefined length?

(You could find the whole code here int2bin main.cpp)

#include <iostream>
//#include <regex>



int main()
{
  while(true) {
      //VARS
      unsigned int arrBin[0], intNum; //working, if set [0]! NOT static???
      unsigned int *pArr0 = &arrBin[0];
      unsigned int *pArr1 = &arrBin[1];
      std::cout << sizeof(arrBin) << '\n'; // 0 => sizeof() here items and not mem space?
      std::cout << pArr0 << '\n';// 0x7fff12de6c38
      std::cout << pArr1 << '\n';// 0x7fff12de6c3c
      int i; 
      float refNum;
     
      std::cout << "\n\nEnter a number to convert: "; 
      
      // GET INPUT
      std::cin >> refNum; // float
      intNum = refNum;    // get int of the dec for comparing. Why does this "int = float" work??? 
2 Answers
unsigned int arrBin[0]

The size of an array variable must not be 0. The program is ill-formed. Don't do this.

unsigned int *pArr1 = &arrBin[1];

Here, you use subscript operator beyond the bounds of the array (beyond one past last element), so the behaviour of the program is undefined. Don't do this.

(while I have no idea, how to get the number of items this way).

The number of items is 0 (or would be if that was allowed in the first place).

The other thing I don't understand, is why I can write myInt = myFloat?

You haven't even declared such identifiers.

I'm writing to a static (?) array I had defined as myArray[0] for experimenting.

By 'static' you probably mean 'fixed-sized'. static means something totally different, see https://www.geeksforgeeks.org/static-keyword-cpp/.

So it seems NOT to be static

It is not static, hence, it's not surprising that it's not static.

but sizeof(myArray) always returns 0

Its size is 0, as the size of 0 was specified. While this is not supported by the standards, it's possible that some compilers allow it.

but I can find mem address for each item (while I have no idea, how to get the number of items this way).

&arr[i] yields the address.

The other thing I don't understand, is why I can write myInt = myFloat?

Integer numbers are always real numbers, but real numbers are not always integer numbers. So, how would you store 0.5 as an integer? You could cast it or you could round it.

So, what IS a static array?

In the link I have provided you, it is mentioned that static variables in a function are variables for whom memory is allocated for the whole duration of a program. Hence, a static array is an array declared with the static keyword for which space are allocated for the whole lifecycle of your program. No such array was declared in your function.

And should I better use for an array of undefined length?

This is opinionated. You could create a pointer and navigate to items using pointer arithmetics, achieving the same behavior as with arrays, but without the length being fixed and with a slightly different syntax. Or you could use a library, a vector or whatever fits your task and taste.

Related