problem with sizeof operator

前端 未结 6 1825
遥遥无期
遥遥无期 2020-12-01 15:24

As i want to find array size dynamically in function, i used sizeof operator. But i got some unexpected result. here is one demo program to show you, what i want to do.

6条回答
  •  温柔的废话
    2020-12-01 15:49

    Inside getSize(), you're getting size of pointer, which is 8 bytes (since you're probably running 64-bit OS). In main(), you're getting size of array.

    If you want to know array size, pass result of sizeof(S) as additional argument to getSize().

    More alternatives would be using some container (like std::vector) or turning function into template function, as Nawaz proposed.

提交回复
热议问题