Hi there,

I'm having a problem with the code below:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void setArray( float [], const int Size );
void displayArray( const float * );

int main()
{
    const int arraySize = 20;
    float array[ arraySize ];

    // Initialise the array:
    setArray( array, arraySize );

    displayArray( array );
}

void setArray( float a[], const int Size )
{
    srand( time(0) );

    for ( int i = 0; i < Size; i++ )
        a[ i ] = 1 + rand() % 50;           // Number between 1 and 50
}

void displayArray( const float *aPtr )
{
    for ( ; *aPtr != '\0'; aPtr++ )
        cout << *aPtr << endl;
}

It's meant to generate and fill and array with 20 randomly generated FLOATING POINT numbers between 1 and 50 and then print the array using pointers. I get a very strange output:

20
50
14
7
35
24
45
26
26
31
38
13
3
29
32
29
30
5
8
5
4.59024e+33
5.95096e-39
3.21401e-39
2.8026e-44
5.95096e-39
NaN
3.21409e-39
5.88403e-39
1.4013e-45
3.86253e-39
3.86527e-39
6.27351e-39
3.21407e-39
NaN
3.21407e-39
4.48515e+33
5.87747e-39
3.86527e-39

Please help! Thanks :)

The fact that it's printing more than twenty numbers should be a strong indication that your condition for the loop in displayArray is incorrect. Specifically, it's written with a C-style string in mind, not an array of float. Try passing the size to displayArray like you do with setArray. That particular condition style won't work unless you have some kind of sentinel value at the end of the array (which you don't).

for ( ; *aPtr != '\0'; aPtr++ )

This isn't going to work. Your array is defined for twenty elements. Who knows what is stored in the memory locations beyond that? You can't assume anything. I would pass the function the address of the last element of the array so it knows when to stop.

Oh I see, thanks for the help!

Also, as a side Q, do you define both of these as C-style strings?

char *s1 = "Hello";
char s2[]= "Hi";

Well the problem seems to be that you expect the array to be terminated with a 0 . This is a mess.

What I think you have done is confuse the normal mechanism for strings e.g.

char* X="Fred";   // This gives    
// X[0]=='F' ... X[3]=='d' X[4]==0

with any other type of array.
e.g.

int X[]={1,2,3,4};
// X[3]==4, X[4] is undefined

But you really don't want to be using the last element of a float as a test value. (what would happen if say you used 0 and then one of your numbers was zero). So instead pass the size.

void displayArray( const float *aPtr,const int Size)
{
    for (int i=0;i<Size; i++ )
        cout <<aPtr[i] << endl;
}

>Also, as a side Q, do you define both of these as C-style strings?
Yes. The defining trait of a C-style string is the null character at the end. String literals guarantee its presence.

Awesome, thanks :) I was reading a chapter on C-style strings today, and then went back to normal arrays...definitely got confused!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.