Quick Question. For some reason I can't figure out how to get an array of structures if I am defining the size dynamically.

I can do it for a pointer i.e

struct DB
{
   string name;
   int age;
   string ice;
};

//some lines

DB* data1p;
    data1p= new DB[no1];

but i can't figure out how to do the equivalent of pointer = &variable.

Thanks for any help.

Incidently when you get stuck on little problems like this and can't figure them out where do you look for help?

The address of a variable returned by the reference operator (&) is not an l-value, meaning you can't put it on the left side of an assignment operator. Besides, why would you want to?

Hmm, perhaps you could do:

int x;
	x = *(new int[5]);
	(&x)[0] = 1;
	(&x)[1] = 1337;

but I believe this is a very bad idea. new is creating an array of five ints, and this array is being copied into x, leaving the original array somewhere in memory that is unknown to us, so we can't delete it later. Also, x only has room for one int, not 5, so copying 5 ints into x may intrude on memory belonging to someone else.

Perhaps this

#include <iostream>

int main ( )
{
	int &x = *(new int[5]);
	(&x)[0] = 1;
	(&x)[1] = 1337;
	std::cout<<(&x)[0]<<(&x)[1] ;
	std::cin.get() ;
}

again not advisable, for the above reasons

Thanks for all the replies people.

Besides, why would you want to?

Hmm I guess I was just trying to continue as I had been before i.e int* a; a=&b; Then using b unless I want to send it into a function or something but I guess this doesn't carry over in this case.
Just to check I have got the right end of the stick, you are saying it is best to just define a pointer and continually dereference it every time? There is no "normal" variable defined at the memory address of the pointer.

Hmm I guess I was just trying to continue as I had been before i.e int* a; a=&b; Then using b unless I want to send it into a function or something but I guess this doesn't carry over in this case.

I'm not following exactly...

a contains the address of b. modifying b is the same as modifying *a, until a is resigned to another address, or b goes out of scope (or a goes out of scope)

Just to check I have got the right end of the stick, you are saying it is best to just define a pointer and continually dereference it every time? There is no "normal" variable defined at the memory address of the pointer.

I'm not sure what you mean by "normal variable". Do you mean a primitive data type (i.e., int, float, double, char, short, long, etc.)? If so, there can definitely be a normal variable at the address of a pointer:

int* x = new int;//there is an int at the memory address of int
*x = 5;
//work with x, even past ends of scope
delete x;//you need to manually free the int, C++ won't do it for you when you use new

the value that x itself contains is a a memory address. you should only use x without the dereference operator if you're dealing with another pointer

int y = new int;
*y = 6;
delete x;//assuming x wasn't already deleted
x = y;//no dereference

I know the basics of working with int, float and structures but I am not as good with pointers I know how to do an array of structures for examples if the structure was called records i would normally do something like records.title for example. Since I dynamically allocated the memory I am struggling since I had to define a pointer to a structure and I can't figure out how to do the equivalent i've tried things like (*records).title and records->title.

Hope I've explained better this time.

records* myarray;
myarray = new records[numRecords];
myarray[i].title = "w/e";

or, you you mean:

records** myarray;
myarray = new records*[numRecords];
(*myarray[i]).title = "w/e";//same as...
myarray[i]->title = "w/e";

x[y] is equivalent to *(x + y) . If that's confusing, read "Pointers and Arrays" in this article

Basically, in the first example, [] is already dereferencing the pointer, so you can't dereference it again with * or ->.

records** myarray;
myarray = new records*[numRecords];
(*myarray).title = "w/e";//same as...
myarray->title = "w/e";

Storing values before allocating memory for 'em? I will be surprised if it works.

Storing values before allocating memory for 'em? I will be surprised if it works.

Oh, right. Sorry.

records** myarray;
myarray = new records*[numRecords];
for(int i = 0;i < numRecords;i++)
    myarray[i] = new records;
(*myarray[2]).title = "w/e";//same as...
myarray[2]->title = "w/e";
This article has been dead for over six months. Start a new discussion instead.