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?

5
  • 7
    sizeof does not do what you think it does. Commented Feb 15, 2017 at 9:00
  • you can modify i < sizeof(inputs); into i < 3 Commented Feb 15, 2017 at 9:01
  • 2
    You cannot pass an array by value in c++. It will decay into a pointer. Commented Feb 15, 2017 at 9:01
  • 1
    There's a list of good C++ books here. Commented Feb 15, 2017 at 9:03
  • 1
    change void neuron(double inputs[]) to template <std::size_t N> void neuron(double (&inputs)[N]) Commented Feb 15, 2017 at 9:04

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

It may not need <array>
oh, yes. Forgot to remove
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).

Comments

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;
}

Comments

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 .

3 Comments

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.
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.
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.