Thread: dynamic arrays of structs...

  1. #1
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    dynamic arrays of structs...

    this sounds silly, but some reason (which i can't spot) when i declrare an array of simple structs on the free store, in the following program i can't access them through (->) derefrerencing a pointer instead it only let's use the dot operator (.)...

    Code:
    #include<iostream>
    using namespace std;
    
    struct node
    {
    	int data;
    };
    
    
    int main()
    {
    	int size;
    
    	cout << "Enter the size: ";
    	cin >> size;
    
    	node *ptr = new node[size];
    	
    
    	for(int i = 0; i < size; i++)
    	{
    		cin >> ptr[i]->data;
    	}
    
    	
    	for(i = 0; i < size; i++)
    	{
    		cout << ptr[i]->data;
    	}
    
    
    	return 0;
    }
    i don't think i'm making a mistake when it comes time to declaring an array dynamically, however the problem pertains to the 2 (for) loops by trying to access (ptr[i]->data)....the only way to compile it is to use the (.) dot operator, but as my memory serves me correctly i'm supposed to use (->) when i try to access data through the pointer...weird....???

    hmmm.... either i haven't done this stuff for a while or i just don't see it....

    any help would be appreciated...


    matheo917

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    [n] dereferences a pointer at the n:th offset, so you have to use . instead of ->
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    let me see if i understand you correctly...

    by typing: ptr[i] --- whatever (i) is in this case, what i'm impilictly doing is derefrencing the pointer, so in result i must use (.) dot operator to access its data members....

    hmmm.....sounds reasonable, but i can't really picture this....
    hmmm....

    ptr....is a pointer to the first 'struct' in the array of the free store...


    can you elaborate on this a bit more ....

    thanx in advance


    matheo917

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    node * p = new node;

    p is a pointer to a single node. To reference the data component of the node you have to use the arrow operator or the derference opertor with the parentheisis combo.

    node *p1 = new node[size];

    p1 is a pointer to a collection of nodes, where the memory for the group is consecutive. In acutality p1 points to the first piece of memory for the section of memory reserved for the group of nodes. That means p1, for all intents and purposes, is the name of an array or is an array (the name of an array "can be considered" a constant pointer to the first element of the array) . Therefore use the dot operator. if you are using the [] preceding it and the -> if you are using just the name with offset, just like you would for an array declared with static memory/on the stack rather than on the free store/heap/dynamic memory.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Every pointer is potentially an array in memory, and then pointing to the first element of it.
    By using the [ ] operators, you pick out a specific element of the array and dereferencing it.
    If you don't intend the pointer to be an array, you usually use * to dereference it, but that is the same thing as using [0] (dereferencing the first element).

    Imagine this:
    Code:
    int MyArray[12];
    
    cout << MyArray[3];
    In this case, when using [ ], you get the actual data (an integer) and not a pointer to an integer.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    thanx

    thanx...that makes everything a lot clrearer....


    matheo917

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If you truly like dereferencing stuff so much:

    Code:
    (ptr+i)->member = something;

  8. #8
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    nifty....

    good to know

    thanx

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Or:

    cout << (&ptr[i])->data;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-04-2008, 10:39 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 11:32 AM
  4. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  5. dynamic arrays and structures
    By godofbabel in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2002, 03:45 PM