pointer to struct - a few questions - pointers

#include<stdio.h>
#include<stdlib.h>
/* Link list node */
struct node
{
int data;
struct node *next;
};
/* Function to reverse the linked list */
static void reverse(struct node** head_ref)
{
struct node *prev = NULL;
struct node *current = *head_ref;
struct node *next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
What are the lines that start with struct in the reverse function responsible of?
do they extend the original struct or creating new structs that the original struct pointing to? I don't really understand why there is no name to the original struct
Is there a diffrence between struct node *next; and struct node* next; ?

Line struct node *prev is declaration of variable prev of type "pointer to struct node". These lines just declare some local variables. prev contains a pointer to the last processed node, current contains a pointer to the currently processing node and next is used to save a pointer to the next node of original list.
There's no difference between struct node *next and struct node* next.

#willys is right. As we know struts is group of similar and un-similar datatype.When struct is created , it allocates a block of memory.And that memory has an address.
struct node{
int age;
char name[20];
struct node *next_address; //Address of its type (self referential structure)
}
This above struct allocates a block of memory . Inside this block 3 different data is storing (age,name and a address of structure node)
When you want to store more blocks (for storing more data ) , you need to allocate more struct.But, there is a problem when all structs are allocated in memory , they don't have any relation between each other.Its a cause of memory leak.
So, keep the address field on each block of allocated memory, so that any block of memory can store the address of its nearest block.
Its , the real flavor value of Linked List.So, there is no confusion about the name of the struct.

Related

How to create a dynamic allocation array with unknown size in struct in c++

I was trying to create a struct with an unknown size array. here's what I think, but it seems to have a problem because memcheck report an error.I'm not sure if this is legal in c++
struct information
{
std::string name;
int x=0;
int* score=new int[x];
}

set values of a struct pointer to a struct

Is it possible to set the values of a struct pointer in a struct? I get an error and cannot typecast myStruct* into myStruct.
typedef struct {
int foo;
int bar;
} myStruct;
int main() {
myStruct *pS;
myStruct S1 = {0,0};
myStruct S2;
pS = S1;
S2 = pS; // I get an error there, cannot set struct pointer to a struct
}
So, in your example, you have pointer pS and regular variable S1.
A pointer is a variable that stores the memory address as its value.
Variable is the name of memory location.
So, the difference between regular variable is that variable stores value of an object, but pointer stores memory address of an object.
There are operators which allow getting object's address and getting object value by it's address:
Address-of operator &. &a will return address of object a.
Dereference operator *. *p will return object stored by address p.
Thus, in your code you should get two errors:
pS = S1; // error: Trying to assign struct value to a pointer
S2 = pS; // error: Trying to assign pointer to a struct value
To fix this, you should assign address to a pS and value to S2
typedef struct {
int foo;
int bar;
} myStruct;
int main() {
myStruct *pS;
myStruct S1 = {0,0};
myStruct S2;
pS = &S1; // getting address of S1
S2 = *pS; // getting value stored by address pS
}

Printing Address of Struct Element

I have the following struct:
typedef struct Author
{
char** novels;
} Author;
And I want to print the address of an element in the novels array. I tried these two:
printf("%p\n", &(herbert->novels[1]));
printf("%p\n", herbert->novels[1]);
But I'm not sure which is correct. Can someone help me understand which to use and why?
Take a look at the below...
typedef struct Author
{
char** novels;
} Author;
int main()
{
Author a;
char b = 'b';
a.novels = new char*[2];
a.novels[0] = NULL;
a.novels[1] = NULL;
printf("1. %p\n", a.novels[1]);
printf("2. %p\n", &(a.novels[1]));
delete[] a.novels;
return 0;
}
this outputs the following
1. 0000000000000000
2. 00000000001269C8
You can see the first print is actually a NULL - which is the value stored at the a.novels[1].
The second is the address of the a.novels[1] memory.
Assuming you look for the memory address of the item, you'll need the second syntax
printf("%p\n", &(herbert->novels[1]));

Pointers to stack

I am sorry that I cannot support my question with some code (I didnt understand how to structure it so it would be accepted here), but I try anyway.
If I understand correctly, a struct that references a struct of same type would need to do this with contained pointer for reference. Can this pointer reference to allocated space on the stack (instead of the heap) without creating segmentation fault? -
how should this be declared?
Yes, you can use pointers to variables on the stack, but only when the method that provides that stack frame has not returned. For example this will work:
typedef struct
{
int a;
float b;
} s;
void printStruct(const s *s)
{
printf("a=%d, b=%f\n", s->a, s->b);
}
void test()
{
s s;
s.a = 12;
s.b = 34.5f;
printStruct(&s);
}
This will cause an error however, as the stack frame would have disappeared:
s *bad()
{
s s;
s.a = 12;
s.b = 34.5f;
return &s;
}
EDIT: Well I say it will cause an error, but while calling that code with:
int main()
{
test();
s *s = bad();
printStruct(s);
return 0;
}
I get a warning during compilation:
s.c:27:5: warning: function returns address of local variable [enabled by default]
and the program appears to work fine:
$ ./s
a=12, b=34.500000
a=12, b=34.500000
But it is, in fact, broken.
You didn't say what language you are working in, so assuming C for now from the wording of your question... the following code is perfectly valid:
typedef struct str_t_tag {
int foo;
int bar;
struct str_t_tag *pNext;
} str_t;
str_t str1;
str_t str2;
str1.pNext = &str2;
In this example both str1 and str2 are on the stack, but this would also work if either or both were on the heap. The only thing you need to be careful of is that stack variables will be zapped when they go out of scope, so if you had dynamically allocated str1 and passed it back out of a function, you would not want str1->pNext to point to something that was on the stack within that function.
In other words, DON'T DO THIS:
typedef struct str_t_tag {
int foo;
int bar;
struct str_t_tag *pNext;
} str_t;
str_t *func(void)
{
str_t *pStr1 = malloc(sizeof(*pStr1));
str_t str2;
pStr1->pNext = &str2;
return pStr1; /* NO!! pStr1->pNext will point to invalid memory after this */
}
Not sure if this is specifically a C/C++ question, but I'll give C/C++ code as example in anyway.
The only way you can declare it: (with minor variations)
typedef struct abc
{
struct abc *other;
} abc;
other can point to an object on the stack as follows:
abc a, b; // stack objects
b.other = &a;
This is not a question about scope, so I'll skip commenting on possible issues with doing the above.
If, however, you want to assign it to a dynamically created object, there's no way this object can be on the stack.
abc b;
b.other = malloc(sizeof(abc)); // on the heap

Pointers in structure

I'm trying to use a file pointer that I have declared in a structure of linked list, but I keep getting it as a NULL value.
I have the following structure:
struct _hash_table
{
char found;
struct _hash_chain *hash_chain;
}
struct _hash_chain
{
uint64_t value;
FILE *fout;
struct _hash_chain *next;
}
and
struct _hash_table hash_table[TABLE_SIZE];
I keep getting hash_table[i]->hash_chain->fout = NULL and it's pointer address is nil.
Do I need to dynamically allocate memory for the pointer?
struct _hash_table hash_table[TABLE_SIZE]; - This will not allocate memory for struct _hash_chain because hash_chain is pointer variable in _hash_table.
...
struct _hash_table hash_table[TABLE_SIZE];
for (i = 0; i < TABLE_SIZE; i++);
{
hash_table[i].hash_chain = (struct _hash_chain *)malloc(sizeof(struct _hash_chain));
memset(hash_table[i].hash_chain, 0, sizeof(struct _hash_chain));
}
//Then do file open for TABLE_SIZE times
//hash_table[0].hash_chain->fout = fopen("file.txt", "w");
...
Accssing h_table[i].hash_chain without dynamic memory allocation will leads to crash(an undefined behaviour). I hope you will take care of next pointer.

Resources