Redirecting input from file

A

Army1987

I've got one more error that I can't seem to get worked out. I have a
segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.

Here's the most recent version of my code. Everything else seems to
work properly.
Absolutely not.
#include <stdio.h>
#include <stdlib.h>
#include <math.h> Why?

int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5;
I'd use size_t (if for some reason I had to avoid the preprocessor)...
const int errorcode = -1;
Why the hell do you need it?
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;
These casts don't buy anything.
FILE *fpin;
FILE *fpout;
if (argv < 3) {
fprintf(stderr, "Usage:\t%s infile outfile\n", argv[0]);
return EXIT_FAILURE;
}
/* or fpin = stdin, fpout = stdout, or ask the user what to do...*/
fpin = fopen(argv[1],"r");
if (fpin == NULL) { /* handle... */ }
fpout = fopen(argv[2],"w");
same as above...
/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
Why the hell sizeof(int)? And why do you need the cast?
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);

/* Calculation Section */
while(condition != EOF) {
while (condition < 2)
It is well possible that the file is ill-formed and you'll be able
to read date[count] but not precipvals[count]...
condition = fscanf(fpin,"%ld,%f", &date[count],
&precipvals[count]);

if(count == 0) {
precip3hrs[count] = precipvals[count];
}
if(count == 1) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count > 1) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
count++;
if (count >= arraylength) break;
}

/* Output section */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
Couldn't you bring these two out of the loop and make the loop
count from 2?
if(count > 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}


/* Write to file */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count > 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);
if (fclose(fpout) != 0)
perror("Something went wrong");
printf("Testing\n");

/* Deallocation */
free(precipvals);
Why didn't you use free((void *)precipvals); this time? :)
 
D

dmoran21

segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.
Here's the most recent version of my code. Everything else seems to
work properly.

Absolutely not.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Why?

int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5;

I'd use size_t (if for some reason I had to avoid the preprocessor)...
const int errorcode = -1;

Why the hell do you need it?
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;

These casts don't buy anything.
FILE *fpin;
FILE *fpout;

if (argv < 3) {
fprintf(stderr, "Usage:\t%s infile outfile\n", argv[0]);
return EXIT_FAILURE;}

/* or fpin = stdin, fpout = stdout, or ask the user what to do...*/
fpin = fopen(argv[1],"r");

if (fpin == NULL) { /* handle... */ }
fpout = fopen(argv[2],"w");

same as above...
/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);

Why the hell sizeof(int)? And why do you need the cast?
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);
/* Calculation Section */
while(condition != EOF) {

while (condition < 2)
It is well possible that the file is ill-formed and you'll be able
to read date[count] but not precipvals[count]...
condition = fscanf(fpin,"%ld,%f", &date[count],
&precipvals[count]);
if(count == 0) {
precip3hrs[count] = precipvals[count];
}
if(count == 1) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count > 1) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
count++;

if (count >= arraylength) break;
/* Output section */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}

Couldn't you bring these two out of the loop and make the loop
count from 2?


if(count > 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
/* Write to file */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count > 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);

if (fclose(fpout) != 0)
perror("Something went wrong");> printf("Testing\n");
/* Deallocation */
free(precipvals);

Why didn't you use free((void *)precipvals); this time? :)
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
free(date);
date = (long*)NULL;
return 0;
}


I'm just going by the way I was taught and how my book does it. I'm
not a professional programmer.

Dave
 
D

dmoran21

Please do not use tabs - use spaces, 3 at a time. Otherwise you
can get the above mess, or, on some systems, all leading tabs are
deleted.
Thanks for the tip.

Dave
 
A

Army1987

segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.
Here's the most recent version of my code. Everything else seems to
work properly.

Absolutely not.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Why?

int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5;

I'd use size_t (if for some reason I had to avoid the preprocessor)...
const int errorcode = -1;

Why the hell do you need it?
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;

These casts don't buy anything.
FILE *fpin;
FILE *fpout;

if (argv < 3) {
fprintf(stderr, "Usage:\t%s infile outfile\n", argv[0]);
return EXIT_FAILURE;}

/* or fpin = stdin, fpout = stdout, or ask the user what to do...*/
fpin = fopen(argv[1],"r");

if (fpin == NULL) { /* handle... */ }
fpout = fopen(argv[2],"w");

same as above...
/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);

Why the hell sizeof(int)? And why do you need the cast?
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);
/* Calculation Section */
while(condition != EOF) {

while (condition < 2)
It is well possible that the file is ill-formed and you'll be able
to read date[count] but not precipvals[count]...
condition = fscanf(fpin,"%ld,%f", &date[count],
&precipvals[count]);
if(count == 0) {
precip3hrs[count] = precipvals[count];
}
if(count == 1) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count > 1) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
count++;

if (count >= arraylength) break;
/* Output section */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}

Couldn't you bring these two out of the loop and make the loop
count from 2?


if(count > 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
/* Write to file */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count > 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);

if (fclose(fpout) != 0)
perror("Something went wrong");> printf("Testing\n");
/* Deallocation */
free(precipvals);

Why didn't you use free((void *)precipvals); this time? :)
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
free(date);
date = (long*)NULL;
return 0;
}


I'm just going by the way I was taught and how my book does it. I'm
not a professional programmer.
You asked why the program segfaults. Most of my comments are
possible reasons why it does. And I can't see the reason why even
the most unprofessional programmer in the world would, e.g.,
allocate space for 5 ints and write 5 floats, or 5 longs, in it.
 
D

dmoran21

<[email protected]> ha scritto nel messaggioI've got one more error
that I can't seem to get worked out. I have a
segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.
Here's the most recent version of my code. Everything else seems to
work properly.
Absolutely not.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Why?
int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5;
I'd use size_t (if for some reason I had to avoid the preprocessor)...
const int errorcode = -1;
Why the hell do you need it?
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;
These casts don't buy anything.
FILE *fpin;
FILE *fpout;
if (argv < 3) {
fprintf(stderr, "Usage:\t%s infile outfile\n", argv[0]);
return EXIT_FAILURE;}
/* or fpin = stdin, fpout = stdout, or ask the user what to do...*/
fpin = fopen(argv[1],"r");
if (fpin == NULL) { /* handle... */ }
fpout = fopen(argv[2],"w");
same as above...
/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
Why the hell sizeof(int)? And why do you need the cast?
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);
/* Calculation Section */
while(condition != EOF) {
while (condition < 2)
It is well possible that the file is ill-formed and you'll be able
to read date[count] but not precipvals[count]...
condition = fscanf(fpin,"%ld,%f", &date[count],
&precipvals[count]);
if(count == 0) {
precip3hrs[count] = precipvals[count];
}
if(count == 1) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count > 1) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
count++;
if (count >= arraylength) break;
}
/* Output section */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
Couldn't you bring these two out of the loop and make the loop
count from 2?
if(count > 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
/* Write to file */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count > 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);
if (fclose(fpout) != 0)
perror("Something went wrong");> printf("Testing\n");
/* Deallocation */
free(precipvals);
Why didn't you use free((void *)precipvals); this time? :)
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
free(date);
date = (long*)NULL;
return 0;
}
I'm just going by the way I was taught and how my book does it. I'm
not a professional programmer.

You asked why the program segfaults. Most of my comments are
possible reasons why it does. And I can't see the reason why even
the most unprofessional programmer in the world would, e.g.,
allocate space for 5 ints and write 5 floats, or 5 longs, in it.


I got it to work now.

Dave
 
D

dmoran21

<[email protected]> ha scritto nel messaggionews:[email protected]...
<[email protected]> ha scritto nel messaggioI've got one more error
that I can't seem to get worked out. I have a
segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.
Here's the most recent version of my code. Everything else seems to
work properly.
Absolutely not.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Why?
int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5;
I'd use size_t (if for some reason I had to avoid the preprocessor)...
const int errorcode = -1;
Why the hell do you need it?
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;
These casts don't buy anything.
FILE *fpin;
FILE *fpout;
if (argv < 3) {
fprintf(stderr, "Usage:\t%s infile outfile\n", argv[0]);
return EXIT_FAILURE;}
/* or fpin = stdin, fpout = stdout, or ask the user what to do...*/
fpin = fopen(argv[1],"r");
if (fpin == NULL) { /* handle... */ }
fpout = fopen(argv[2],"w");
same as above...
/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
Why the hell sizeof(int)? And why do you need the cast?
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);
/* Calculation Section */
while(condition != EOF) {
while (condition < 2)
It is well possible that the file is ill-formed and you'll be able
to read date[count] but not precipvals[count]...
condition = fscanf(fpin,"%ld,%f", &date[count],
&precipvals[count]);
if(count == 0) {
precip3hrs[count] = precipvals[count];
}
if(count == 1) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count > 1) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
count++;
if (count >= arraylength) break;
}
/* Output section */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
Couldn't you bring these two out of the loop and make the loop
count from 2?
if(count > 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
/* Write to file */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count > 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);
if (fclose(fpout) != 0)
perror("Something went wrong");> printf("Testing\n");
/* Deallocation */
free(precipvals);
Why didn't you use free((void *)precipvals); this time? :)
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
free(date);
date = (long*)NULL;
return 0;
}
I'm just going by the way I was taught and how my book does it. I'm
not a professional programmer.
You asked why the program segfaults. Most of my comments are
possible reasons why it does. And I can't see the reason why even
the most unprofessional programmer in the world would, e.g.,
allocate space for 5 ints and write 5 floats, or 5 longs, in it.

I got it to work now.

Dave


I also tried to remove the casts and it didn't run. Works with them in
so I'll leave them in for now. As for the 5 ints, floats, longs, I was
experimenting with a data set and forgot to fix it.

Dave
 
K

Keith Thompson

I also tried to remove the casts and it didn't run. Works with them in
so I'll leave them in for now. As for the 5 ints, floats, longs, I was
experimenting with a data set and forgot to fix it.

It's really not necessary to quote an entire long article to add a
one-line followup. Trim irrelevant material.

If removing the casts caused the program to break, you've probably
still got bugs; the casts are likely hiding them, not correcting them.

It would also be helpful to be more specific than "it didn't run".
*How* didn't it run? Did it compile and fail during execution? How?
 
R

Richard Heathfield

(e-mail address removed) said:

I also tried to remove the casts and it didn't run. Works with them in
so I'll leave them in for now.

The casts won't fix your problem. They may, however, disguise it.
 
A

Army1987

Army1987 said:
<[email protected]> ha scritto nel messaggio news:[email protected]...
I'm just going by the way I was taught and how my book does it. I'm
not a professional programmer.
You asked why the program segfaults.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5000;
const int errorcode = -1;
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;
FILE *fpin;
FILE *fpout;

fpin = fopen(argv[1],"r+");
If argc is 1, argv[1] is a null pointer. If argc is 0, it doesn't
even exist. Either way, the behaviour is undefined, probably a
segmentation fault.
fpout = fopen(argv[2],"w");
If argc is 2, argv[1] is a null pointer. If argc is <2, it doesn't
even exist. Either way, the behaviour is undefined, probably a
segmentation fault.
If sizeof(int) < sizeof(float), you'll run out of space before
actually writing 5 floats. The behaviour would then be undefined,
probably a segmentation fault.
If sizeof(int) < sizeof(float), you'll run out of space before
actually writing 5 floats. The behaviour would then be undefined,
probably a segmentation fault.
/* Calculation Section */
while(condition != EOF) {
condition = fscanf(fpin,"%l,%f", &date[count], &precipvals[count]);
If the first fopen() failed, fpin is a null pointer. The behaviour
is undefined, probably a segmentation fault. And so will it be for
each time you'll use fpin.
If the third malloc() failed, date is a null pointer. The behaviour
is undefined, probably a segmentation fault. And so will it be for
each time you'll use date.
If the second malloc() failed, precipvals is a null pointer. The
behaviour is undefined, probably a segmentation fault. And so will
it be for each time you'll use precipvals.
If count > arraylength (times sizeof int, divided by sizeof float)
the behaviour is undefined, very likely a segmentation fault.
That's non-portable. The behaviour is implementation-defined (even
if I admit I don't believe it is this one which causes the
segmentation fault, but on the DS9K...).
}
if(count == 1) {
precip3hrs[count] = precipvals[count];
}
if(count == 2) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count > 2) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
}

/* Output section */
for(count = 1; count <= arraylength; count++) {
You probably meant count = 0; count < arraylength? (see below)
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f"
"\n",precipvals[count],precip3hrs[count]);
}
if(count == 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f"
"\n",precipvals[count],precip3hrs[count]);
}
if(count > 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f"
"\n",precipvals[count],precip3hrs[count]);
If count == arraylength, the behaviour is undefined, probably a
segmentation fault. Arrays in C are indexed from 0 to size - 1.
}
}

/* Write to file */
for(count = 1; count <= arraylength; count++) { Ditto as above.
if(count == 1) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]); If the second fopen() failed, fpout is a null pointer. Guess what?
}
if(count == 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count > 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);
Here you have no chance of knowing wheter fpout was successfully
written to and closed. (But I admit this will definitely not cause
a segmentation fault...) Sorry, I couldn't resist to this one.
 
A

Army1987

I also tried to remove the casts and it didn't run.
Are you compiling it with a C++ compiler?
(If so, I'd be surprised of knowing free() works even without
casting the argument to void*).
 
D

dmoran21

<[email protected]> ha scritto nel messaggio[snip code which casts the result of malloc]> I also tried to remove the casts and it didn't run.

Are you compiling it with a C++ compiler?
(If so, I'd be surprised of knowing free() works even without
casting the argument to void*).

I am compiling with gcc on Linux. As for the results of the runs, my
boss ran the program that I wrote and the output looks like it's OK. I
will admit that I've not dealt with arrays since I took a class in C
in 2004. I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.

Dave
 
K

Keith Thompson

<[email protected]> ha scritto nel
messaggio[snip code which casts the result of malloc]
I also tried to remove the casts and it didn't run.
Are you compiling it with a C++ compiler?
(If so, I'd be surprised of knowing free() works even without
casting the argument to void*).

I am compiling with gcc on Linux. As for the results of the runs, my
boss ran the program that I wrote and the output looks like it's OK. I
will admit that I've not dealt with arrays since I took a class in C
in 2004. I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.

If you have code that casts the result of malloc, and removing the
cast causes it to fail, the most likely explanation is that you've
forgetten the required '#include <stdlib.h>'.

Telling us that "it didn't run" doesn't help anybody. You keep
telling us things that make it clear that there are problems in your
code, but you don't post the actual code so we can help you fix them.

You can post your actual current code and let us help you with it, or
you can keep it to yourself and rest secure in the knowledge that your
code will continue to work until it breaks at the most inconvenient
possible moment. It's your choice.
 
D

dmoran21

<[email protected]> ha scritto nel
messaggio[snip code which casts the result of malloc]
I also tried to remove the casts and it didn't run.
Are you compiling it with a C++ compiler?
(If so, I'd be surprised of knowing free() works even without
casting the argument to void*).
I am compiling with gcc on Linux. As for the results of the runs, my
boss ran the program that I wrote and the output looks like it's OK. I
will admit that I've not dealt with arrays since I took a class in C
in 2004. I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.

If you have code that casts the result of malloc, and removing the
cast causes it to fail, the most likely explanation is that you've
forgetten the required '#include <stdlib.h>'.

Telling us that "it didn't run" doesn't help anybody. You keep
telling us things that make it clear that there are problems in your
code, but you don't post the actual code so we can help you fix them.

You can post your actual current code and let us help you with it, or
you can keep it to yourself and rest secure in the knowledge that your
code will continue to work until it breaks at the most inconvenient
possible moment. It's your choice.

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

I will post the code when I have it up at work...I can't get to it at
home.

Dave
 
B

Ben Bacarisse

I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.

A rather depressing density of errors. OK, it is a course for non CS
students but even so... I was going to pick out some of my favourite
howlers but, after a little thought, that seems rather cruel.
 
D

dmoran21

A rather depressing density of errors. OK, it is a course for non CS
students but even so... I was going to pick out some of my favourite
howlers but, after a little thought, that seems rather cruel.

When I took the class, it was required to do things a certain way and
that's the way I've always done them because I've only learned that
way. Maybe I need to go buy another book.

Dave
 
B

Barry Schwarz

On Jul 2, 2:07 pm, "Army1987" <[email protected]> wrote:

Many relevant comments the OP chose to ignore.

If your book tells you to include unused headers ...

and to cast the NULL pointer constant ...

and to cast the return from malloc ...

and that sizeof(int) is enough space for a float ...
snip

I'm just going by the way I was taught and how my book does it. I'm
not a professional programmer.

then you very much need to find a better book.



Remove del for email
 
D

dmoran21

then you very much need to find a better book.

Remove del for email

I included the math.h header because I originally planned to do some
other calculations, but forgot to take it out when I changed my mind.

Dave
 
R

Richard Heathfield

Ben Bacarisse said:
A rather depressing density of errors. OK, it is a course for non CS
students but even so... I was going to pick out some of my favourite
howlers but, after a little thought, that seems rather cruel.

I looked at two of the PDFs (the intro, and the one on variables), and
found little that was actually wrong. Presumably it gets worse later
on.
 
F

Flash Gordon

When I took the class, it was required to do things a certain way and
that's the way I've always done them because I've only learned that
way. Maybe I need to go buy another book.

I've not looked at the notes, but I would recommend K&R2 as a reference
whatever your current book is.
 
A

Army1987

Keith Thompson said:
<[email protected]> ha scritto nel
messaggio[snip code which casts the result of malloc]
I also tried to remove the casts and it didn't run.
Are you compiling it with a C++ compiler?
(If so, I'd be surprised of knowing free() works even without
casting the argument to void*).

I am compiling with gcc on Linux. As for the results of the runs, my
boss ran the program that I wrote and the output looks like it's OK. I
will admit that I've not dealt with arrays since I took a class in C
in 2004. I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.

If you have code that casts the result of malloc, and removing the
cast causes it to fail, the most likely explanation is that you've
forgetten the required '#include <stdlib.h>'.
He did include it, at least in the code he posted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top