0

I am trying to scan in a list of data from a text file, and that data is supposed to be put into an array of type double, and this must be done in a function outside of main. Therefore the pointer needs to be to the array in main, from the function actually scanning the data into the array.

The problem comes when I attempt to make the function return the array through a pointer like (assuming the prototype is already existing):

#include <stdio.h>
#include <stdlib.h>

#define SIZE 12    

int
main(void)
{
double data[SIZE];
int i;

readData(data, SIZE);
}



void
readData(double data[], int SIZE)
{
    FILE* fp;
    fp = fopen("data.txt", "r");
    int i;

    for (i = 0; i < SIZE; i++)
    {
        fscanf(read, "%lf", &data[i]);
        if(data[i] < 0)
            printf("Negative data at %d\n", i);
    }

    printf("The array is: ");
    for (i = 0; i < SIZE; i++)
    {
        printf("%f ", data[i]);
    }
fclose(fp);

    return;
}

This simply gives me a run time error and crashes, with no compile error at all. I'm guessing it's due to the fact that the fscanf is attempting to write to &*data[CONST] but I have no idea how else to get it to write to the pointer, and not just a local variable.

Is there some trick to getting file input into a function pointer?

user3857017
  • 265
  • 4
  • 14
  • 1
    readData(&data[CONST]) is calling read data with a pointer to memory just beyond the last element of the array. You need to get more familiar with how arrays work in C. – Almo Jul 20 '14 at 02:13
  • The most likely cause of a crash (with the code as shown at the time I am writing this!) is that the file failed to open. Do `if (fp == NULL) return`; or similar, before entering the loop. Also you should close `fp` after reading , if it did actually open; and you should check `fscanf` for failure (if it does not return `1` then it failed to read, so your program would go on to read garbage out of `data[i]`, possibly causing a crash). – M.M Jul 20 '14 at 03:20
  • also you need `#include `, and it'd be useful to show a complete program (i.e. include your definition of `size`). – M.M Jul 20 '14 at 03:21
  • I have #include and defined, I'll add them in above for completeness, and as of now I'm just hashing out the basics and I'm going back in to add error messages for failures for the file and read part of it, but thanks for the information of faster ways of doing that! – user3857017 Jul 20 '14 at 03:23
  • presumably `fscanf(read` should be `fscanf(fp` . Check that the code giving you the problem is actually the *exact* code you have posted (preferably copy-paste ) – M.M Jul 20 '14 at 03:48

3 Answers3

1

You need a little tweak to readData.

void readData(double data[], int CONST);

In main, call

readData(data, CONST);

Also, change the line

    fscanf(read, "%lf", &*data[i]);

to

    fscanf(read, "%lf", &data[i]);

Update

In the updated code, you have

void readData(data[], int CONST)

instead of

void readData(double data[], int CONST)
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • So at readData(double data[], int CONST); I don't need to do double* data[]? Is the pointer implicit with arrays as a function argument? Also, it's now telling me I need an ; , or ) before the numeric constant in that line. Thanks a lot though, that makes more sense than before! – user3857017 Jul 20 '14 at 02:35
  • @user3857017, can you please add to your post the current version of the code, how you are calling `readData`, where you are seeing the compiler error, and the verbatim copy of the compiler error message? – R Sahu Jul 20 '14 at 02:39
  • The code is updated now, minus other functions that I need for later on to manipulate the data I have in the array. The other functions are /**/ commented out so they aren't affecting the current code. The exact error I get is: "error: expected ';', ',' or ')' before numeric constant" – user3857017 Jul 20 '14 at 02:46
  • I have a feeling `CONST` is defined using a `#define`. That explains the problem. Replace `CONST` in `readData` by `size`. – R Sahu Jul 20 '14 at 02:56
  • @R Sahu It is, and it has to be for the project I'm doing. I changed it in the above code, and it was already SIZE in my actual code. I think it's all fixed as far as the pointer issue goes, but now I'm having a problem getting a counter for the elements in the array from 'readData'. – user3857017 Jul 20 '14 at 03:04
  • @user3857017 [see here](http://stackoverflow.com/questions/22677415/why-do-c-and-c-compilers-allow-array-lengths-in-function-signatures-when-they/22677793#22677793) for explanation of `[]` in function parameter – M.M Jul 20 '14 at 03:19
0

You declared int i twice, once in main() and once if your function, but it is unused in main().

When you pass an array to a function, writing to an element of the array is not writing to a local variable because when you pass an array, you are passing a pointer to the first element of the array. When you pass data, it is the same as passing &data[0]. Writing to data[i] is the same as writing to *(data + i), and since data is the address of the first element of your array, you are writing to the array itself, not a copy of it. Similarly, &data[i] gives the address of the i'th element of your original array, not a copy of it, so you can and should use that in your function.

And finally, you are still missing the data type of your array in your function declaration.

In your updated code, you did #define SIZE 12, but later used size. Since C is case sensitive, size and SIZE are not the same.

  • The reason for the duplicate i's is because I'm in the midst of trying to figure out a way to count the elements in the array, and then somehow relay that from the function readData back to main, the code has grown a bit from what is shown. Thank you for explaining the pointer thing. However it seems odd that arrays were set up to work like that, but oh well. – user3857017 Jul 20 '14 at 03:36
  • I figured it had something to do with that, but was just pointing it out. – m_duran Jul 20 '14 at 04:08
0

make sure data.txt file exist,

also for demo data.txt should have 12 lines of numbers,

also use file pointer fscanf(fp, "%lf", &data[i]);

mandar
  • 98
  • 6