需要一些关于使用指针添加C++数组的建议

Need some advice with C++ array addition using pointers

本文关键字:数组 C++ 添加 指针 于使用      更新时间:2023-10-16

我正在尝试使用指针获取两个数组的和,但当输出都为零时,我该怎么办?

如果有更好的方法,我想知道

这是代码

更新

#include  < cstdio >  
#include < iostream >
using namespace std;
unsigned i;
int main(){
/* Array`s input */
short A[3];
for( i = 0 ; i < 3 ; i++ ){
printf("Insert number for [A]: ");
scanf("%hd",&A[i]);
}
printf("n");
short B[3];
for( i = 0 ; i < 3 ; i++ ){
printf("Insert number for [B]: ");
scanf("%hd",&B[i]);
}
short C[3];
// Pointers
short *punt_A, *punt_B, *punt_C;
punt_A = &A[0];
punt_B = &B[0];
punt_C = &C[0];
// Addition
for( i = 0 ; i < 3 ; i++ ){
C[i]=punt_A[i]+punt_B[i];
}
printf("nnArray addition = { %d, %d, %d }n", *punt_C, *(punt_C + 1), *(punt_C + 2));

return 0;
}

记住short A[3];将允许您合法访问A[0]。。A[2]

在中

for( i = 3; i-- ; )
{
/* i-- is a tricky way of entering the loop
* This checks i for the condition, and passes (i-1, the current value
* of i) to the loop
* Though the method is smart I think it is less readable
* There is no access violation here, forgive my previous comment :(
*/
printf("Insert number for [A]: ");
scanf("%hd",&A[i]);  // Remember %hd for short.
}

此处的加法可以更好地表示为:

for( i = 0; i<3;i++){ 
/* I changed the forloop structure which may be used 
* for reading the arrays too. In fact this has no surprises.
*/
C[i]=punt_A[i]+punt_B[i]; // Or *(punt_A+i) + *(punt_B+i)
}

关于,

printf("nnArray addition = { %d, %d, %d }n", *punt_A, *punt_B, *punt_C );

这分别只打印A[0],B[0],C[0],并且您的格式说明符"%d"与short类型不匹配。正确的说明符是"%hd"。使用循环打印结果数组。

punt_Apunt_Bpunt_C都向左指向一个元素,使其超过各自数组的末尾,例如punt_A现在指向A[3]。数组是沿着字边界在堆栈上分配的,因此堆栈上的每个数组后面都有"死区"。在Windows调试版本中,未初始化的区域用0xcccccccc标记,所以我看到-13108,它是(short)(0xcccccccc)。在发布版本中,您只会看到以前在该内存地址中留下的内容,在许多情况下,该地址只是零。

当我看到for (i = 3; i--; )时,我拍了两张。您已将i--放在检查条件正常的位置。使用for (i = 2; i >= 0; i--; )更清楚。它实际上是偶然工作的,因为当评估条件i--时,它会获取i并检查该条件是否为非零。检查后,i递减并进入循环。当i变为零时,循环终止。所以在循环中,你会看到i=2,1,0。递减后运算符在任何评估之后应用,例如,如果int n = 1;,则n + n--;返回2,而不是1。CCD_ 19在对表达式求值之后递减。

几件事:

  • 您的数组和指针被声明为short,但scanf()/printf()说明符是%d,意思是int。典型地,short是16位整数,而int是32位整数。这可能会造成奇怪的影响
  • 您应该在2而不是3开始循环。3已超出界限
  • 在最后一个循环结束时,所有指针都将指向数组末尾之外,但无论如何都要将它们传递给printf()(这实际上是未定义的行为)
  • 事实上,你不想输出C[]的内容——加法结果吗?为什么在那里使用punt_Apunt_B

现在这是对程序的粗略修复

#include <stdio.h>
#include <iostream>
using namespace std;
unsigned i;
int main(){

/* Array`s inputs */
int *A = new int[3];
for( i = 3 ; i-- ; ){
cout << "Insert number for [A]: " << endl;
scanf("%d",&A[i]);
}
printf("n");
int* B = new int[3];
for( i = 3; i-- ; ){
cout << "Insert number for [B]: " << endl;
scanf("%d",&B[i]);
}
int* C = new int[3];
// Pointers
int *punt_A, *punt_B, *punt_C;
punt_A = A;
punt_B = B;
punt_C = C;
// Addition
for( i = 3; i--; ){
*punt_C = *punt_A + *punt_B;
punt_A++;
punt_B++;
punt_C++;
}
for(int i = 0; i < 3; i++)
{
cout << *C << endl;
C++;
}
delete [] A;
delete [] B;
delete [] C;
return 0;
}
  • 在您的程序中:

问题是:初始化一个短路数组。short的大小为2字节。但是,您正在加载数字,应为4字节的整数%d。因此,您加载了这2个字节,而其他两个字节则被忽略了。我编译它时没有墙-迂腐的旗帜,仍然得到警告:

main.cpp: In function ‘int main()’:
main.cpp:17:21: warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘short int*’ [-Wformat=]
scanf("%d",&A[i]);
^
main.cpp:28:21: warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘short int*’ [-Wformat=]
scanf("%d",&B[i]);

现在要解决这个问题,让它们都是int。但你要求更好的解决方案。如果给您两个数组,并且您必须对它们求和,并且由于您使用的是c++11,对您来说,使用容器可能更容易。

# include <stdio.h>
# include <iostream>
#include <vector>
#define COUNT 3
using namespace std;
unsigned i;
int main()
{
/* Array`s inputs */
vector<int> A, B;
int worker;
for( i = COUNT ; i-- ; )
{
cout << "Insert number for [A]: " << endl;
cin >> worker;
A.push_back(worker);
}
for( i = COUNT ; i-- ; )
{
cout << "Insert number for [B]: " << endl;
cin >> worker;
B.push_back(worker);
}
for(int i = 0; i < COUNT; i++)
{
A[i] = A[i] + B[i];
}
cout << "summed up" << endl;
for(vector<int>::iterator it = A.begin(); it != A.end(); it++)
{
cout << *it << endl;
}
return 0;
}

现在注意,我为你的计数做了一个定义。此外,我再次使用aray A进行输出,节省了空间。问题是,该向量本身对于push_back()具有O(n)复杂度(除非你重新计算大小,但这需要知道输入的确切数量,这在未来可能不会是一种情况,你可能会加载到EOF)。为了消除这种情况并使程序更快,可以使用列表来获得更好的性能。

但是,如果您想使用指针,请使用迭代器。:]

(请注意,我的代码不受失败保护,这意味着如果你写一封信,它会做一些奇怪的事情,但我概述了这个想法:]