使用for循环和指针打印浮点数组
Printing float array using for loop and pointers
我开始在数组和指针的CPP。
我试图打印一个浮点数数组,但是我超出了浮点数数组。
我怎么能停止打印浮点数组一旦我击中结束?
// all about arrays and pointers
#include <iostream>
using namespace std;
int main (int argc, char * argv []) {
float inputs1 [] = { 12 };
float inputs2 [] = { 12 , 4 };
cout << "sizeof(inputs1)" << sizeof(inputs1) << endl;
cout << "sizeof(inputs2)" << sizeof(inputs2) << endl;
float *it = inputs2;
cout << *it << endl;
it++;
cout << *it << endl;
cout << endl;
cout << "sizeof(inputs1): " << sizeof(inputs1) << endl;
cout << "sizeof(inputs2): " << sizeof(inputs2) << endl;
cout << "start address: " << &inputs2 << endl;
cout << "end address: " << &inputs2[sizeof(inputs2)] << endl;
for (float *it2 = inputs2; it2!=&inputs2[sizeof(inputs2)]; it2++) {
cout << "it2 " << it2 << endl;
cout << "*it2 " << *it2 << endl;
}
}
sizeof
返回其参数的大小(以字节为单位) - 不是其中的元素数1。
要得到元素的数量,你必须用sizeof(inputs1)
除以sizeof(inputs1[0])
。
1除非每个元素是一个字节,例如char
相关文章:
- 动态分配的聊天数组打印缺失的数据和空
- 对象数组打印空白字符串
- 数组打印"random"值时出现问题
- 数组打印出垃圾值
- 数组打印奇怪的字符,例如 ∟ @
- 测量我的随机数数组打印的时间始终显示为 0 秒
- 数组打印出错误的数字
- 从对象数组打印字符串对象
- 您好,如何防止此函数使用质数数组打印 1
- 如何从主内部的数组打印总计
- 如何将数组打印为矢量形式
- 有人能给我一些关于在C++中使用数组打印出丰富、完美和不足的数字的提示吗
- 为什么我的C++数组打印相同的值
- 如何阻止我的数组打印地址
- 将值输入到结构数组 + 打印输出
- C++11 为什么 cout 从布尔数组打印大整数
- 从字符串数组打印其中一个字符串时出现空指针赋值错误
- 将无符号字符数组打印为十六进制
- 缩短的数组打印错误的结果
- C++中的字符串数组:打印额外的字符