PDA

View Full Version : Debugging...


ChefNinja
06-25-2002, 08:47 AM
This is not my code, a friend of mine asked me to post this here for him... I don't know why he couldn't just do it himself.. but whatever.

I don't know enough C to fix this for him, so maybe you guys can help...



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

void grabData(int dat);
int processData(int *dat);

/* This is where it all kicks off */
int main() {
/* Array to store the file data */
int elements[30];

/* Pointer to element array */
int *data;

/* Result */
int result = 0;

/* Initialise the element array */
memset(elements, 0, (sizeof(integer) * 30));

data = &elements[0];

printf('Simple data processing script\n');
printf("by slyfx [www.slyfx.com]\n\n");

/* Grab the data from the file */
grabData(*data);

/* Process the data and grab the result */
result = processData(data);

printf("The result of the processing is %s\n", result);

return(0);
}

/* Opens the data file and read it in */
void grabData(int *dat) {
int loop;
FILE *fp;

fp = fopen("data.sly", "r");
if (fp == NULL) {
perror("Failed to open file for reading!\n");
exit(1);
}

/* Grab the lines */
for(loop = 0; loop < 30; loop++) {
if (fscanf(fp, "%d", dat[loop]) == EOF) {
perror("Fatal: not enough data!\n");
exit(1);
}
}

*(dat + 1) = 20;

fclose(fp);
}

/* Process the data */
int processData(int *dat) {
int loop;
int res = 0;

for(loop = 0; loop < 30; loop++) {
res += (dat[loop] ? (dat[loop] - 50) : 69) ? dat[loop] : 69;
while (res > 300000) {
res -= dat[loop] ? dat[loop] : 169;
}
}

return *res;
}



Here's the error messages given to me by the compiler when I run it...



(27) : warning C4013: 'memset' undefined; assuming extern returning int

(27) : error C2065: 'integer' : undeclared identifier

(46) : warning C4028: formal parameter 1 different from declaration

(81) : error C2100: illegal indirection



Really, I have no idea what is wrong. I am so newb :\

kmj
06-25-2002, 11:17 AM
I don't think this is really debugging. I don't think it's a bug until it's in actual compiled code. Rather, you've got syntax errors in your code.

Now, a little tip: the number at the start of each line is the line number. So if you go to that line number in your code, you'll see the line where the compiler thinks something is wrong.

1st problem: memset is not defined: memset is defined in memory.h (or string.h), you need to include that file.

2nd problem: integer undeclared: this should be pretty obvious, considering you're using "int" (not "integer") to declare integer values all through the code. Change "integer" to "int".

I don't know which lines 46 and 81 are, so if you want more help with them, you can let me know.

If I seem a little abrasive, it's because I'm suspicious <gr> that I'm inadvertantly doing someone's homework for them, which would really piss me off. ..and who's your friend? Does he have an account here? Seems weird.

Strike
06-25-2002, 01:07 PM
Yeah, although I'm wary of doing someone's CS assignments for them as well, here's help on the last two.

Third error: you defined grabData to accept an integer pointer up at the top, and then you say it accepts an integer at the bottom. Pick one declaration for a function and stick with it.

Last error: you are using the dereferencing operator (*) on something that isn't a pointer (which is the only thing you can dereference)

ChefNinja
06-25-2002, 05:37 PM
I don't understand when you say



Third error: you defined grabData to accept an integer pointer up at the top, and then you say it accepts an integer at the bottom. Pick one declaration for a function and stick with it.


No, you are not doing any of my homework, I'm only in highschool. I pointed out earlier, I don't know C ;\

kmj
06-25-2002, 06:07 PM
well, tell your friend that the function prototype doesn't match the function definition:


....
/* prototype */
void grabData(int dat);

/* definition */
void grabData(int *dat) {