Print a file with all words reversed
How short can a "reverse words in a file" program be in C?
In python it can be accomplished in single line,but what about C?How about 8 lines?
In Python: for i in open("file").read().split(): print i[::-1];
C code to print a file with words reversed:
Who said a file can be opened only using FILE pointer?A FILE pointer is actually an void pointer.
A void pointer:
But,
void *ptr=8;
printf("%d",*ptr);
is an error,as the "printf" statement tries to dereference a void pointer without cast,which is illegal.So the extraction operator " * " cant be used with a void pointer without cast.Also the size of a void pointer is 4 bytes like all the other pointer types.So the above program uses same amount of bytes if FILE *fp; had been used.
An additional info:
void ptr=8; is also illegal.An ordinary variable can't be of type void.
Dereferencing a void pointer:
A void pointer may be dereferenced by casting it into another datatype.Here is an example in C++:
In python it can be accomplished in single line,but what about C?How about 8 lines?
In Python: for i in open("file").read().split(): print i[::-1];
C code to print a file with words reversed:
Who said a file can be opened only using FILE pointer?A FILE pointer is actually an void pointer.
A void pointer:
"A void pointer can point to any type of data"
void *ptr=8;
printf("%d",*ptr);
is an error,as the "printf" statement tries to dereference a void pointer without cast,which is illegal.So the extraction operator " * " cant be used with a void pointer without cast.Also the size of a void pointer is 4 bytes like all the other pointer types.So the above program uses same amount of bytes if FILE *fp; had been used.
An additional info:
void ptr=8; is also illegal.An ordinary variable can't be of type void.
Dereferencing a void pointer:
A void pointer may be dereferenced by casting it into another datatype.Here is an example in C++: