0

I am using a function to create an array of structures for certain information. I get this error and I'm guessing that the char buf[] variable is the one causing the error.

Is there a way I can convert the buf variable in order for it to be compatible with the strsub function? The last strsub call is the one that is giving me the error.

The salary is a double variable and the buf is a char[].

void strsub(char buf[], char sub[], int start, int end) {
int i, j;
double x;

for (j = 0, i = start; i <= end; i++, j++) {
    sub[j] = buf[i];
}
sub[j] = '\0';

while (!feof(fp)) {
    fgets(buf, MAX, fp);
    strsub(buf, dataArray[i].first, 0, 6);
    strsub(buf, dataArray[i].initial, 8, 8);
    strsub(buf, dataArray[i].last, 10, 18);
    strsub(buf, dataArray[i].street, 20, 35);
    strsub(buf, dataArray[i].city, 37, 47);
    strsub(buf, dataArray[i].state, 49, 50);
    strsub(buf, dataArray[i].zip, 52, 56);
    strsub(buf, dataArray[i].age, 58, 59);
    strsub(buf, dataArray[i].sex, 61, 61);
    strsub(buf, dataArray[i].tenure, 63, 63);
    strsub(buf, dataArray[i].salary, 65, 69);




}
}
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
Pahjay
  • 17
  • 6

2 Answers2

0

You are trying to pass a double to a function that expects a string to write into; this obviously won't work, you'll have to copy that piece of input string to a suitable buffer, and then parse it to a double (using e.g. strtod).

On a personal note, this is a quite serious mistake, you should really review your C manual before going further.

apandit
  • 3,304
  • 1
  • 26
  • 32
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • I'll look into this. Thank you! My professor told us to use this function without actually explaining how it worked. I am trying to figure out why its not working, unfortunately. – Pahjay May 07 '14 at 00:00
-4

You can use GDB to help you. For example, you program name demo.c

$ gcc -g -o demo demo.c
$ gdb demo

Type "apropos word" to search for commands related to "word"...
Reading symbols from demo...done.
(gdb) 

And then, you can set some break points and use step or next to observe the memory.

Gapry
  • 253
  • 1
  • 7
  • 20