Accessing arrays of char pointers inside of structs. 
Author Message
 Accessing arrays of char pointers inside of structs.

I am using Borland C++.  (So shoot me!)

Anyway, I have a struct declared thusly:

        struct P {
                char *s[12];
                struct P *prev,*next;
                }

and I have a pointer to a struct P, say pP.  How do I access the nth
element of the s array, for the struct pointed to by pP?

I thought simply using:

        pP -> s[n]

would do it, but that fails miserably.  When I print that out as a
pointer, I get 0, and I think using it as a lvalue trashes memory.

In desperation, I have defined the following macro:

        #define MAKEP(ptr,idx) (char **) &(ptr -> s[idx])

and it seems to have solved the problem.  Using MAKEP, the following
conditions hold:

        MAKEP(P,i) is the address of the pointer that points to my string.

        *MAKEP(P,i) is the address of my string.

        **MAKEP(P,i) is the first charater of my string.

But surely, there is a better way!

****************************************************************************

Bizarreness is the essence of the {*filter*}

(THis [sic] message should be assumed to have been smiley captioned for the
humor impaired)



Sun, 09 Oct 1994 04:14:16 GMT  
 Accessing arrays of char pointers inside of structs.

Quote:
>Anyway, I have a struct declared thusly:

>    struct P {
>            char *s[12];
>            struct P *prev,*next;
>            }

>and I have a pointer to a struct P, say pP.  How do I access the nth
>element of the s array, for the struct pointed to by pP?

        struct  P       *pP;    /* If you typedef struct P

Quote:
>I thought simply using:

>    pP -> s[n]

        This would work if you had defined char s[12] inside your
structure (versus char *s[12]). What I think you have, when you use s,
is a pointer to the head of an array, which is a pointer also. So I
would recommend trying...

        pP->(*s)[n];

Carl Walker
--
Carl Walker                             "Hectic days ahead of us,
Bucknell University                      Rotten ones behind,
Lewisburg, PA                            Keeping plastics in our pockets,
                                         Children never mind." - K, S, & W



Mon, 10 Oct 1994 05:19:27 GMT  
 Accessing arrays of char pointers inside of structs.

Quote:

> I am using Borland C++.  (So shoot me!)

I dunno about Borland C++, but since this post is in comp.lang.c
instead of comp.lang.c++, I'll check my response with a C compiler.

Quote:
> Anyway, I have a struct [...] [..paraphrased:..]
>         struct P { char *s[12]; struct P *prev,*next; }
> and I have a pointer to a struct P, say pP.  [...]
> I thought simply using: pP -> s[n] would do it, but that fails miserably.

Sounds like a buggy compiler (unless C++ has diverged more in this
respect than I had thought, or the -> operator was being tinkered
with in ways not mentioned).  For example, when I run this program
as compiled by the local C compiler, with arguments "foo bar bletch",

    #include <stdio.h>
    #include <stdlib.h>

    typedef struct S {char *s[12]; struct S *prev, *next; } T;

    int main( int n, char **a ) {
        int i;
        T *p;
        p = (T*)malloc(sizeof T);
        for( i=1; a[i] && i<=12; i+=1 )
            p->s[i-1] = a[i];
        for( i=0; p->s[i] && i<12; i+=1 )
            printf( "%p %p %s\n", (void*)(p->s+i), (void*)p->s[i], p->s[i] );
        return(0);
    }

it prints

    10002000 7fffbb0c foo
    10002004 7fffbb10 bar
    10002008 7fffbb14 bletch

Seems pretty straightforward to me.

Quote:
> ( THis [sic] message should be assumed to have been smiley captioned
>   for the humor impaired )

Ah, I see.  The posting was a joke, and I missed it entirely.
"Never mind".  ( Uh... could somebody explain the joke to me sometime? )

Wayne Throop       ...!mcnc!aurgate!throop



Tue, 11 Oct 1994 22:15:31 GMT  
 Accessing arrays of char pointers inside of structs.

Quote:
>So I would recommend trying...
>    pP->(*s)[n];

I would recommend that you try it yourself, before posting it to the world.
That isn't legal C.

Now, (*pP->s)[n] is a syntactically valid expression which is close to your
recommendation, but I doubt that it has the desired semantics, since the
member in question was declared char *s[12] and not char (*s)[12].

In fact the original pP->s[n] looks correct to me, provided pP points to an
object of the type in question, and 0 <= n < 12.  The result may be a null
pointer, or even garbage if the array-of-strings was never initialized;
perhaps this is the problem.


(This article supersedes an earlier draft where I managed to{*filter*}up my own
version of the expression, a particularly bad thing to do while correcting
someone else.  Thanks to Mark Brader for catching the typo.)



Fri, 14 Oct 1994 00:14:01 GMT  
 
 [ 4 post ] 

 Relevant Pages 

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

2. A char pointer (char *) vs. char array question

3. problem realloc array inside array of struct

4. problem realloc array inside array of struct

5. struct.array works - struct->pointer not

6. char* inside struct -- allowed?

7. char pointer to 2D char array

8. arrays and pointers, char * vs. char [], distinction

9. array of pointers to array of structs

10. qsorting & managing struct arrays, pointer arrays

11. Structure, where is one array inside the struct...

12. Array inside of a struct gets allocated statically?

 

 
Powered by phpBB® Forum Software