Thread: Assigning Array Elements By Reference

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    11

    Assigning Array Elements By Reference

    Hi All,
    Can you spot an error in my code? It shows no warnings but causes a seg fault. I've isolated it down to the wrd[] lines in red. Rest of the code available if needed.

    Thanks in advance.

    Code:
    char * fetchword( FILE* ifp, char* *wrd, int* wrd_count ) {
      unsigned int c ;      /*character to be added to end of current string*/
      int wrd_len = 0 ;     /*number of characters to be in new string*/
    
      /*other stuff done here*/
    
      *wrd = realloc( *wrd, wrd_len * sizeof(char) ) ;    /*expand current array by one char*/
          if( *wrd == NULL ) {
            perror("realloc returned NULL. Unable to allocate memory.") ;
            exit (-1) ;
          }
         *wrd[wrd_len-1] = c ;            /*replace existing '\0' with new char*/
         *wrd[wrd_len] = '\0' ;             /*add the string terminator after new char*/
    
      return *wrd ;
    Last edited by bhenderson; 08-22-2009 at 05:26 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you alloc wrd_len number of elements, *wrd[wrd_len] doesn't exist.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    if *wrd[] doesn't exist, what can I use?

    Thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I never said *wrd[] didn't exist. I said *wrd[wrd_len] didn't exist. If you ask for six things, and then try to use the seventh, what do you expect?

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    Oh right, yes, that would be obvious then. Sorry for being dumb!

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    I'm pulling my hair out with this one now. I thought I had it sorted last night, but seemingly not! Any clues guys? They'd be really appreciated.

    Code:
    char * fetchword( FILE* ifp, char* *wrd, int* wrd_count ) {
    
      /*other stuff here*/
    
      *wrd = realloc( *wrd, (wrd_len+1) * sizeof(char) ) ;   
          *wrd[(wrd_len-1)] = (char)c ; /*seg fault*/
          *wrd[(wrd_len)] = '\0' ;             /*seg fault*/
          /* *wrd[0] = (char)c; */              /*works fine*/
    
    return *wrd ;

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    An array and index expression is the same as a pointer and an offset so don't throw 'em into the same mix
    Code:
    *wrd[(wrd_len-1)] = (char)c;
    Edit: Whoops! didn't realize that wrd is a double-indirect pointer. Have you allocated storage for wrd before moving onto *wrd?
    Last edited by itCbitC; 08-23-2009 at 03:22 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bhenderson View Post
    I'm pulling my hair out with this one now. I thought I had it sorted last night, but seemingly not! Any clues guys? They'd be really appreciated.

    Code:
    char * fetchword( FILE* ifp, char* *wrd, int* wrd_count ) {
    
      /*other stuff here*/
    
      *wrd = realloc( *wrd, (wrd_len+1) * sizeof(char) ) ;   
          *wrd[(wrd_len-1)] = (char)c ; /*seg fault*/
          /* (*wrd)[wrd_len-1] = (char)c; */
          *wrd[(wrd_len)] = '\0' ;             /*seg fault*/
          /* (*wrd)[wrd_len] = '\0'; */
          /* *wrd[0] = (char)c; */              /*works fine*/
    
    return *wrd ;
    Check the blue bits -- I bet we need to specify order of operations, otherwise the indexing will happen first and then the * will happen at the end.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    Thanks tapstop, that makes sense to me and fixes the issue. Hurray!

    -The array and index are mixed because wrd is a char**.

    Working Code:
    Code:
    (*wrd)[wrd_len-1] = (char)c ;
    (*wrd)[wrd_len] = '\0' ;
    Thanks to everyone else for their effort as well!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  3. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

Tags for this Thread