array of structures using pointers rather than array indices

Posted 17 April 2012 - 12:05 AM

I am having problems with structures and pointer. I have tried over and over to just understand it and something obvious is not clicking to me. Here is somewhat of a demo of what i understand. Please tell me where I am going wrong and why.

This is not a homework assignment. this is similar to what I am having problems with. I am looking for it to be able to to accept an integer for how many people I will be entering ages for. Enter their names and ages in pairs and then displaying them in order. And to do this with the use of pointers rather than array indices. Thank you for any help that you can give me.

// Does this allocate the pointers memory
// would it set the amount of data that *info will hold

return 0;

}
[/code]

this is using and array of structures

#include<iostream>
#include<string>
using namespace std;
struct Age
{
string name;
int years;
};
int main()
{
Age *info; // does this declare an object that can access the struct Age how many times that it
// is allocated for
info = new Age; // Does this allocate the size of the Age *info
// does dynamically allocating memory mean that it increases the memory of
// the Struct Age everytime the user inputs more pairs of data
cout << " Enter the data below: \n\n";
for ( int x = 0; x < ??; x ++ ) // or should i use a do while loop unless there is some way to
// set a variable equal to the amount of memory allocated after
// the user is done entering data.
// If it is a do while loop Im not sure what to the
// expression to tho. Is it something == null or != null. or
// something like that?
{
cout << " Name: ";
getline(cin, info->name);
cout << " Age: ";
cin >> info->years;
} // Im pretty sure i need a bool expression here somwhere to ask if there is more data
// to enter
//display results
cout << "\n\n";
cout << " Name Age \n\n";
for ( int x = 0; x < ??; x++ )
{
cout << " " << info.name[x] << " " << info.years[x] << endl;
// Ths what i am thinking would diplay the results but i always get errors when i do it this way
// Wow i just saw what u wrote about the form i was using. I put the indices on the wrong variable.
// Ok i see that now.
}
return 0;
}

I hope this helps you help me.

SOrry still learning how to format codes in here.Those are the only examples of using the new instance that i have seen.

Re: array of structures using pointers rather than array indices

Posted 17 April 2012 - 07:45 PM

/*This is a pointer that is pointing to nothing (or garbage I should say)
To "point" this pointer to memory we can point it to memory on the stack
or allocate "new" memory for it to point to on the heap */
Age *info;
/*This is creating brand new (dynamic) memory on the heap
and info is pointing to it, the memory is only allcoated once.
This only allocates enough memory for one Age object, if we want
to allocate an array of Age objects, then we can do so by the following
info = new Age[some number of elements] */
info = new Age;

Re: array of structures using pointers rather than array indices

Posted 18 April 2012 - 06:41 AM

Quote

Just out of curiosity, if someone knows what is meant here with "using pointers rather than indices", please tell me too.

When dealing with arrays you normally use index notation: array[x]. However it is also possible to use pointer arithmetic instead: *(array + x). Note the parentheses are required. This is possible because the array name is actually the address of the first element. You may want to consider using an actual pointer and assign that pointer to the start of your array. By using a pointer instead of the array name you can then do more operations, such as increment the pointer, which you can not do when using the array name.

Re: array of structures using pointers rather than array indices

Posted 18 April 2012 - 04:23 PM

Quote

Another matter is moving back and forth with "table++" or "table--". There is no index.

arrays of data are allocated sequentially in memory, if they didn't allocate this way then they wouldn't be considered "arrays". To get the data at the memory location n, you simply increment the memory location (n-1) (obviously right?)

Re: array of structures using pointers rather than array indices

Posted 18 April 2012 - 07:14 PM

Yea I understand how the pointer works. My problem is that all these methods dont work when my array is a structure that holds multiple elements. I need to be able to call it x mount of times to put in x amount of elements. "x" is user defined.

If i rewrite wthe code Ive been playing with in here and implement some of the corrections this is what I got.

Re: array of structures using pointers rather than array indices

Posted 18 April 2012 - 07:46 PM

Cant wait to get to get to the sorting in ascending order functions lol.
but for now i need to just figure out the inputs and outputs using sraay of structures and functions. Using pointers rather than indices but I do need to make sure that my knowledge of indices are worthy too. Finals are coming up and I am getting stuck on the last couple lessons and the rest I feel confident about. And thank you all again for helping me out. You all have made incredible points about programming and I have learned quite a bit, thanks to you guys in dreamincode.net.