I'm currently facing some problems accessing a double pointer.
1. The double pointer that is an element of a structure.
2. The double pointer is also instance of another structure.
3. That structure also contains an element that is explicitly a char type variable declared by typedef.
For example.
The main structure is this.
typedef struct SomeOne
{
NodeT **aOthers;
int height;
} SomeOne;
NodeT is defined as below:
typedef struct NodeT
{
NodeItemT info;
} NodeT;
NodeItemT is defined as below:
typedef char NodeItemT;
Now from the main function I want to add a value to the
NodeT **aOthers;
I have to declare SomeOne structure as follow:
SomeOne* somePerson;
Now from somePerson if I've to store a value to the "**aOthers" what I've to do?
To add a value I've a function defined as this:
void padd(SomeOne *somePerson, NodeItemT item);
Now can anyone please help me to define this function?
Here are your types:
typedef char NodeItemT;
typedef struct
{
NodeItemT info;
} NodeT;
typedef struct
{
NodeT **aOthers;
} SomeOne;
Here's how you can access them:
SomeOne so;
NodeT* others = new NodeT();
so.aOthers = &others;
(*so.aOthers)->info = 'A';
Does aOthers have to be a pointer to a pointer?
Related
struct Element{
Element() {}
int data = NULL;
struct Element* right, *left;
};
or
struct Element{
Element() {}
int data = NULL;
Element* right, *left;
};
I was working with binary trees and I was looking up on an example. In the example, Element* right was struct Element* right. What are the differences between these and which one would be better for writing data structures?
I was looking up from this website:
https://www.geeksforgeeks.org/binary-tree-set-1-introduction/
In C, struct keyword must be used for declaring structure variables, but it is optional(in most cases) in C++.
Consider the following examples:
struct Foo
{
int data;
Foo* temp; // Error in C, struct must be there. Works in C++
};
int main()
{
Foo a; // Error in C, struct must be there. Works in C++
return 0;
}
Example 2
struct Foo
{
int data;
struct Foo* temp; // Works in both C and C++
};
int main()
{
struct Foo a; // Works in both C and C++
return 0;
}
In the above examples, temp is a data member that is a pointer to non-const Foo.
Additionally, i would recommend using some good C++ book to learn C++.
In C++, defining a class also defines a type with the same name so using struct Element or just Element means the same thing.
// The typedef below is not needed in C++ but in C to not have to use "struct Element":
typedef struct Element Element;
struct Element {
Element* prev;
Element* next;
};
You rarely have to use struct Element (other than in the definition) in C++.
There is however one situation where you do need it and that is when you need to disambiguate between a type and a function with the same name:
struct Element {};
void Element() {}
int main() {
Element x; // error, "struct Element" needed
}
Why is this invalid?
typedef struct _NODE_struct_
{
int value;
std::vector<node_t*> neighbors;
} node_t;
When this is valid:
struct node_t
{
int value;
std::vector<node_t*> neighbors;
};
The first gives error: 'node_t' was not declared in this scope. All my main function does is declare an object n of type node_t. It does not instantiate or manipulate.
The correct way to express what you're trying to say is to typedef a forward declaration.
typedef struct _NODE_struct_ node_t;
struct _NODE_struct_
{
int value;
std::vector<node_t*> neighbors;
};
But (especially since this is C++, not C) it would be better to not even use the typedef:
struct node_t;
struct node_t
{
int value;
std::vector<node_t*> neighbors;
};
(and even in C you could write struct node_t*)
but at this point, it becomes obvious that you don't even need the forward declaration. Also, all names that end in '_t' are reserved, so:
struct Node
{
int value;
std::vector<Node*> neighbors;
};
(ordinarily I would give the spiel about avoiding pointers in containers, but it looks like the nodes are borrowed peers, so it is correct)
The first code bit is trying to combine a definition with a typedef before it happens. If you are coming from a scripting or function context where you are trying to map an anon class, this does not work for C++. (I realize the comments answer the question, I thought I'd write this answer anyway just for reference.)
I have this struct:
struct problem
{
int length;
struct node **x;
};
and I created a struct of this struct like this:
struct problem prob;
I can do this in C:
prob.x = Malloc(struct node *,prob.length);
but how I can do it in c++ style with new ? and why ?
In C++, it would be achieved with this.
std::vector<node *> problem(length);
The code you show is effectively emulating a small subset of the features of vector. Namely, an array-like container that is aware of its size.
Ok, this code might work, note that you are no longer holding a pointer to a pointer, but a simple array - which may or may not work for what you are trying to do:
typedef struct tagnode
{
...
} node;
typedef struct tagproblem
{
int length;
node *x;
tagproblem(int len) : length(len)
{
x = new node[length];
}
~tagproblem()
{
delete [] x;
}
} problem;
//Now create...
problem = new problem(2);
typedef struct {
int Key_value;
Node *link;
}Node;
Is the above declaration valid? or should I use
typedef struct _node{
int Key_value;
_node *link;
}Node;
No, it's not valid (as you would have noticed if you tried to compile it).
The typedef alias isn't introduced until the after the typedef, so you can't use it inside itself.
The second one isn't valid either (in C), _node is not a valid type name. You must use struct _node for the self-reference.
I tend to use pre-declaration and split it:
typedef struct Node Node;
struct Node {
int Key_Value;
Node *link;
};
The thing is that you can actually give the same name to both the structure and the typedef:
typedef struct Node {
int Key_value;
struct Node *link;
} Node;
Here I have added something which would have caused your code to not compile in a C compiler: The typedef isn't created until after the structure is defined. This means we must use the structure name in the link member declaration.
This can either be solved by giving the structure a name, as above, or by declaring the typedef first:
typedef struct Node *Node;
struct Node {
int Key_value;
Node *link;
};
Also note that in C++ you don't need to use the typedef keyword, or the struct keyword when declaring the link member:
// C++ version
struct Node {
int Key_value;
Node *link;
};
Structure (and class) names can be used as types in C++.
The first declaration is invalid, because Node is not known when you declare link as a structure member. The reason is that a declaration name is visible only after the declarator (simply put, that is after a comma, equal sign, or semi-colon). So, typedef being a declaration like any other, the name Node is only visible after the final semi-colon that ends the declaration statement.
Thus, you must use the second form (the first won't even compile). However, if you're on C, note that you should prepend the struct keyword to _node, like this:
typedef struct _node {
int Key_value;
struct _node *link;
} Node;
This is not necessary if you're on C++.
Both are invalid. Here's one valid way, for C and C++:
struct Node
{
int Key_value;
struct Node *link;
};
// if C, you can also do this
typedef struct Node Node;
The main point is that whatever the type of Link is, it must be something that's already been declared. The line struct X { .... declares that struct X is a type (but does not define it yet, but that's OK).
In C you should do:
typedef struct _node {
int Key_value;
struct _node *link;
} Node;
However, if you are using C++, it's simpler to omit the typedef at all:
struct Node {
int Key_Value;
Node* link;
}
If I create a structure in C++ like this:
typedef struct node {
int item;
int occurrency;
};
I know that a structure is allocated in memory using successive spaces, but what is the name of the structure (node in this example)? A simple way to give a name to the structure?
In C++ you don't have to use typedef to name a structure type:
struct node {
int item;
int occurrency;
};
is enough.
A pointer to an instance of that struct would be defined as node* mypointer;
E.g: You want to allocate a new instance with new:
node* mypointer = new node;
In C
struct node {
int item;
int occurrency;
};
is a tag, and by itself, it doesn't represent a type.
That is why you cannot do
node n;
You have to do
struct node n;
So, to give it a "type name", many C programmers use a typedef
typedef struct node {
int item;
int occurrency;
} node;
That way you can do
node n;
Instead of
struct node n;
Also, you can omit the tag and do the following
typedef struct {
int item;
int occurrency;
} node;
However, in C++ this all changes, the typedef syntax is no longer needed. In C++ classes and structs are considered to be user-defined types by default, so you can just use the following
struct node {
int item;
int occurrency;
};
And declare nodes like this
node n;
node is the name of the type. You can have multiple objects of that type:
struct node {
int item;
int occurrency;
};
node a;
node b;
In this example, both a and b have the same type (==node), which means that they have the same layout in memory. There's both an a.item and a b.item.