Thread: segmentation fault in for loop

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    13

    segmentation fault in for loop

    please could someone detect some kind of error in the code, because it never enters the loop for, because it doesnt printf, since i cant find the error, i was hoping someone could help me.

    PHP Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAXLINE 1024
      
    char *res 
      char line
    [MAXLINE]="blabla = job";
      
    char *vchar;
      
    int k,n;
      
    vchar=(char)malloc((sizeof (char))*100);
      
    res=fopen(argv[2], "r+");
      
      for(
    k=0n=0line[k]!='\0'k++,n++ )
            {
                
    printf("this is the loop");
                if ((
    line[k]>='a' && line[k]<='z') ||(line[k]>='A' && line[k]<='Z'))
              {
                
    line[k]=vchar[n];
                
    printf("IF 1\n");
              }
                else 
              {
                if((
    line[k]>='a' && line[k]<='z') ||(line[k]>='A' && line[k]<='Z'))
                  { 
                      
    printf("IF 2\n");
                     
    vchar[k]='\0';
                if(
    strstr(name,vchar))
                  {
                    
    printf("YET another IF\n");
                    
    fprintf(res,"%s",corpo);
                  }
                else
                  {
                    
    vchar[n]='\0';
                    
    fprintf(res,"%s",vchar);
                  }
                  }
             } 
    Last edited by Rpog; 04-20-2004 at 03:15 AM.

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    13

    Unhappy

    i switched from
    Code:
    vchar=(char)malloc((sizeof (char))*100);
    to

    Code:
    vchar=malloc((sizeof(char))*100);
    and it didnt segmentation fault, sorry to waste space in the forum

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    You have other problems. For example, you create a pointer to res, but not the storage for res, thus your pointer is pointing to nothing. By itself, that wouæld probably kill you sooner or later, but you then go on to use it as the target for a call to fopen() which returns a FILE * not a char.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    anguished incognito54's Avatar
    Join Date
    Apr 2004
    Posts
    24
    the best way to find segmentationf fault is to use gsb and then run the program till it dumps the core... you see where it crashed and then you add breakpoints to lines that can be sensitive and run the program step by step and see what happens
    I've never felt the nausea of longing to feel nothing,
    I never wanted to cease to exist, just disappear...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM