struct.array works - struct->pointer not 
Author Message
 struct.array works - struct->pointer not

Hi all
The problem is one of referencing an array via a structure pointer
versus straight array - member access - abbrev code below.
struct records{
        int hts[MAXLINES];
Quote:
}

int main(void)
{
        struct record *rec; /*just rec for alternative */
        while(i < MAXLINES)
          {
===>             rec->hts[i] = 0;    /* initialise array subscritps */  
            i++;
          }
        .
        /* more code */
        /* struct ref not used just passed to tokens_in_str() func */
        searchfunction(args....,rec,...);

        /* after more code - end of main() */

Quote:
}

void toks_in_string function(struct record *rec, int line,char
str[],int tokens[])
{
        while((line < MAXLINES) && (str != NULL) etc.....)
          {
            if(strstr(liststring,tokens[i]) != NULL)
====>              rec->hts[line] += 1;

        etc....../* this func called  every line by serachfunc() */.
The problem occurs (===>) with these 2 loops. Quite simply if i
reference by straight struct referencing (ie rec.hts no pointer) alls
well but I cant use the resulting array data back in main(). This code
above (with struct pointers) yeilds the right data back in main() but
with the run-time error of "null pointer assignment"??????
Any help would be appreciated.
David Anderson



Fri, 13 Jul 2001 03:00:00 GMT  
 struct.array works - struct->pointer not
Please look at your post and ask yourself whether a reader has all the
necessary information to solve the problem.

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.

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.

Paul Lutus

Quote:

>Hi all
>The problem is one of referencing an array via a structure pointer
>versus straight array - member access - abbrev code below.
>struct records{
> int hts[MAXLINES];
>}
>int main(void)
>{
> struct record *rec; /*just rec for alternative */
> while(i < MAXLINES)
>   {
>===>      rec->hts[i] = 0;    /* initialise array subscritps */
>     i++;
>   }
> .
> /* more code */
> /* struct ref not used just passed to tokens_in_str() func */
> searchfunction(args....,rec,...);

> /* after more code - end of main() */
>}
>void toks_in_string function(struct record *rec, int line,char
>str[],int tokens[])
>{
> while((line < MAXLINES) && (str != NULL) etc.....)
>   {
>     if(strstr(liststring,tokens[i]) != NULL)
>====>       rec->hts[line] += 1;

> etc....../* this func called  every line by serachfunc() */.
>The problem occurs (===>) with these 2 loops. Quite simply if i
>reference by straight struct referencing (ie rec.hts no pointer) alls
>well but I cant use the resulting array data back in main(). This code
>above (with struct pointers) yeilds the right data back in main() but
>with the run-time error of "null pointer assignment"??????
>Any help would be appreciated.
>David Anderson



Fri, 13 Jul 2001 03:00:00 GMT  
 struct.array works - struct->pointer not
Aight, I'm not sure this will work, but why don't you try putting an "&"
before the rec->hts[]    I'm not too sharp on pointers (if you will excuse
the subtle pun...), but I think that should work.

Alex (a.k.a. Ringleader)

p.s.  Remove the "nospam" in my address to respond via e-mail

----------

Quote:

>Hi all
>The problem is one of referencing an array via a structure pointer
>versus straight array - member access - abbrev code below.
>struct records{
> int hts[MAXLINES];
>}
>int main(void)
>{
> struct record *rec; /*just rec for alternative */
> while(i < MAXLINES)
>   {
>===>       rec->hts[i] = 0;    /* initialise array subscritps */  
>     i++;
>   }
> .
> /* more code */
> /* struct ref not used just passed to tokens_in_str() func */
> searchfunction(args....,rec,...);

> /* after more code - end of main() */
>}
>void toks_in_string function(struct record *rec, int line,char
>str[],int tokens[])
>{
> while((line < MAXLINES) && (str != NULL) etc.....)
>   {
>     if(strstr(liststring,tokens[i]) != NULL)
>====>       rec->hts[line] += 1;

> etc....../* this func called  every line by serachfunc() */.
>The problem occurs (===>) with these 2 loops. Quite simply if i
>reference by straight struct referencing (ie rec.hts no pointer) alls
>well but I cant use the resulting array data back in main(). This code
>above (with struct pointers) yeilds the right data back in main() but
>with the run-time error of "null pointer assignment"??????
>Any help would be appreciated.
>David Anderson



Fri, 13 Jul 2001 03:00:00 GMT  
 struct.array works - struct->pointer not
:struct records{
: int hts[MAXLINES];
:}

you forgot the ; after the } in the above line. that'll cause
problems.

:int main(void)
:{
: struct record *rec; /*just rec for alternative */
: while(i < MAXLINES)
:   {

well, you're gonna want rec[i].hts here

:===>      rec->hts[i] = 0;    /* initialise array subscritps */
:     i++;
:   }
[Snippage]

: while((line < MAXLINES) && (str != NULL) etc.....)
:   {
:     if(strstr(liststring,tokens[i]) != NULL)
:====>       rec->hts[line] += 1;

This looks messy. Not too sure whats going on here but you
probably want something like: rec[line].hts



Sat, 14 Jul 2001 03:00:00 GMT  
 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


Sat, 14 Jul 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. memory leak: pointer->pointer->pointer->struct

2. memory Leak: pointer->pointer->pointer->struct

3. return a struct, not a struct pointer ?

4. Why pass structs? (not struct pointers)

5. pointers to functions in structs which take a pointer to that struct as an arg

6. cast pointer to struct to pointer to type of first member of struct

7. array of pointers to array of structs

8. qsorting & managing struct arrays, pointer arrays

9. struct a <---> struct b

10. Problem with struct/array inside a struct using fread/fwrite

11. Initialisation of structs containing arrays of structs...?

12. native dll calling with structs containing arrays of nested structs

 

 
Powered by phpBB® Forum Software