array of structures using pointers rather than array indices

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 3917 Views - Last Post: 20 April 2012 - 02:11 PM Rate Topic: -----

#1 pla2pimp  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 27-February 12

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.

#include<iostream>
#include<new>
#include<string>

using namespace std;

struct Age
{
    string name;
    int years;
};

void getDataFunc(Test *, int)

int main()
{

    // not sure if this is the right format for the object pointer
    Age *info;
    int numPeople;
    cout << " Enter the number of people: ";
    cin >> numPeople;
    // this part to. not sure how to format it. I do kno that this is to dynamically alocate the space needed to enter the number of people
    info = new int[numPeople];

    cout << "\n\n";
    cout << " Enter the following data. \n\n";
    // am i referencing them right in my function call
    getDataFunc(info, numPeople);

    cout << "\n\n";
    cout << " The data entered is: ";
    cout << " Name           Age \n";
    for ( int x = 0; x < numPeople; x++)
    {
        // the only thing i can think of to call the data is by using idices. I need to kno another way without using them(if possible)
        cout << " " << ( x + 1 ) << " " << info->name[x] << "        " << info->years << "\n";
    }

    return 0;
}

void getDataFunc( Age *ptr, int count)
{
    //not sure on how to input for a user defined amount of people with out using array indices.
    for ( int x = 0; x < count; x++ )
    {
        cout << " Name: ";
        cin >> info->name;
        cout << " Age: ";
        cin >> info->years;
    }
}
 


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.

Is This A Good Question/Topic? 0
  • +

Replies To: array of structures using pointers rather than array indices

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: array of structures using pointers rather than array indices

Posted 17 April 2012 - 03:30 AM

Age *info;

If info is a pointer to an array of Age structs, why would you use new int here?
info = new int[numPeople];

Was This Post Helpful? 0
  • +
  • -

#3 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: array of structures using pointers rather than array indices

Posted 17 April 2012 - 05:34 AM

I don't seem to get a grasp of what you mean by the heading.

Are you after a linked list or map or what?
Why array if no indices?
Was This Post Helpful? 0
  • +
  • -

#4 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: array of structures using pointers rather than array indices

Posted 17 April 2012 - 08:19 AM

void getDataFunc(Test *, int)

Where is Test defined? And you're missing a ;.

info = new int[numPeople];

As JackOfAllTrades said, you should allocate Ages, not ints.

cout << " " << ( x + 1 ) << " " << info->name[x] << "        " << info->years << "\n";

You can access an element of the array with info[x], so you can access the members with info[x].name and info[x].years.

cout << " Name: ";
cin >> info->name;
cout << " Age: ";
cin >> info->years;

info isn't defined here and you should access the members like I said above, or you'll input to the first element each time. This is why:
array[index]
//is the same as
*(array + index)
//so
array[0]
//is the same as
*(array + 0)
//or
*array

Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: array of structures using pointers rather than array indices

Posted 17 April 2012 - 11:25 AM

Compare the size of a integer to the size of Age. Is there a difference?

struct Age
{
	std::string name;
    int years;
};


int main() {

	std::cout<<"AGE SIZE (bytes) = "<<sizeof(Age)<<std::endl
	         <<"INT SIZE (bytes) = "<<sizeof(int)<<std::endl;

	std::cin.ignore();
	std::cin.get();
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 pla2pimp  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 27-February 12

Re: array of structures using pointers rather than array indices

Posted 17 April 2012 - 06:06 PM

Sorry it was a little messy. I built it in the comment section and I wanted to just write something off my head just to get a raw example lol.

JackOfAllTrades Im not even really sure what the "new int" is used for.
 
#include<iostream>

using namespace std;

int main()
{
    int *info; // is this the equivalent of: int info[];
    int numPeople = 0;


    cout << " Enter the amount of people to enter data for \n";
    cin >> numPeople;
    info = new int[numPeople]
// 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.
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

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; 


This post has been edited by jjl: 17 April 2012 - 07:47 PM

Was This Post Helpful? 0
  • +
  • -

#8 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: array of structures using pointers rather than array indices

Posted 17 April 2012 - 10:40 PM

Just out of curiosity, if someone knows what is meant here with "using pointers rather than indices", please tell me too.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

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.
#include <iostream>

using namespace std;

int main()
{
   int array[] = {1,2,3,4,5};
   int *arrp = array;

   for(size_t i = 0; i < sizeof(array)/sizeof(int); ++i)
   { // Normal array notation.
      cout << &array[i] << "  " << array[i] << endl;
   }
   cout << endl;
   for(size_t i = 0; i < sizeof(array)/sizeof(int); ++i)
   { // array pointer notation.
      cout << (array + i) << "  " << *(array + i) << endl;
   }
   cout << endl;
   for(size_t i = 0; i < sizeof(array)/sizeof(int); ++i)
   { // Pointer notation.
      cout << arrp << "  " << *(arrp) << endl;
      *(arrp++); 
   }
   return(0);
}



Jim
Was This Post Helpful? 2
  • +
  • -

#10 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: array of structures using pointers rather than array indices

Posted 18 April 2012 - 02:38 PM

I understand that "table[i]" is about defined to be the same as "table + i" where the "i" IS index.

So form table+i is not without index.

Another matter is moving back and forth with "table++" or "table--". There is no index.
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: array of structures using pointers rather than array indices

Posted 18 April 2012 - 03:12 PM

View Postturboscrew, on 18 April 2012 - 04:38 PM, said:

I understand that "table[i]" is about defined to be the same as "table + i" where the "i" IS index.


Not quite.

table[i] would be equivalent to *(table + 1). &table[i] would be equivalent to (table + 1).
Was This Post Helpful? 1
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

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?)

example
#include <iostream>

using namespace std;
int main() {
   int arr[5] = {1,2,3,4,5}; //array of integers
   int *p = arr; //pointer to the first element in the array
   do {
      std::cout<<*p++; // equivalent to std::cout<<*p; p++; (post increment) 
   } while(p != (arr + 5)); //loop while p does not point to last element in array
   return 0;
}



If we wanted to print the array in reverse order using the (--) operator, what we change in the code above?

This post has been edited by jjl: 18 April 2012 - 06:31 PM

Was This Post Helpful? 0
  • +
  • -

#13 pla2pimp  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 27-February 12

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.

 
#include<iostream>
#include<string>
#include<new>

using namespace std;

struct Age
{
    string name;
    int years;
}


void inputData(Age *, string, int);   // function to gather the input data
void displayData(Age *, string, int); // I need to practice passing arrays and 
                                      // such into functions

int main()
{
    int numPeople; // to hold the number of elements my struct will hold

    cout << " Enter the number of people to enter data for: ";
    cin >> numPeople;

    Age *info = new Age[numPeople];

    cout << "\n\n";
    cout << " Enter the following data for " << numPeople << " people. \n\n";

    inputData(info, numPeople);

    outputData(info, numPeople);
    
    return 0;
}

void inputData(Age *info, int numPeople)
{
    int x;
    for ( x = 0; x < numPeople; x++ )
    {
        cout << " Name: ";
        getline(cin, *(info + x).name);// not sure about the format here
                                       // getline(cin, info->name) is what i 
                                       // had before
        cout << " Age: ";
        cin >> *(info + x).years; // same here. I had
                                  // cin >> info->years;
                                  // what is the correct format for input the 
                                  // data to an array of structures when it is 
                                  // entered or called in a function
    }
}

void outputData(Age *info, int numPeople)
{
    int x;

    cout << "\n\n";
    cout << " Here is the data you have entered: \n\n";
    
    for ( x = 0; x < numPeople; x++ )
    {
        cout << " " << *(info + x).name << "        " << *(info + x).years << endl; // I am stumped right here to. is it info[x].name
      // or info[x]->name or info->name. PLEASE HELP

    }
    
}






Im still getting errors saying with my getline. and says expression must have class type when i try to input the string and int data.
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: array of structures using pointers rather than array indices

Posted 18 April 2012 - 07:46 PM

Why are you trying to use pointer notation instead of the more normal array notation?

There are two ways to address your variable in this function:
getline(cin, (info+x)->name); // Using pointer. Why?
getline(cin, info[x].name); // Normal array notation

Either of the above are correct.

Jim

This post has been edited by jimblumberg: 18 April 2012 - 07:46 PM

Was This Post Helpful? 0
  • +
  • -

#15 pla2pimp  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 27-February 12

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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2