Solved

array of struct?

Posted on 2008-10-24
14
260 Views
Last Modified: 2012-05-05
say I have:





and I want to create an array of TreeNode, how do I do that?
typedef struct treeNode   

   { ..................

     .......................

   } TreeNode;

Open in new window

0
Comment
Question by:kuntilanak
  • 5
  • 5
  • 3
14 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22801324
Declare a pointer:
TreeNode *nodes;

Then you can create the array dynamically:

nodes = new TreeNode[somesize];

but these will be unsual since a tree is not used to be stored in an array, but in some kind of dynamically collection.
0
 

Author Comment

by:kuntilanak
ID: 22801340
I don't think that C has a new operation, what if I do:

TreeNode * arr[100];

will that work?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22801369
>>I don't think that C has a new operation
You put your question in the C and C++ areas

>>what if I do:
>>TreeNode * arr[100];
You just need:
TreeNode arr[100];

0
 

Author Comment

by:kuntilanak
ID: 22801505
okay, so TreeNode arr[100];

will also work for me?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22801641
yes, it will.
0
 

Author Comment

by:kuntilanak
ID: 22801707
someone recommend me to use malloc, how can I do that?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22801735
if you need a fixed set of object, you can work as described.
if you don't know the number of elements at design time, then malloc will help you better, as the 'new' operator does in C++. Something like:

TreeNode *arr = (TreeNode *)malloc(sizeof(TreeNode) * num_elements);

where you have to define num_elements.
But notice you are responsible to free that memory somewhere with the free() function, like:

free(arr);
0
 

Author Comment

by:kuntilanak
ID: 22801799
Why is it not TreeNode * arr[100]; instead of TreeNode arr[100];??
0
 

Author Comment

by:kuntilanak
ID: 22802149
and say I want to create a function to traverse the array in order to find a specific value, will this work
static TreeNode * traverseArray(TreeNode * array[], TreeNode * t){

for (int i = 0; i<100; i++){

  if (strcmp(array[1]->name, t->name) == 0)

	return t;

}

return NULL;

}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22806471
>> Why is it not TreeNode * arr[100]; instead of TreeNode arr[100];??

Because then it would be an array of pointers to TreeNode, not an array of TreeNode's as you wanted :)

You can do that of course, but you'd need to modify the rest of the code to work with that.


>> and say I want to create a function to traverse the array in order to find a specific value, will this work

The idea is good, but there's a few mistakes :
TreeNode *traverseArray(TreeNode *array[], TreeNode *t) {

  int i = 0;

  for (i = 0; i < 100; i++) {                                      /* <--- will the array always have size 100 ? */

    if (strcmp(array[i]->name, t->name) == 0) {                    /* <--- use i as index ! */

      return array[i];                                             /* <--- return the element from the array */

    }

  }

  return NULL;

}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22808825
Didn't jaime_olivares answer your question ? I merely answered a follow-up question. You need to take care in accepting the right answers. Not only in order not to offend the experts who took their time to help you, but also to have a usable PAQ database.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 22817564
I think a split woud be a fair decision.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22819868
kuntilanak : the reply you accepted does not answer your initial question. You need to make some effort to identify the reply (replies) that answered your question, and accept those. Otherwise, it'll be more difficult for other people to find the answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unable to start eclipse ? 17 128
How can i compile this github project?? 2 74
Acrinis True image 2 55
Adjust the position 3 47
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now