C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-22-2009, 05:23 PM   #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.
bhenderson is offline   Reply With Quote
Old 08-22-2009, 05:32 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
If you alloc wrd_len number of elements, *wrd[wrd_len] doesn't exist.
tabstop is offline   Reply With Quote
Old 08-22-2009, 06:02 PM   #3
Registered User
 
Join Date: Aug 2009
Posts: 11
if *wrd[] doesn't exist, what can I use?

Thanks
bhenderson is offline   Reply With Quote
Old 08-22-2009, 06:05 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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?
tabstop is offline   Reply With Quote
Old 08-22-2009, 06:20 PM   #5
Registered User
 
Join Date: Aug 2009
Posts: 11
Oh right, yes, that would be obvious then. Sorry for being dumb!
bhenderson is offline   Reply With Quote
Old 08-23-2009, 02:35 PM   #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 ;
bhenderson is offline   Reply With Quote
Old 08-23-2009, 03:13 PM   #7
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
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.
itCbitC is offline   Reply With Quote
Old 08-23-2009, 03:14 PM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 08-23-2009, 03:21 PM   #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!
bhenderson is offline   Reply With Quote
Reply

Tags
array, assign, by reference, double, pointer

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional Array Addressing BlackOps C Programming 11 07-21-2009 09:26 PM
allocation and reallocation of memory dynamically of an integer array zamir C++ Programming 16 05-29-2009 07:25 PM
Which Library Files for these unreferenced functions? lehe C++ Programming 3 01-31-2009 10:30 PM
Screwy Linker Error - VC2005 Tonto C++ Programming 5 06-19-2007 02:39 PM
c++ linking problem for x11 kron Linux Programming 1 11-19-2004 10:18 AM


All times are GMT -6. The time now is 12:01 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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