0

I am having a very simple function to input values. But I am getting a wired output. I get different values to what I am inserting? What am I doing wrong?

#include <iostream>
using namespace std;

void testFunc(float arr[], int sizeOfArray);

int main() {
    int sizeOfArray = 4;
    float arrA[] = {};
    float arrB[] = {};
    
    cout << "1st array VALUES" << endl;
    testFunc(arrA, 4);
    cout << "A -> ";
    for(int i=0; i<sizeOfArray; i++){
        cout << arrA[i] << " | ";
    }
    cout << endl;
    
    cout << "2nd array VALUES" << endl;
    testFunc(arrB, 4);
    cout << endl << "B -> ";
    for(int i=0; i<sizeOfArray; i++){
        cout  << arrB[i] << " | ";
    }
    cout << endl;
    
    return 0;
}

void testFunc(float arr[], int sizeOfArray) {
    for(int i=0; i<sizeOfArray; i++){
        cout << "Insert val " << i+1 << ": ";
        cin >> arr[i];
    }
}
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
Fergoso
  • 1,584
  • 3
  • 21
  • 44
  • This is not `C` code. Second, your arrays have 0 entries. What are you expecting to accomplish by accessing elements that are out-of-bounds? – PaulMcKenzie Jul 04 '21 at 06:59
  • @PaulMcKenzie Sorry about that I just updated few seconds before you corrected that. Thank yo0u :) – Fergoso Jul 04 '21 at 07:01
  • 1
    You have not initialized the array and trying to print the value, hence it returns or prints garbage value – Ashish M J Jul 04 '21 at 07:02
  • You should provide sample input, as well as the “bad” output you are seeing. – Ben Y Jul 04 '21 at 07:03
  • @AshishMJ Thanks you :) So `float arrA[] = {}` is the fault and `float arrA[4];` is the fix? – Fergoso Jul 04 '21 at 07:03
  • 1
    Might also want to set a default size if this isn’t a dynamically allocated array. – Ben Y Jul 04 '21 at 07:04
  • 1
    @Fergoso -- Arrays are fixed size in C++. Once the size is 0, it will remain 0. Second, C++ has something called `undefined behavior`. Your code could have crashed if built with different compilation options or built using a different compiler. Does the C++ book you're learning from have any examples like what you posted? I bet it doesn't. Learning C++ cannot be done from cheat sheets or off-the-cuff. It is one of the most difficult languages to learn, and only using good peer-reviewed C++ books is the way to learn such a language. – PaulMcKenzie Jul 04 '21 at 07:04
  • Thank you so much all!!!!! I understand the cause now. :) +1 to all 3 of you!!! – Fergoso Jul 04 '21 at 07:07

1 Answers1

0

The float arrA[] = {} creates an array of floats with 0 size. When you iterate over it, you access an out of bound memory in here:

for(int i=0; i<sizeOfArray; i++);

You can list initialize an array and not specify the size of an array, using braces {...}. But in such case, the size is deduced from the elements in the list. You have empty list.

For example:

float arr[] = {}; // Empty
float arr[4]; // Four elements, all default initialized
float arr[] = {3.14, 1.61}; // Two elements, specified values
float arr[4] = {3.14, 1.61}; // Four elements, last two are value initialized

You will benefit from taking a look at these articles:

Why aren't variable-length arrays part of the C++ standard?

C++ Containers Library

rawrex
  • 4,044
  • 2
  • 8
  • 24