Thread: Structures, and pointers to structures

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    5

    Structures, and pointers to structures

    Ok, heres my problem.
    I made an array of pointers to structures, i initalized the array of pointers to structures by using the address operators to obtain the addresses of the array.
    so i did something like this:
    Code:
    struct bla blaStruct1 = {
    //ect
    };
    struct bla blaStruct2 = {
    //ect
    };
    struct bla blaStruct3 = {
    //ect
    };
    struct bla *ptrs[] = {&blaStruct1, &blaStruct2, &blaStruct3};
    
    char *x = &ptrs[0]->atwodemensionalstringarray[0][0];
    when i run the code i get a SIGBUS error, which if i remeber correctly is telling you that you are accessing memory that you are not supposed to. So what i am i doing wrong? I want to point to the char at [0][0] in structure that is in an array of pointers to structs. If anybody could shed some light on what i am doing wrong that would be great!

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516

    char *x = &ptrs[0]->atwodemensionalstringarray[0][0];
    you problem lies in this statement..lets have a look. ptr[0] itself is a pointer to a variable of type struct bla. now, in you statement, you say &ptr[0] which means address of the pointer itself, and not the address stored in ptr[0], which is the address of blastruct1. thus, you are trying to access something at the address of the pointer, which you are not supposed to use..the correct way would be..
    Code:
     char *x = ptrs[0]->atwodemensionalstringarray[0][0];
    note that the & sign isnt there in the statement..
    Last edited by PING; 03-30-2005 at 01:32 PM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What's more is that if atwodemensionalstringarray[0][0] is a character and you are trying to assign that to a pointer it will not work. I'm guessing you can probably get rid of the last [0].

    Code:
    char *x = ptrs[0]->atwodemensionalstringarray[0];
    However, without actually seeing the definition of your bla struct it's just a guess.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    However, without actually seeing the definition of your bla struct it's just a guess.
    True, but i guess, that we can leave it to the OP to think it out
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  2. Pointers to structures - Beginner question
    By RobJ in forum C Programming
    Replies: 6
    Last Post: 04-10-2006, 05:57 PM
  3. structures with pointers to structures
    By kzar in forum C Programming
    Replies: 3
    Last Post: 11-20-2004, 09:32 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 12:53 PM