0

Hi,

have tryed and everything below gives me 0

sizeof(array) / sizeof(*array);
sizeof(array) / sizeof(array[0]);
sizeof(array) / sizeof(array data type);
//even the thing which i do not understant:
template< typename T, std::size_t N > inline
std::size_t size( T(&)[N] ) { return N ; }

I need to get the size of pointer array..

here is the whole code:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
//
double * dSM = NULL, * temp;
//
void GaunamDuomenis(string sF);
double TeigiamuVidurkis(double dMasyvas[]);
//
int main ()
{
	string sFP;
	cout << "Filename:\n";
	cin >> sFP;
	GaunamDuomenis(sFP);
	cout << "Arithmetical mean of positive numbers: " << TeigiamuVidurkis(dSM) << endl;
	return 0;
}
//
void GaunamDuomenis(string sF)
{
	string sS;
	ifstream ifsF(sF.c_str());
	if (!ifsF)
		{
		cout << "OOOps couldn't open..\n";
		}
	else
		{
		int i = 0;
		while (getline(ifsF, sS, ' '))
			{
			i++;
			temp = (double*) realloc(dSM, i * sizeof(double));
			if (temp != 0)
				{
				dSM = temp;
				dSM[i-1] = atof(sS.c_str());
				}		
			}
		}
}
//
double TeigiamuVidurkis(double dMasyvas[]) // Need to get length of dMasyvas
{
	double dS = 0, dV = 0;
	int iMI; // variable for the size of array
	cout << iMI << endl;
	for (int i = 0; i < 5; i++)
		{
		if (dMasyvas[i] > 0)
			{
			dS += dMasyvas[i];
			}
		}
	dV = dS / 5;
	return dV;
}

Edited by n0de: n/a

3
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by n0de
Featured Replies
  • you can not use sizeof to get the number of bytes allocated to an array. All sizeof(any pointer here) does is give you the size of a pointer -- on 32-bit compilers it will be 4. There are two solutions I can think of: 1. Use vector instead of array, … Read More

0

do you mean this array?

double * dSM = NULL

It depends on the size you pass to new.

sorry, mistyped (my stupid head..), i wanted to say that i need length of that array not the size (sorry for mistake)

p.s. yes, this array

1

you can not use sizeof to get the number of bytes allocated to an array. All sizeof(any pointer here) does is give you the size of a pointer -- on 32-bit compilers it will be 4.

There are two solutions I can think of:
1. Use vector instead of array, such as std::vector<double> ay; You can call vector's size() method to get the number of doubles it contains.

2. Pass the array size as another parameter to the functinon.

Comments
Thanks ;)
0

you can not use sizeof to get the number of bytes allocated to an array. All sizeof(any pointer here) does is give you the size of a pointer -- on 32-bit compilers it will be 4.

There are two solutions I can think of:
1. Use vector instead of array, such as std::vector<double> ay; You can call vector's size() method to get the number of doubles it contains.

2. Pass the array size as another parameter to the functinon.

thanks for the first soultion, i have already made second myself

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.