#include <stdio.h>
void load();
void main()
{
char *data=malloc(1000);
FILE *file;
file=fopen("test.txt","r");
while(!feof(file))
{
data=fgetc(file); // why dont it print without the pointer sign?
data++;
}
printf("%c",data[1]);
}
#include <stdio.h>
void load();
void main()
{
char *data=malloc(1000);
FILE *file;
file=fopen("test.txt","r");
while(!feof(file))
{
data=fgetc(file); // why dont it print without the pointer sign?
data++;
}
printf("%c",data[1]);
}
data is the pointer which points to your allocated memory, so you definitely don't want to be modifying it.
If you're going to use a char pointer, then you need to keep one pointing to the start of the allocated memory (data), and another to point to each new char in turn (temp)Code:int i = 0; int ch; while( (ch=fgetc(file)) != EOF ) { data[i++] = ch; }
Code:char *temp = data; int ch; while( (ch=fgetc(file)) != EOF ) { *temp++ = ch; }
> void main()
main returns an int
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.