Probably a dumb question, but I'm just curious. What do the variables *temp and *perm mean in this structure?
struct process {
int id;
char name;
} *temp, *perm;
Short version of
struct process {
int id;
char name;
};
process *temp;
process *perm;
This declares a struct type named process and then declares two variables which are pointers to process structs.
Related
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?
I have to create a memory management program using a char array. The code I need to edit is as follows:
char mem[65536];
void initialize(void){
// TODO
}
void* allocate(int Size){
// TODO
return ((void*) 0);
}
void deallocate(void* Pointer){
// TODO
}
// scan the memory returning the total free space
int free(void){
// TODO
return 0;
}
I am currently trying to use structs and store them in the char array, but that doesn't seem to be working out. The struct I am using is:
typedef struct Node{
int size;
int free;
struct Node *next;
struct Node *prev;
}Node;
I try to find the memory for the struct by:
Node *start = &mem;
Is there an error in the struct implementation, or should I be implementing something other than a struct to manage this?
EDIT:
Restrictions:
I have to store all data in the char array mem.
No global variables other than mem is allowed.
I came across the code below and I'm a little confused as to its purpose.
struct bob{
int myNum;
struct bob * next;
};
static struct bob_stuff{
int theNum;
struct bob *lists;
} bob;
I know the second struct is static and being intialized as a bob struct, but why would you do that? But I'm not really sure why'd you have 2 structs.
I think that it is the names of the structures that confuse you.
It is a definition of a single linked list.
The first structure
struct bob{
int myNum;
struct bob * next;
};
defines a node of the list. That it would be more clear I will rewrite it with different names
struct Node{
int value;
struct Node * next;
};
The second structure simply defines a head of the list and the number of nodes in the list (as I thik)
static struct bob_stuff{
int theNum;
struct bob *lists;
} bob;
So it can be rewritten like
static struct SingleLinkedList{
int nodes_count;
struct Node *head;
} list;
This abstract list is used as a container for some Bob stuff.:)
That looks like "module" (or "class", if you will) state for maintaining a bunch of singly-linked lists of integers.
Of course the naming is terrible, it should be e.g.
struct list_node {
int myNum;
struct list_node *next;
};
static struct {
int theNum;
struct list_node *lists;
} listState;
Note that the name ("struct tag") bob_stuff is pointless and confusing, and should probably be removed. If you want a static, thus local, variable of struct type, chances are the tag won't be useful anyway.
I have studied many example related to linked list example but they just explain in simple way using only one structure type. So i need to dig more to clear my doubts:
struct movies_t {
int number;
string title;
int year;
movies_t *next;
}
Does all of the nodes of a linked list necessarily should be of movies_t type (as its in array:array is a collection of values of similar type) ?
If i want to add a one or several more different new structure dynamically and those contains completely different elements except the int number; which is a common element in all data structure to sort data orderly. Is it possible ?
If I have to implement nested data structures(given below), would it be possible to do it with Linked list or with some tree data structure (Binary, B, B+ trees )?
struct movies_t {
int number;
string title;
int year;
}
struct friends_t {
int number;
string name;
string email;
movies_t favorite_movie;
}
struct world_t {
int number
string country;
string continent;
double population
movies_t favorite_movie;
friends_t friends_forever;
}
You can create a class rather structure for node. Do the following,
class parent_t{
int number;
parent_t* next;
}
class movies_t: public parent_t {
string title;
int year;
}
class friends_t : public parent_t {
string name;
string email;
movies_t favorite_movie;
}
class world_t: public parent_t {
string country;
string continent;
double population
movies_t favorite_movie;
friends_t friends_forever;
}
Now, you can create the linked-list of parent_t type. Then you can insert the movie_t, friends_t, world_t type objects into the linked-list.
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.