|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Trying to use c for the first time in years and blowing out in fgets.
I created this very simple program which still produces the error: #include <stdio.h> int main(int argc, char *argv[]) { FILE *fp1; char *text1; fgets(text1,150,stdin); fputs(text1,stdout); return(0); } The error is "The instruction at "0x78022901" referenced memory at "0x00000003". The memory could not be "written"." I get it after typing in some text and hitting enter. I'm using gcc. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
blindsey wrote:
> Trying to use c for the first time in years and blowing out in fgets. > > I created this very simple program which still produces the error: > > #include <stdio.h> > > int main(int argc, char *argv[]) > { > FILE *fp1; > char *text1; > > fgets(text1,150,stdin); > fputs(text1,stdout); > return(0); BTW, you don't need parentheses in this return statement. > } http://c-faq.com/aryptr/aryptr2.html -- Peter |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
blindsey wrote, On 15/04/08 23:58:
> Trying to use c for the first time in years and blowing out in fgets. > > I created this very simple program which still produces the error: > > #include <stdio.h> > > int main(int argc, char *argv[]) > { > FILE *fp1; > char *text1; > > fgets(text1,150,stdin); <snip> You have declared text1 as being a pointer, where does it point to? Go to the comp.lang.c FAQ at http://c-faq.com/ and read about arrays and pointers. -- Flash Gordon |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Apr 15, 7:06pm, Peter Nilsson <ai...@acay.com.au> wrote:
> blindsey wrote: > > Trying to use c for the first time in years and blowing out in fgets. > > > I created this very simple program which still produces the error: > > > #include <stdio.h> > > > int main(int argc, char *argv[]) > > { > > FILE *fp1; > > char *text1; > > > fgets(text1,150,stdin); > > fputs(text1,stdout); > > return(0); > > BTW, you don't need parentheses in this return statement. > > > } > > http://c-faq.com/aryptr/aryptr2.html > > -- > Peter Thanks! Ah, in the old days I never would have ... Like I said, it had been a while. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
"blindsey" <blindsey@dsicdi.com> wrote in message news:ff029a70-ed6b-49a7-9aab-6505af1c8ab2@p25g2000hsf.googlegroups.com... > Trying to use c for the first time in years and blowing out in fgets. > > I created this very simple program which still produces the error: > > #include <stdio.h> > > int main(int argc, char *argv[]) > { > FILE *fp1; > char *text1; You should initialise text1 here, to point to an array of at least 150 chars, or using malloc to allocate at least 150 chars. Otherwise it will contain an arbitrary (and illegal) address. Such as 0x00000003. > fgets(text1,150,stdin); > fputs(text1,stdout); > return(0); > } > > The error is "The instruction at "0x78022901" referenced memory at > "0x00000003". The memory could not be "written"." I get it after > typing in some text and hitting enter. > > I'm using gcc. -- Bart |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
blindsey <blindsey@dsicdi.com> writes:
> Trying to use c for the first time in years and blowing out in fgets. > > I created this very simple program which still produces the error: > > #include <stdio.h> > > int main(int argc, char *argv[]) > { > FILE *fp1; You never initialize fp1, but that's ok since you never use it. > char *text1; text1 is a pointer. You never initialize it, so it points to some arbitrary location in memory -- or it doesn't point anywhere at all. > fgets(text1,150,stdin); Now you're trying to read up to 150 bytes of information from stdin into some unspecified location. Kaboom. > fputs(text1,stdout); > return(0); > } > > The error is "The instruction at "0x78022901" referenced memory at > "0x00000003". The memory could not be "written"." I get it after > typing in some text and hitting enter. > > I'm using gcc. One fix would be to declare text1 as an array rather than as a pointer. Another would be to allocate some memory for text1 to point to, perhaps using malloc(). The comp.lang.c FAQ is at <http://www.c-faq.com/>. Reading the whole thing probably wouldn't be a bad use of your time. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
try this:
#include <stdio.h> void main(void) { char text1[151]={0}; fgets(text1,sizeof(text1)-1,stdin); fputs(text1,stdout); } or, to save into a file whatever you type in the keyboard: #include <stdio.h> void main(void) { char text1[151]={0}; FILE *f; fgets(text1,sizeof(text1)-1,stdin); f=fopen("test.txt","w"); if(f) { fputs(text1,stdout); fclose(f); } } there is a very alike example in: http://www.cplusplus.com/reference/c...dio/fputs.html []'s |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
try this:
#include <stdio.h> void main(void) { char text1[151]={0}; fgets(text1,sizeof(text1)-1,stdin); fputs(text1,stdout); } or, to save into a file whatever you type in the keyboard: #include <stdio.h> void main(void) { char text1[151]={0}; FILE *f; fgets(text1,sizeof(text1)-1,stdin); f=fopen("test.txt","w"); if(f) { fputs(text1,f); fclose(f); } } there is a very alike example in: http://www.cplusplus.com/reference/c...dio/fputs.html []'s |
|
![]() |
| Outils de la discussion | |
|
|