0

I am unable to get the following code working properly.

#include <iostream>

using namespace std;

void neuron(double inputs[])
{
    for (int i = 0; i < sizeof(inputs); i++) {
        cout<<inputs[i];
    }
}

int main()
{
    double inputs[] = {10,12,12};
    neuron(inputs);
    return 0;
}

I want to pass an array to the function neuron and then print the elements. I am unable to do that. The code is giving me garbage values. What is wrong with this code?

Voicu
  • 56
  • 5
Nikhil Raghavendra
  • 1,570
  • 5
  • 18
  • 25

4 Answers4

5
#include <iostream>

using namespace std;

template<size_t size>
void neuron(double (&inputs)[size])
{
    for (size_t i = 0; i < size; i++) {
        cout<<inputs[i]<<endl;
    }
}

int main()
{
    double inputs[] = {10,12,12};
    neuron(inputs);

    return 0;
}

example

user5821508
  • 332
  • 2
  • 10
0

If you pass inputs to neuron then neuron will only see the start address of the array, it will not see the length of the array. You need to pass the length as an extra parameter. Also not that sizeof gives you the size of the array in bytes, not in number of elements.

void neuron(double inputs[], size_t len)
{
    for (int i = 0; i < len; i++) {
        cout<<inputs[i];
    }
}

int main()
{
    double inputs[] = {10,12,12};
    neuron(Inputs, sizeof inputs / sizeof inputs[0]);
    return 0;
}

As your question is tagged C++ you better should use C++ STL types like std::vector (for arrays of dynamic size) or std::array (for arrays of fixed size).

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
0

Just try the following code

#include <iostream>
using namespace std;


void neuron(double inputs[],int length)
{

    for (int i = 0; i <length; i++) {
        cout<<inputs[i];
    }
}

int main()
{
    double inputs[] = {10,12,12,22,22};
    neuron(inputs, (sizeof(inputs)/sizeof(*inputs)));
    return 0;
}

The sizeof() returns the size of primitive type rather than the size of array .

manish kumar
  • 4,412
  • 4
  • 34
  • 51
  • `sizeof(inputs)` is equal to `sizeof(double)*3` in `main`, but to `sizeof(double*)` in `neuron`. Arrays decay to pointers when you pass them to a function. – barak manos Feb 15 '17 at 09:07
  • That only works in this specific case where the array has 3 elements. `sizeof(inputs) - 1` always results in 3 (or 7 or something else, depending on the architecture), regardless of the array's length. It doesn't have anything to do with the array length, it returns the size of a pointer type minus one byte, which just happens to be the array's length of 3 in this particular case. – leemes Feb 15 '17 at 09:08
  • 1
    Why do you want someone to down-vote your answer for you??? Wouldn't it be simpler to just fix it or delete it altogether? – barak manos Feb 15 '17 at 09:11
0

You are passing pointer of first element of the arrray so size will be size of double.
This is how I would to it:

#include <iostream>

using namespace std;

void neuron(double inputs[], int size)
{
    for (int i = 0; i < size; i++) {
        cout<<inputs[i];
    }
}

int main()
{
    double inputs[] = {10,12,12};
    neuron(inputs, sizeof(inputs)/sizeof(double));
    return 0;
}
Irakli
  • 126
  • 1
  • 12