I have a small program.
I wanted to get the number of elements of the array p1. When I debug, I get 0. But I think it should be 6.
// ConsoleApplication3.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
//
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
using namespace std;
double array_concat(double p1[], double p2[])
{
double ans[2][6];
int i, j;
i = 0;
printf("%d\n", sizeof(p1) / sizeof(p1[0])); //is this wrong?
for (j = 0; j < sizeof(p1) / sizeof(p1[0]); j++){
ans[i][j] = p1[j];
}
i = 1;
for (j = 0; j < sizeof(p1) / sizeof(p1[0]); j++){
ans[i][j] = p2[j];
}
return ans[2][6];
}
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello\n";
int i;
double c[2][6];
double p1[6] = { 0, 1, 0, 0, 0, 0 };
double p2[6] = { 1, 1, 0, 0, 0, 0 };
c[2][6] = array_concat(p1, p2);
for (i = 0; i < 12; i++){
printf("%lf\n", c[i]); //is this wrong?
}
return 0;
}
What was wrong?
Edited code, so the p1,p2 and the return value of the function should better be poiters. I made it as in the example https://www.kompf.de/cplus/artikel/funcpar.html, but somehow it doesn't work. // ConsoleApplication3.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung. //
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
using namespace std;
double **array_concat(double *p1, double *p2)
{
double** ans = 0;
//ans = new double*[2];
//double ans[2][6];
int i, j;
i = 0;
printf("%d\n", sizeof(p1) / sizeof(p1[0])); //is this wrong?
for (j = 0; j < sizeof(p1) / sizeof(p1[0]); j++){
ans[i][j] = p1[j];
}
i = 1;
for (j = 0; j < sizeof(p1) / sizeof(p1[0]); j++){
ans[i][j] = p2[j];
}
return ans;
}
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello\n";
int i;
//double c[2][6];
double p1[6] = { 0, 1, 0, 0, 0, 0 };
double p2[6] = { 1, 1, 0, 0, 0, 0 };
//double *c;
double **c = array_concat(p1, p2);
for (i = 0; i < 12; i++){
printf("%lf\n", c[i]); //is this wrong?
}
return 0;
}