Pointer to struct

This is a discussion on Pointer to struct within the C++ Programming forums, part of the General Programming Boards category; Can anyone help me on this. If you have got an array of structures pointed to by a pointer, how ...

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359

    Question Pointer to struct

    Can anyone help me on this. If you have got an array of structures pointed to by a pointer, how do you access the subscript and then the subsequent members of that structure?
    Thanks
    Be a leader and not a follower.

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    struct house {
        int people;
        int toilets;
        char *address;
    }
    
    house myStreet = new house[5];
    
    myStreet[2]->people = 1;
    myStreet[4]->address = new char[strlen((const char *)"4 Subdene")  + 1];
    
    strcpy(myStreet[4]->address, "4 Subdene");
    myStreet[4]->address[0] = '5';
    Something like that?
    Last edited by Dual-Catfish; 06-30-2002 at 04:29 PM.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Lets say that i defined a structure as follows :


    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct Person
    {
           char* name;
           char* surname;
    };
    
    ...
    
    int main()
    {
           struct Person  student[20];
           struct Person*ptrStudent;
           int i;
           ptrStudent = student;             //Initialize the pointer
           
           for(i=0;i<20;i++)
           {
                ptrStudent[i]->name = (char*)malloc(20);
                ptrStudent[i]->surname = (char*)malloc(20);
           }
           
           return 0;
    }
    "ptrStudent[i]->name" is the same as saying
    "(*ptrStudent[i]).name" or it should be.

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Thanks, for your replies, thats helped alot. Dual just one quick question. In this section of code when you are assigning the length of the variable,

    Code:
    myStreet[4]->address = new char[strlen((const char *)"4 Subdene")];
    why can't you do:-

    Code:
    myStreet[4]->address = new char[strlen("4 Subdene")];
    don't fully understand what the const char is there for.

    Thanks.
    Be a leader and not a follower.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Since a "string" is already a const char *, it seems entirely redundant to say it

    Besides, it's needs to be
    myStreet[4]->address = new char[ strlen("4 Subdene") + 1 ];

    Gotta remember to add space for that all important \0 at the end of the string

  6. #6
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    this thing below is wrong ...
    Code:
           struct Person*ptrStudent;
           for(i=0;i<20;i++)
           {
                ptrStudent[i]->name = (char*)malloc(20);
                ptrStudent[i]->surname = (char*)malloc(20);
           }
    it should be :

    Code:
           struct Person*ptrStudent;
           for(i=0;i<20;i++)
           {
                ptrStudent[i].name = (char*)malloc(20);
                ptrStudent[i].surname = (char*)malloc(20);
           }
    the subscript operator
    here
    ptrStudent[i]
    access the value pointed by ptrStudent
    and this value is NOT a pointer !
    so using '->' (access fields by pointer) is wrong.
    it should be '.' (access fields by value/object)

    be carefull what you write ...

  7. #7
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    I have been declaring the pointer to the structure a different way.

    Code:
    struct House
    {
      //whatever
    };
    
    House* Street;
    
    Street = new House[NUM_HOUSES];
    is this wrong?
    Be a leader and not a follower.

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Nope.

  9. #9
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Well everytime I try to access a member i get the following error:

    [C++ Error] New.cpp(48): E2288 Pointer to structure required on left side of -> or ->*

    Code:
    #include <iostream.h>
    
    const int MAX_ADDRESS=50;
    
    struct House
    {
      int NumWindows;
      char Address[MAX_ADDRESS];
    };
    
    int main(void)
    {
      const int NUM_HOUSES=5;
      
      House *Street;
    
      Street = new House[NUM_HOUSES];
    
      strcpy(Street[1]->Address, "22 Holgate rd\0");
    
      cout << Street[1]->Address;
      getchar();
    
      return 0;
    }
    Be a leader and not a follower.

  10. #10
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Can someome have a quick look at the problem, I can't go any further until this is resolved.

  11. #11
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    here we go.....hope it ll work as u would like.........
    --------------------------------------------------------------------------------
    #include <iostream.h>
    #include <conio.h>
    const int MAX_ADDRESS=50;

    struct House
    {
    int NumWindows;
    char Address[MAX_ADDRESS];
    };

    int main(void)
    {
    const int NUM_HOUSES=5;

    House* Street= new House[NUM_HOUSES];

    strcpy(Street[1].Address, "22 Holgate rd\0");

    cout << Street[1].Address;
    getche();

    return 0;
    }
    Programming is a high logical enjoyable art for both programer and user !!

  12. #12
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Thanks, for the reply. That got the problem sorted. How come I did not have to use the -> operator, as I delcared a pointer to an object?
    Be a leader and not a follower.

  13. #13
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    thats because yr using (ARRAY) of the strucure, so u ve to use the (.) while u ve to use ( ->) if ur using a pointer to single structure.

  14. #14
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Ta, thanks for everyones help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 10:45 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. "dereferencing pointer to incomplete type"
    By incognito54 in forum C Programming
    Replies: 2
    Last Post: 11-01-2005, 08:50 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21