0

Edit: I made some changes at my code.

I want to write a line fitting program by using the data from two .txt file. The code is as following:

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

int data_read(char programs_x[], char programs_y[]) {

    int i=0, j=0, k;
    int numProgs_x=0;
    int numProgs_y=0;
    char line_x[1024];
    char line_y[1024];

    FILE *file_x;
    FILE *file_y;
    file_x = fopen("data_x.txt", "r");
    file_y = fopen("data_y.txt", "r");

    while(fgets(line_x, sizeof line_x, file_x)!=NULL) {
        //check to be sure reading correctly
        //printf("%s", line_x);
        //add each filename into array of programs
        programs_x[i]=strdup(line_x); 
        i++;
        //count number of programs in file
        numProgs_x++;
    }

    while(fgets(line_y, sizeof line_y, file_y)!=NULL) {
        //check to be sure reading correctly
        //printf("%s", line_y);
        //add each filename into array of programs
        programs_y[j]=strdup(line_y); 
        j++;
        //count number of programs in file
        numProgs_y++;
    }

    fclose(file_x);
    fclose(file_y);
    return 0;

}

int main ( void ) {
    int i, j, k, n=1024;
    float s1=0,s2=0,s3=0,s4=0,a,d,b;
    char programs_x[1024], programs_y[1024];
    data_read(programs_x, programs_y);
    for(i=0;i<n;i++) {
        scanf("%f", &programs_x[k]);
    }
    for(i=0; i<n; i++){
        scanf("%f", &programs_y[k]);
    }
    for(i=0;i<n;i++) {
        s1=s1+programs_x[i];
        s2=s2+programs_x[i] * programs_x[i];
        s3=s3+programs_y[i];
        s4=s4+programs_x[i] * programs_y[i];
    }
    d=n*s2-s1*s1;
    a=(s2*s3-s1*s4)/d;
    b=(n*s4-s1*s3)/d;
    printf("\nThe values of a and b are : %f\t%f\n",a,b);
    printf("\nThe Required Linear Relation is : \n");
    if(b>0){
        printf("\ny=%f+%fx\n",a,b);
    }
    else {
        printf("y=%f%fx",a,b);
    }
    return 0;
}

When I try to compile this code, the compiler shows these error:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
test3.c:
Error E2349 test3.c 22: Nonportable pointer conversion in function data_read
Error E2349 test3.c 33: Nonportable pointer conversion in function data_read
*** 2 errors in Compile ***

How do I fix the errors? Where did I make mistakes in declaring and calling the data types? I'm pretty sure I declare programs_x and programs_y as char this time and not int.

HAKIM
  • 7
  • 7
  • `programs_x[i]` is a single character (i.e. its type is `char`). The result of `strdup(line_x)` is a *pointer* to a null-terminated string (i.e. its type is `char *`). Those are not the same thing. – Some programmer dude Oct 04 '17 at 14:24
  • 1
    And there are much worse problems, like you not actually *calling* `data_read`, only *declaring* its function prototype. Or attempting to pass arrays of wrong type. Besides getting a more up-to-date environment with a more up-to-date compiler, you should *really* [get a few good beginners books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and start over. – Some programmer dude Oct 04 '17 at 14:26
  • 1
    @BasileStarynkevitch. Assume that OP is working with what they have. No need to be smug about having a nice compiler on hand. – Mad Physicist Oct 04 '17 at 14:27
  • 1
    But learning a standard language is practically important – Basile Starynkevitch Oct 04 '17 at 14:27
  • @BasileStarynkevitch From what I remember, Borland C++ 5.5 was a fairly standard compliant compiler. Not sure if it supported C99, but at least it had full C90 and C++98 compliance, unlike earlier Borland compilers. This is the compiler which was used in the Borland C++ Builder RAD tool - it is still maintained under the name Embarcadero. Newer versions most definitely support C99. – Lundin Oct 04 '17 at 14:29
  • Btw the only thing that is C99 in this code is the `//` comments and those have been supported by Borland since the beginning of time. – Lundin Oct 04 '17 at 14:33
  • Actually almost everything is wrong and my advice is to start learning from the basics. – 0___________ Oct 04 '17 at 14:56
  • I'm voting to close this question as off-topic because OP needs to learn basic things, which can be found in any beginners C book – 0___________ Oct 04 '17 at 14:57
  • @Someprogrammerdude so how do I fix that? sorry, I'm still learning C – HAKIM Oct 10 '17 at 11:34

2 Answers2

2

The error could mean that the non-standard function strdup() is not supported. A C compiler does not need to support it, which is why it is a good idea to avoid that function. If the function is supported as a non-standard extension (it is part of POSIX), you might find it in the header <string.h> which you didn't include.

As for the cause of the rest of the errors, I have no idea, since those appear to originate from other files than the one you posted.

Lundin
  • 195,001
  • 40
  • 254
  • 396
2

The error is difficulty to pin down as we don't have line numbers but this line is definitely not right in your main:

data_read(char programs_x[], char programs_y[]);

To call a function you just list the variables and values you're passing to it like this:

data_read(programs_x, programs_y);

Which will no doubt cause more errors/warnings to be flagged as you declare programs_x and programs_y as arrays of int, but data_read is expecting arrays of char. So there's a conflict in what you think your function wants and what you're providing to it which you need to sort out.

Chris Turner
  • 8,082
  • 1
  • 14
  • 18
  • Good remark, but I think the compiler actually (incorrectly) treats that line as a (non-standard) prototype declaration, since the errors did not originate from that line. – Lundin Oct 04 '17 at 14:40