Thread: Array of pointer variables....

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    56

    Array of pointer...

    Hello,

    I'm starting to learn pointers...
    i understand that :

    Code:
    ...
    int* p=new int [2];
    for (int i=0; i<2; i++)
         cout<<(*(p+i)).beta;
    ...

    But, I have trouble with array of pointer, something like this:

    alpha[i] -> beta = 13.5
    or (*alpha[i]).beta = 13.5

    Could someone give an exemple of array of pointer?
    Last edited by davidvoyage200; 03-06-2003 at 07:29 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Could someone give an exemple of array of pointer variable?
    Code:
    #include <iostream>
    
    int main ( void )
    {
      static int a[5] = {1,2,3,4,5};
      int *pa[5] = {&a[0], &a[1], &a[2], &a[3], &a[4]};
    
      std::cout<<"Address: "<< &a[2] <<" Value: "<< a[2] <<std::endl;
      std::cout<<"Address: "<< pa[2] <<" Value: "<< *pa[2] <<std::endl;
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    here is the little program i'm trying to do...

    Code:
    #include <iostream.h>
    
    struct patientRec
    {
    	int idNum;
    	int height;
    	int weight;
    };
    
    
    
    int main()
    {
    	patientRec* patPtrArray[20];
    	patPtrArray[3]->idNum = 6356;
    	cout<<patPtrArray[3]->idNum;
    	return 0;
    }
    this program does not work...why?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >patPtrArray[3]->idNum = 6356;
    Pointers have to point to a valid address before you can use them:
    Code:
    #include <iostream.h>
    
    struct patientRec
    {
      int idNum;
      int height;
      int weight;
    };
    
    int main()
    {
      patientRec* patPtrArray[20];
      
      patPtrArray[3] = new patientRec;
      patPtrArray[3]->idNum = 6356;
      cout<<patPtrArray[3]->idNum;
      
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    all right lets go thru it line by line...
    Code:
    #include <iostream.h>
    This is legal but poor form. You should be using the newer c++ style headers as the old iostreams library is deprecated.
    Code:
    struct patientRec
    {
    	int idNum;
    	int height;
    	int weight;
    };
    Again fine. Just a def for a struct.
    Code:
    int main()
    {
    This is good. We see a lot of void main here. That would have been bad but u didnt slip up there.
    Code:
    patientRec* patPtrArray[20];
    You know what this line does?
    It makes an uninitialised array of twenty pointers to a patientRec struct.
    Code:
    patPtrArray[3]->idNum = 6356;
    Now what silly thing have you done here? Answer that and u have solved your problem. This says store 6356 in the int member idNum of the struct pointed to by patPtrArray[3]. But you remember that that array is UNINITIALISED.
    What exactly are those pointers pointing at?
    Where did you allocate anything other than pointers?
    Code:
    cout<<patPtrArray[3]->idNum;
    	return 0;
    }
    This is good but std::cout and #include<iostream> would have been the order of the day.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Don't use #include <iostream.h> use #include <iostream> and using namespace std;
    Last edited by XSquared; 03-06-2003 at 08:08 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    thanks a lot!, but unfortunately, i have another problem with my program
    here is my code:

    Code:
    #include <iostream.h>
    
    struct patientRec
    {
      int idNum;
      int height;
      int weight;
    };
    
    int main()
    {
      patientRec* patPtrArray[3] = new patientRec;
      
    	for (int i=0; i<3; i++)
    		cin>>patPtrArray[i]->idNum;
    	for (int j=0; j<3; j++)
    		cout<<patPtrArray[j]->idNum;
      
      return 0;
    }

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    It would help if you told us the problem...

    Edit:
    Code:
    patientRec* patPtrArray[3] = new patientRec;
    That won't work... you have to initialise each element of the array.

    Code:
    patientRec* patPtrArray[3];
    for( int i = 0; i < 3; i++ )
        patPtrArray[i] = new patientRec;
    And you have to delete them all at the end of your program.

    Code:
    for( int i = 0; i < 3; i++ )
        delete patPtrArray[i];
    Code:
    #include <iostream.h>
    Don't do that.

    Code:
    #include <iostream>
    using namespace std;
    Last edited by XSquared; 03-06-2003 at 08:26 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    And you have to delete them all at the end of your program.
    Code:
    for( int i = 0; i < 3; i++ )
        delete patPtrArray[i];
    Or if you are as lazy as me do
    Code:
    delete [] patPtrArray;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer with Multi-dimensional Array
    By whichet in forum C Programming
    Replies: 7
    Last Post: 11-28-2007, 12:26 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. pointer to multidimensional array
    By Bigbio2002 in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2006, 10:29 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM