Still having a little trouble w/th ptrs

This is a discussion on Still having a little trouble w/th ptrs within the C Programming forums, part of the General Programming Boards category; I have implemented that delwhitespace code. Pretty neat little code. Thanks heaps for that. I am able to compile it, ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    38

    Still having a little trouble w/th ptrs

    I have implemented that delwhitespace code. Pretty neat little code. Thanks heaps for that. I am able to compile it, but it does not do anything. As it will not enter my while loop, which originally worked.
    Here the while statement:
    Code:
    while( fgets( ptrstr, 80, file ) != NULL )
    {
        delspace( ptrstr );
        fwrite( (void *) ptrstr, sizeof( char * ), strlen( ptrstr ), compfile );
        printf("%d%p\n", strcompare( ptrstr, ptroldstr ), ptrstr );
        strcpy( ptroldstr, ptrstr );
    }

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    38
    Thanks for that. I ll try it out straight away. The ptrstr is declared as follows:
    Code:
        char *ptrstr, *ptroldstr;     
    
        /*create memory for the two string pointers */
        ptrstr = ( char * ) malloc( sizeof( char ) * 80 ); 
        ptroldstr = ( char * ) malloc( sizeof( char ) * strlen( ptrstr ));
    For more details see attachment. Appreciate the help. Its been bugging me all week.Cheers, Johannes
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    38
    Ta for that. I can see what you mean. What just blows me out of sanity is why the stupid while loop is not once used when compiling it. If I put as puts() before the while loop it will compile and display it, once I put a puts() within the while loop, NOTHING. The while loop was working before but?? Thats programming for you. U solve one problem and fall over the next one.
    Have you got an idea what might cause that problem?Cheers, Johannes

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    38
    I have seperated the delete space function from my main program and now it works. I am not quite sure why. I think it was because of have%p rather than %s in the printf() statement.

    But now another question:
    I want to get a number that represents the count of same letters in two string pointers and then write the number plus the string to file. This way it looks like that:
    Before:

    Jamies,P
    Jamison,M

    After:

    0Jamies,P
    3ison,M

    Am I write in thinking that you' d have to have the number as a string and then concat them?

    Thankfull for any hints or help. Cheers,
    Johannes
    Attached Files Attached Files
    Last edited by Yourhighness; 05-16-2003 at 10:34 PM.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    38
    Thank you so much for that. i have been looking at the code all afternoon and couldnt quite see what went wrong. And the int solution ( &int ) is just what I was trying to think of. Now if I get you right, fwrite is writing in binary? How do I write to file so that you can view it in a text file then? Coz I think thats what we are to do for the assignment.

    Thanks so much for the help.
    Cheers,
    Johannes

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    38
    thanks for the help. I got it to sorta work. I am still getting a "segmentation fault - core dumped" message tho.
    Code:
    while( fgets( ptrstr, 80, file ) != NULL )
        {   
            delspace( ptrstr );
            result = strcompare( ptrstr, ptroldstr );       
            printf( "%d%s", result, ptrstr );
            fwrite( &result, sizeof( int ), 1, compfile );
            fwrite( ptrstr, sizeof( char ), strlen( ptrstr ), compfile );
            strcpy( ptroldstr, ptrstr );
    puts( "I WANT MORE" );
        }
    
    puts( "I WANT EVEN MORE" );/*segmentation fault comes here*/
    
        fclose( file );
        fclose( compfile );
        return 0;
    }
    The other thing is with strcpy in my compare function. It says:

    deltest.c: In function `strcompare':
    deltest.c:135: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast

    Code:
    int strcompare( char *ptrstr, char *ptroldstr )
    {
        int i;
         
        if( strlen( ptroldstr ) == 0 )
        {
            ptroldstr = ptrstr;
            return 0;
        }
        for( i = 0; i < strlen( ptrstr ); ++i)
           if( *( ptrstr + i ) != *( ptroldstr + i ) )
               break;
            
        ptroldstr = strcpy( ptrstr, ptrstr[ i ] );
        return i;
            
    }
    I had that warning quite freqquently in earlier stages but I am not quite sure what thats supposed to mean for me?
    Any hints, ideas appreciated. Cheers,
    Johannes
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    38
    Cheeers. I just ran it under cygwin and its running fine. I think its just that I was still using fwrite. Just means I have to have a read on fprintf again. Thank you so much for the help. Now that thats done, the uncompression shouldnt be too hard.
    Thanks again,
    Johannes

    PS: Would this work? At the moment its complaining bout the strncpy use.
    Code:
    char * strcompare( char *ptrstr, char *ptroldstr )
    {
       int i;
       char * temp;
    
       temp = malloc ( 80 * sizeof *temp);
      
       if( strlen( ptroldstr ) == 0 )
       {
           strcpy(ptroldstr,ptrstr);
           return 0;
       }
       for( i = 0; i < strlen( ptrstr ); ++i)
       {
          if( isdigit( *( ptrstr + i ) ) != 0 )
          {
             if( *( ptrstr + i ) == '0' )
             {
                temp = strstr( ptrstr, "0" );
                strcpy( ptrstr, temp );
             }
    
             strncpy( ptrstr, *( ptrstr + i ), strlen( ptrstr ) );
          } 
          
       }
    
       strcpy( ptroldstr, &ptrstr[ i ] );
       return ptrstr;
    }
    It wouldnt let me attach the file so I accidentaly started a new thread. The file and another question is under "slightly got lost and confused".Sorry, I know you probably have better things to do but I really appreciate all the help. May be one day I understand enough to be of help to others too.
    Cheers, Johannes

    "You gotta hate programming to love it. The more you hate it the more you love it!"
    Last edited by Yourhighness; 05-18-2003 at 06:51 AM.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    38

    Question how to change frm number to letters of string

    Here is the new file attached. To my understanding all that is to be done for the uncompression is: looking for a number at slot 0 of ptroldstr --> if it is 0 then get rid of the 0
    --> if it is any number, copy that many numbers off
    ptroldstr into ptrstr
    --> then insert a space ( ' ' ) after the comma and
    before the initial.
    At the moment I slightly lost the plot and got confused. So If you could once again give me a hint or two that would be great.

    I really appreciate the help. Cheers, Johannes

    I love programming - I hate staring at the code and not figuring out what the problem is
    Attached Files Attached Files
    Last edited by Yourhighness; 05-18-2003 at 10:13 AM.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Threads merged.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    38
    Thanks for merging the threads. I had a look at the faq's but I had trouble finding information on how to subscribe, unsubscribe from threads, how to close a thread or if we have the ability to do so.
    Cheers,
    Johannes

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    38
    Hey guys. When running prog.c I just realised that half way through it stuffs up??? It shows the following:

    Code:
    0JAMES,P
    3ISON,M
    0JAMISON,P
    1OHNS,G
    0JOHNSON,N
    0KAY,R
    3SER,A
    0KAYSELY,Z
    0JOHNSON should be 5ON,N .
    As it is meant to take the J represented by the 1 of the previous line plus whatever letters are the same as the above string. Ie:
    'OHNS'. Am I not refreshing ptroldstr properly? What could be the problem?
    It only stuffs up at that point and at the very end comparing 3SER,A with 0KAYSELY,Z.

    Thanks for any help or hints given.
    Cheers,
    Johannes
    'The more I try, the more I understand. The annoyance of not seeing the wrong seems huge, but its worth every effort'
    Attached Files Attached Files
    Last edited by Yourhighness; 05-19-2003 at 02:29 AM.

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    38
    One of my lecturers who s also teaching C, said that I am loosing oldptrstr at some point and that I have to updated it. I am just not sure exactly where and how. I played around with it this afternoon and I couldnt get it to work properly. Also he mentioned that hardcoding malloc with the 80 is inefficient.
    Would it be more efficient if I was to malloc within the while loop of main and instead of 80, strlen( ptrstr )?
    Anyway, here the sample file given to us containing the names. Thanks heaps,
    Johannes
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    May 2003
    Posts
    38

    uncomp.c works but doesnt do anything?

    I just compiled and ran my part 2 of the assignment, where the uncompression is supposed to take place. It is compiling and runing but if you view the created text file, it has done absolutely nothing to the contents of the file! I dont see why. All these lines of code in the functions, and nothing happens. Any idea what I oversaw when I coded it last night and this afternoon?

    [quote]'The more I try, the more I understand. The annoyance of not seeing the wrong seems huge, but its worth every effort'[quote]
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    May 2003
    Posts
    38
    Yeah that with the char [] is what my lecturer says too. But would it work with malloc in the while, or is it a better idea to go off an change all my ptrstr s to char[]?
    Also I dont quite see what you mean? I am having trouble to visualise that part of the function. Even when I take a piece of paper and a pen I dont quite seem to get the picture.
    Cheers,
    Johannes
    PS: I ended up using the '&' because it gave me all these warnings before. It was kind of an intelligent guess?

    'The more I try, the more I understand. The annoyance of not seeing the wrong seems huge, but its worth every effort'

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    38
    Thanks for that. I was just about to post my code again. I had it so it would actually sorta do it but not. I will have a look at your file in a second. Now out of curriosity, How come my file writes the following:
    Code:
    0JAMES,P
    3JAMISON,M
    ISON,M
    8JAMISON,P
    P
    1JOHNS,G
    OHNS,G
    5JOHNSON,N
    ON,N
    0KAY,R
    KAY,R
    3KAYSER,A
    SER,A
    5KAYSELY,Z
    LY,Z
    If I would get the number of the first line and the letters from the second line, it would be exactly what is asked for - I think!
    Lovely. I am getting the hang of it, but thinking that my next assignment is on linked lists of linked list - delicious.

    PS: Just noticed from your example comp file - that it goes
    Code:
    8P
    wheras the other one's the '.' isnt swallowed.

    PPS: AM I understanding it right that
    Code:
    &ptrstr[result]
    means that it starts writing from the result element of the string array ptrstr?

    Again, I really appreciate all your help.
    Cheers,
    Johannes
    'The more I try, the more I understand. The annoyance of not seeing the wrong seems huge, but its worth every effort'
    Attached Files Attached Files
    Last edited by Yourhighness; 05-20-2003 at 02:39 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. struct ptrs to struct ptrs
    By lolguy in forum C Programming
    Replies: 5
    Last Post: 02-23-2009, 11:02 PM
  3. Replies: 6
    Last Post: 01-03-2007, 02:02 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 11:58 AM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21