set values of a struct pointer to a struct - pointers

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
}

Related

when to use to double pointers and pointers

// A C program to demonstrate linked list based implementation of queue
#include <stdio.h>
#include <stdlib.h>
struct QNode {
int key;
struct QNode* next;
};
struct Queue {
struct QNode *front, *rear;
};
struct QNode* newNode(int k)
{
struct QNode* temp = (struct QNode*)malloc(sizeof(struct QNode));
temp->key = k;
temp->next = NULL;
return temp;
}
struct Queue* createQueue()
{
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
void enQueue(struct Queue* q, int k)
{
struct QNode* temp = newNode(k);
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
void deQueue(struct Queue* q)
{
if (q->front == NULL)
return;
struct QNode* temp = q->front;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
free(temp);
}
int main()
{
struct Queue* q = createQueue();
enQueue(q, 10);
enQueue(q, 20);
deQueue(q);
deQueue(q);
enQueue(q, 30);
enQueue(q, 40);
enQueue(q, 50);
deQueue(q);
printf("Queue Front : %d \n", q->front->key);
printf("Queue Rear : %d", q->rear->key);
return 0;
}
The above code is from geeksforgeeks website.
in function calls they used pointer to struct,
in function definition they passed pointer to struct.
how it works, I thought we need to use double pointers , otherwise > it is pass by value instead of pass by reference.
the above code works fine, but i have doubt about it.
In main there is a variable q declared which is a pointer to a struct. The variable q is used as the function argument which means the function receives a pointer to the struct. The function can dereference the pointer and modify the struct. The variable q is technically passed by value because its value is a pointer and that's what the function receives. But you have to remember that q points to a struct that could be modified by the function.
Because this situation causes some confusion some people have tried to introduce new terminology like "pass by sharing" or "object sharing" to distinguish it from passing primitive values like an `int' by value.
If you had passed a pointer to a pointer then the function could have modified the variable q declared in main and changed it so it points to a completely different struct. That would be (technically) pass by reference because you are passing a reference to the variable.

character pointer vs int pointer

How this works:
int main()
{
int * ch = NULL;
cout<<"Hello"<<ch<<"World"<<endl;;
cout<<"Hello world1"<<endl;
return 0;`
}
Whereas this fails:
int main()
{
char * ch = NULL;
cout<<"Hello"<<ch<<"World"<<endl;;
cout<<"Hello world1"<<endl;
return 0;`
}
For most pointer types,
cout << ptr;
prints an implementation-defined representation of the pointer value, usually an address in hexadecimal format. But for char*, the operator<<() is overloaded to interpret the pointer as a pointer to the first char in a 0-terminated char array, and print it like printf("%s", ptr); would.
Printing the address a null pointer points to is harmless, following a null pointer to interpret the bytes starting from where it points to is undefined behaviour, and more often than not leads to a segmentation fault.

initializing a typedef struct pointer to NULL

#include<stdio.h>
typedef struct student{
int id;
int mark;
}stud;
typedef struct stud *s1;
void main(){
s1 = NULL;
printf("hi");
}
Please help me how to initialize struct pointer to NULL. i get the following error during compilation.
graph.c: In function ‘main’:
graph.c:11:04: error: expected identifier or ‘(’ before ‘=’ token
You meant to define the variable s1 as
stud *s1;
Live demo: http://ideone.com/9ThCDi
The reason you got the error you did is that you were declaring s1 to be a type for "pointer to struct stud". This is wrong for two reasons:
You didn't want s1 to be a type; you wanted it to be an object.
Your struct was struct student. But you defined an actual type called stud.
Use struct student *s1;
instead of
typedef struct stud *s1;
as far as i know, typedef is used when you are defining your custom data type only.

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.

pointer to struct - a few questions

#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.

Resources