Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing a pointer to a pointer.

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hey guys/gals,

I am trying to store a few strings in the below

struct variables{
char **name;
}var[50];

I want to store lets say 5 names in var[0].name,but how do I reference var[0].name[1]???I keep getting an error.

Thanks
 
First i think you must allocate some memory from the heap!
(remeber new operator?), becose your pointer is null.

Then try something:(It works on my computer).

struct variables{
char **name;
}var[50];

*(var[0].name[0])='1';

Good luck !

cliff
 
I do not really understand why you do it like this
the way I would do it

struct variables
{
name[5][20];

}var[50]

5 is the number of strings, 20 is the max string length
if you do char ** name you define a pointer that references to a char pointer, rather complicated I think.

The solution of cliff is not very correct to my opinion, you still have to assign memory of the heap with new,
If you like to work with pointers :

struct variables
{
char *name[5];

} var[50];

for(int i=0;i<5;i++)
var[0].name=new (20);
strcpy(var[0].name[0],&quot;this is a string&quot;);
In this case 20 is the length of the string


Wim Vanherp
Wim.Vanherp@belgacom.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top