指针问题C++,未正确遍历
pointer issues in C++, not traversing correctly
int main()
{
const int x = 4;
int row, col, j;
int myArray[x][x], *xptr;
for (row = 0; row < 4; row++){
for (col = 0; col < 4; col++){
myArray[row][col] = (row * 4) + col;
}
}
printf("contents of myArray... n");
/* set xptr to the front of myArray*/
xptr = &(myArray[0][0]);
/*Print out the contents of the array. */
for (row = 0; row < 4; row++) {
for (col = 0; col < 4; col++) {
printf("%d ", *(xptr + (row * 4) + col));
}
}
printf("n");
/* Print out contents again, this time increment the pointer. */
printf(" Contents of the x array again... n");
/* Set xptr to the front of the x array. */
xptr = &(myArray[0][0]);
/*print out the contents of the array. */
for (j = 0; j< 16; j++);
printf("%d ", *xptr);
xptr++;
}
}
我无法使用指针作为打印地址重新打印数组。IE,根据我的教授笔记,我应该得到x 数组的内容...
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
再次显示 x 数组的内容...
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
但相反,我得到了
contents of myArray...
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
contents of the x array again...
0
然后它结束了。我已经尝试过玩所有东西,但我不确定是什么给了我这个错误
谢谢克里彻
for (j = 0; j< 16; j++);
看到行尾那个额外的分号了吗?
看起来你的牙套也不对,对于第二个循环。
相关文章:
- 有什么方法可以遍历结构吗
- 在循环中按顺序遍历成员变量
- 遍历模板参数
- 在遍历处理程序的向量时注册和注销处理程序
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 遍历并行数组以确定C++中的最大数字
- 遍历顺序由 std::文件系统directory_iterator给出
- 遍历链表时的无限循环
- 在使用 In Order 遍历成员函数时引发异常(堆栈溢出)时出现问题
- 循环遍历向量<string>并保持每个元素"count"时出现问题
- 如何修复关卡顺序遍历问题(二叉树)的无限循环错误
- 如果使用迭代器,如何解决遍历列表的问题
- 遍历 QHash 并提取重复值的键时遇到问题
- 遍历目录递归问题
- 指针问题C++,未正确遍历
- 使用链表转换稀疏矩阵(遍历问题)
- 线程内二叉树的插入或顺序遍历有什么问题
- 遍历类向量导致问题
- 遍历C++中的字符串列表,出了什么问题?
- 图形遍历问题