B
Bill Cunningham
I have this unfinished untested code that I would like to enquire to
someone that has used malloc() before if I can use it here.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
if (argc > 10) {
printf(" too many characters in name\n try 10 or less\n");
exit(1);
}
char p[20];
FILE *fp;
size_t t;
if ((fopen("argv[1]", "r")) == EOF) {
fprintf(stderr, "fopen error\n");
exit(1);
}
t = fread(p, sizeof(int), 20, fp);
if (t = ferror(fp)) {
fprintf(stderr, " file error indicated\n");
exit(1);
} else if (t = feof(fp)) {
fprintf(stderr, " EOF indicated\n");
exit(1);
}
In the code above the 3rd parameter of malloc() is taking 20 character
members. That's not enough. But I don't want buffer overflow either. I know
this is kind of reinventing the wheel so far in what is above but there is a
reason for this. I am trying to write a text editor that takes code that
only ends with "+" instead of C's normal ";". Can malloc be put where the 20
is and just take a malloc? malloc returns void * and this 3rd parameter a
size_t so maybe a struct or union would be easier. Such as
struct values{
size_t t;
void * val;
};
Would that eliminate the need for casting?
Bill
someone that has used malloc() before if I can use it here.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
if (argc > 10) {
printf(" too many characters in name\n try 10 or less\n");
exit(1);
}
char p[20];
FILE *fp;
size_t t;
if ((fopen("argv[1]", "r")) == EOF) {
fprintf(stderr, "fopen error\n");
exit(1);
}
t = fread(p, sizeof(int), 20, fp);
if (t = ferror(fp)) {
fprintf(stderr, " file error indicated\n");
exit(1);
} else if (t = feof(fp)) {
fprintf(stderr, " EOF indicated\n");
exit(1);
}
In the code above the 3rd parameter of malloc() is taking 20 character
members. That's not enough. But I don't want buffer overflow either. I know
this is kind of reinventing the wheel so far in what is above but there is a
reason for this. I am trying to write a text editor that takes code that
only ends with "+" instead of C's normal ";". Can malloc be put where the 20
is and just take a malloc? malloc returns void * and this 3rd parameter a
size_t so maybe a struct or union would be easier. Such as
struct values{
size_t t;
void * val;
};
Would that eliminate the need for casting?
Bill