You are not logged in.

#1 2007-04-29 12:38:49

harlekin
Member
From: Germany
Registered: 2006-07-13
Posts: 408

C: appending pointer to array of pointer

Hello.
I am trying to code a html parser in C but I ran into some troubles.

Well... first the relevant code so that you know what I am talking about.
I have this structure which will hold an html tag.

struct tag {
        char             *name;
        char             *content;
        struct attribute *attributev;
        int               attributec;
        struct tag       *subtagv;
        int               subtagc;
        struct tag       *parent;
};

Then I want to write a function which takes two tags as arguments and adds the second tag to the array of tags (subtagv) of the first tag.

/* appends subtag in the tag's list of subtags. returns a pointer
 * to subtag (2nd argument) */
struct tag *tag_append(struct tag *tag, struct tag *subtag) {
        /* allocate space for another tag */
        tag->subtagv = realloc(tag->subtagv, sizeof(struct tag) * (tag->subtagc + 1));

        /* set tag->subtagv[tag->subtagc]'s address to subtag ...
         * but I don't know how to handle this */

        /* FIRST TRY: I can access tag->subtagv[tag->subtagc] via
         * tag->subtagv+tag->subtagc as I can access *int[10] via
         * *int+10. But this gives a gcc error:
         * "invalid lvalue in assignment" */
        /*tag->subtagv+tag->subtagc = subtag;*/

        /* SECOND TRY: copy subtag's address into tag->subtagv +
         * tag->subtagc's address with memcpy. But this will through
         * various glibc errors */
        /* memcpy(&tag->subtagv+tag->subtagc, &subtag, sizeof(struct tag*)); */

        tag->subtagc++;

        return subtag;
}

As a matter of completeness here the glibc runtime error:

*** glibc detected *** ./main: corrupted double-linked list: 0x0000000000544060 ***
======= Backtrace: =========
/lib/libc.so.6[0x2b710a876f3d]
/lib/libc.so.6[0x2b710a876fea]
/lib/libc.so.6[0x2b710a878d8f]
/lib/libc.so.6(malloc+0x7d)[0x2b710a87a93d]
./main[0x40094a]
./main[0x40072d]
./main[0x400669]
/lib/libc.so.6(__libc_start_main+0xf4)[0x2b710a82a2f4]
./main[0x4005b9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 fe:00 2011796                            /home/harlekin/code/html/main
00501000-00502000 rw-p 00001000 fe:00 2011796                            /home/harlekin/code/html/main
00502000-005a7000 rw-p 00502000 00:00 0                                  [heap]
2b710a6f2000-2b710a70c000 r-xp 00000000 fe:00 3155971                    /lib/ld-2.5.so
2b710a70c000-2b710a70e000 rw-p 2b710a70c000 00:00 0 
2b710a80b000-2b710a80c000 r--p 00019000 fe:00 3155971                    /lib/ld-2.5.so
2b710a80c000-2b710a80d000 rw-p 0001a000 fe:00 3155971                    /lib/ld-2.5.so
2b710a80d000-2b710a93b000 r-xp 00000000 fe:00 3155977                    /lib/libc-2.5.so
2b710a93b000-2b710aa3b000 ---p 0012e000 fe:00 3155977                    /lib/libc-2.5.so
2b710aa3b000-2b710aa3e000 r--p 0012e000 fe:00 3155977                    /lib/libc-2.5.so
2b710aa3e000-2b710aa40000 rw-p 00131000 fe:00 3155977                    /lib/libc-2.5.so
2b710aa40000-2b710aa47000 rw-p 2b710aa40000 00:00 0 
2b710aa47000-2b710aa54000 r-xp 00000000 fe:00 216919                     /usr/lib/libgcc_s.so.1
2b710aa54000-2b710ab53000 ---p 0000d000 fe:00 216919                     /usr/lib/libgcc_s.so.1
2b710ab53000-2b710ab54000 rw-p 0000c000 fe:00 216919                     /usr/lib/libgcc_s.so.1
2b710c000000-2b710c021000 rw-p 2b710c000000 00:00 0 
2b710c021000-2b7110000000 ---p 2b710c021000 00:00 0 
7fffa03a2000-7fffa03b8000 rw-p 7fffa03a2000 00:00 0                      [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vdso]
Abgebrochen

I think the first try would be the most elegant method but as I said I cannot realize it.
With the second try I am not quite sure if my arguments I'm passing to memcpy are right. I always get confused with pointers to pointers as I am quite new to it.

Thankful for any answer.
harlekin

Last edited by harlekin (2007-04-29 12:39:25)


Hail to the thief!

Offline

#2 2007-04-29 13:16:30

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: C: appending pointer to array of pointer

Your problem is that you've declared it as an array of structs, not as an array of pointers.  An array of pointers would be declared like this:

struct tag       **subtagv;

If you want to continue using an array of structs, you'll need to use memcpy, like this:

memcpy((tag->subtagv)+(tag->subtagc), subtag, sizeof(struct tag));

If you want to use an array of pointers, then simple assignment (where you got the invalid lvalue before) will work, after changing your declaration of subtagv.

Last edited by Cerebral (2007-04-29 13:18:55)

Offline

#3 2007-04-29 13:17:16

harlekin
Member
From: Germany
Registered: 2006-07-13
Posts: 408

Re: C: appending pointer to array of pointer

Thanks a lot! (=


Hail to the thief!

Offline

Board footer

Powered by FluxBB