![]() |
| | #1 |
| Registered User Join Date: Aug 2009
Posts: 11
| Assigning Array Elements By Reference 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 | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| If you alloc wrd_len number of elements, *wrd[wrd_len] doesn't exist. |
| tabstop is offline | |
| | #3 |
| Registered User Join Date: Aug 2009
Posts: 11
| if *wrd[] doesn't exist, what can I use? Thanks |
| bhenderson is offline | |
| | #4 |
| and the Hat of Guessing 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 | |
| | #5 |
| Registered User Join Date: Aug 2009
Posts: 11
| Oh right, yes, that would be obvious then. Sorry for being dumb! |
| bhenderson is offline | |
| | #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 | |
| | #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; Last edited by itCbitC; 08-23-2009 at 03:22 PM. |
| itCbitC is offline | |
| | #8 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
| |
| tabstop is offline | |
| | #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' ; |
| bhenderson is offline | |
![]() |
| Tags |
| array, assign, by reference, double, pointer |
| Thread Tools | |
| Display Modes | |
|
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 |