Re: segmentation fault



On Sun, 18 Jun 2006 17:50:26 GMT, Andrew Poelstra
<apoelstra@xxxxxxxxxxxxxxxxxxxxx> wrote:

On 2006-06-18, Piotr S. <inri@xxxxxxxxxxxx> wrote:
What's wrong in following program:
Lots.

#include <stdio.h>
#include <stdlib.h>
char s[80];
Why are you using a global?

int main()
int main (void) is better.

{
FILE *fp, *fopen();
*fopen()? Does that really compile unhindered? It doesn't make any sense,
as fopen() is already defined in stdio.h.

It is only declared in stdio.h. Since this declaration is at
function/block scope, it serves to temporarily replace the declaration
in stdio.h only for code in this function/block.


if(fp = fopen("plik1","r")== NULL)
{
printf("blad otwarcia"); exit(1);
What's wrong with return 1; or better, return EXIT_FAILURE;.

}
else
{
printf("\notwarty\n");
while(fgets(s,30,fp)== NULL)
While fgets returns a null string, print the null string? That can't be
what you want.

It's even worse. While fgets returns a NULL pointer, attempting to
print the data pointed to invokes undefined behavior.


printf("%s\n",s);
}
}

I thought that buffor s is not properly declared, I had tried to declare
this variable
inside main(), and as char * s; Everytime runnig the program resulted in
segmentation fault.


I can imagine. You shouldn't try to printf a NULL pointer.


Remove del for email
.



Relevant Pages

  • Re: MFC dll and exe porting to 64-bit
    ... Why the obsolete 'char *' instead of the proper LPTSTR? ... We are no longer programming 16-bit windows in an 8-bit character world, ... string arguments should probably be LPCTSTR, that is, const parameters. ... declaration of the function itself, in context (meaning show the surrounding class ...
    (microsoft.public.vc.mfc)
  • on topic to clc [Was: Banks and economy]
    ... pointer type - which I think is correct for "arrays", ... Since, as a formal parameter, char cptr[]; ... pointer to the 'completing' declaration, ...   tentative definition. ...
    (comp.lang.c)
  • Re: split a string
    ... But this reads a string into a single char ... Check fgetsfrom the Standard C Library as a way to get string input ... pointer to a pointer to a char. ... To handle arrays remotely. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: copy a string into a 2d array of chars
    ... This split function should allocate a 2D array of chars ... >focus the program the string is not actually split. ... later) is an array of char containing the original contents of the ... The i-th pointer will contain the starting address of the ...
    (comp.lang.c)
  • Re: problem with function
    ... > If you intended copying the string, you need to use the strcpy function. ... I suggest researching hungarian notation for variable names. ... Isn't 'psz' a pointer a an array of characters containing a string? ... pointer to char. ...
    (alt.comp.lang.learn.c-cpp)