struct.array works - struct->pointer not
Quote:
>Please look at your post and ask yourself whether a reader has all the
>necessary information to solve the problem.
Sorry if I was too brief in this regard - I did cut down on the
content I had written out on paper (my rehersal). I thought I'd leave
out anything that didnt pertain to the problem - point taken though.
Quote:
>Show (1) the definition of the structure, (2) the point when you create an
>instance of that structure, and (3) the point where you assign the structure
>to your pointer. With these three available, it should be no problem to find
>the cause of the problem.
These 3 aspects have been made before (regarding pointer problems) on
this list, so I apologise for needing to be reminded...it was however
just the ticket and the problem was fixed promptly by (3) assigning
the structure to the pointer and (2) creating a valid instance of the
structure in which to point at.
Quote:
>It is not obvious that you are actually creating an instance of your
>structure, just a pointer. If true, that is the problem -- the pointer has
>nothing to point to, and this results in null pointer assignment.
The instance of the structure was (I thought) the first loop where all
ints in array are set to 0, but then i was using rec->hts not rec.hts
so i see your point.
For anyone who wants to review the changes i wont go adding more code
at this late stage - its brief but the guts are here (besides Paul
did suss the problem out even if he thought there wasnt enough details
to go on)......
Quote:
>>struct records{
>> int hts[MAXLINES];
>>}
>>int main(void)
>>{
struct record rec;
struct record *recptr;
/* create instance of struc record here */
Quote:
>> while(i < MAXLINES)
>> {
rec.hts[i] = 0; /* initialise array subscritps */
Quote:
>> i++;
>> }
/* assign struct to pointer */
recptr = &rec;
Quote:
>> /* more code */
>> /* struct ref not used just passed to tokens_in_str() func */
searchfunction(args....,recptr,...);
Quote:
>> /* after more code - end of main() */
>>}
void toks_in_string function(struct record *recptr, int line,char
str[],int tokens[])
Quote:
>>{
>> while((line < MAXLINES) && (str != NULL) etc.....)
>> {
>> if(strstr(liststring,tokens[i]) != NULL)
recptr->hts[line] += 1;
Quote:
>> etc....../* this func called every line by serachfunc() */.
To those who spotted the typo's - yes my code would have suffered if
they (the typos) were actually there at compile time - but I would not
have got a "NULL pointer assignment "message.
Thanks for your efforts
David Anderson