pass scanf value to struct pointer Segmentation Fault - c

All I want to do is a basic printing with a struct pointer using user input. I got a segmentation fault when I try to do with below code. I'm new to c, thanks anyway.
typedef struct {
int *licenseNum;
char *name;
char *region;
} City;
typedef struct {
struct Node *current;
struct Node *head;
struct Node *tail;
struct Node *next;
struct Node *secondNext;
City *data;
} Node;
int main()
{
Node *node = malloc(sizeof(Node));
City *city = malloc(sizeof(City));
puts("License number of the City: ");
scanf("%d", &(node -> data -> licenseNum));
printf("%d", node -> data -> licenseNum);
return 0;
}

You didn't set data in node.
Node * node = malloc(sizeof(Node));
City * city = malloc(sizeof(City));
// node->data is not yet defined!
// It has a random value! You must first initialize it:
node->data = city;
Also you shouldn't use malloc here as memory allocated by malloc has random values. Only use malloc if you for sure initialize all the pointers in a struct with meaningful values prior to using them. It's much safer to use calloc:
Node * node = calloc(1, sizeof(Node));
City * city = calloc(1, sizeof(City));
node->data = city;
calloc works like malloc, but it guarantees that the returned memory is all set to zero (all int values are 0, all pointers are NULL). The first argument of calloc (1 in the code above) is the number of items you want to allocate, here it is just one. E.g. calloc(5, sizeof(City)) would allocate memory for 5 cities in a single block, e.g.:
Cities * cities = calloc(5, sizeof(City));
cities[0].name = "New York";
cities[1].name = "London";
// ... and so on

You didn't initialize node->data.

You allocated memory for node, but not for node->data.
You probably wanted to do this: node->data = city

Related

meaning of a struct within a struct in c [duplicate]

This question already has answers here:
What is self-referencing structure in C?
(3 answers)
Closed 3 years ago.
Can someone explain what we mean when we do, like what does struct Node* next do. does it create a pointer of type struct? any help and resources about structures in c would be helpful
struct Node {
int dest;
struct Node* next;
};
"struct" itself is not a type. "struct [tag]" is a type, for example "struct Node" in your code.
In your case you define a structure type. Every structure of that type will contain a pointer to another structure of that type as a member called "next".
This allows you to chain the structures together in a so called linked list. You store a pointer to the first structure in a variable, then you can follow the chain of links down to the structure you need.
For example, you can do
struct Node *start;
start = malloc(sizeof struct Node);
start->dest = 7;
start->next = malloc(sizeof struct Node);
start->next->dest = 13;
start->next->next = malloc(sizeof struct Node);
start->next->next->dest = 19;
printf("%d %d %d\n", start->dest, start->next->dest, start->next->next->dest);
free(start->next->next);
free(start->next);
free(start);
Please note that this code omits all error handling, in real code you have to handle the case when malloc returns NULL.
Also, in real code you would use such a structure in loops that traverse the chain, not directly as above.
As #Serge is pointing out in comments, is not a struct within a struct, is a reference (a pointer) to an object of the same type, an example:
#include <stdio.h>
struct Node {
int dest;
struct Node* next;
};
int main(void)
{
/* An array of nodes */
struct Node nodes[] = {
{1, &nodes[1]}, // next points to the next element
{2, &nodes[2]}, // next points to the next element
{3, NULL} // next points to null
};
/* A pointer to the first element of the array */
struct Node *node = nodes;
while (node) {
printf("%d\n", node->dest);
node = node->next; // node moves to the next element
}
return 0;
}
Output:
1
2
3
Of course, in my example there is no benefit in using a linked list, linked lists are useful when we don't know the number of elements before-hand.
Another example using dynamic memory:
struct Node *head, *node;
node = head = calloc(1, sizeof *node);
node->dest = 1;
while (more_elements_needed) {
node->next = calloc(1, sizeof *node);
node->next->dest = node->dest + 1;
node = node->next;
}
for (node = head; node != NULL; node = node->next) {
printf("%d\n", node->dest);
}

Value of struct variable changes after malloc of other variable of same struct

So i am trying to initiliaze a node of a graph. The problem is that the value of the variable node->edges_capacity changes from 2 to some random int for no apparent reason, at least to my knowledge.
Moreover, when i use the debug mode of CLion, it prints 2 as expected!
Am i doing something wrong with malloc?
typedef struct node {
int id;
int degree;
int edges_capacity;
struct node **edges;
} Node;
Node *node_create(int id) {
Node *node = (Node *) malloc(sizeof(Node *));
if (node == NULL)
exit(1);
node->id = id;
node->degree = 0;
node->edges_capacity = 2;
node->edges = (Node **) malloc(node->edges_capacity * sizeof(Node *));
node_debug(node); // prints a random int for node->edges_capacity instead of 2
return node;
}
void node_debug(Node *node) {
printf("id:%d degree:%d capacity:%d\n", node->id, node->degree, node->edges_capacity);
}
this line:
Node *node = (Node *) malloc(sizeof(Node *));'
has a couple of problems.
need to be allocating enough room for the full size of a Node, not a pointer to a Node.
when call any of the heap allocation functions (malloc, calloc, realloc) the returned type is void*, which can be assigned to any other pointer. Casting the returned value just clutters the code, making int much more difficult to understand, debug, maintain.
The following, posted code is writing to areas in the heap memory that the program does not own. This is undefined behavior and can lead to a seg fault event. Your noticing one aspect of that undefined behavior.
Change this line :
Node *node = (Node *) malloc(sizeof(Node *));
to :
Node *node = malloc(sizeof(Node));
as you declare node to be a pointer to type Node, and not to Node *.
Also, see this link on why not to cast the result of malloc.

Struct and Pointers

So in my code I have two structures. The first is a node which contains an int value and a pointer to another node. The second structure is used to create an array of 10 pointers each point to a another node. And it also contains link2 which will be used to traverse the array and all the nodes that it points to. I'm trying to add 3 nodes each holds the value 3 into the third index of the array. The pointer in the third index should point to the first 3 then that should point to the second three and so on. When I put add(a,3) three times and then print i get segmentation fault. I tried tracing the code but that still didn't make any sense to me because I always end up with three nodes. Can someone point me in some direction? Thanks!:)/>
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int x;
struct node *link;
};
struct listofnodes
{
struct node *alist[10];
struct node *link2;
};
addFirst(struct listofnodes *a, int num)
{
struct node *nodeone = (struct node *)malloc(sizeof(struct node));
nodeone->x = num;
a->alist[num] = nodeone;
// printf("IT WENT THROUGH\n");
}
add(struct listofnodes *a, int num)
{
struct node *current;
current = a->alist[3];
struct node *nodeone = (struct node *)malloc(sizeof(struct node));
nodeone->x = num;
current->x = 5;
{
while (a->alist[3] != NULL)
{
if (a->alist[3]->link == NULL)
{
a->alist[3]->link = nodeone;
printf("IT WENT THROUGH\n");
break;
}
a->alist[3] = a->alist[3]->link;
}
}
}
main(void)
{
struct listofnodes *a =
(struct listofnodes *)malloc(sizeof(struct listofnodes));
// a->alist[3]=NULL;
addFirst(a, 3);
add(a, 3);
add(a, 3);
add(a, 5);
}
In your main() function you have:
struct listofnodes *a =
(struct listofnodes *)malloc(sizeof(struct listofnodes));
"a" is now a pointer to a listofnodes-sized block of memory containing random locations. You probably want to do something like
a->link2 = NULL;
Also, in your add() function, you have...
add(struct listofnodes *a, int num)
{
struct node *current;
current = a->alist[3];
struct node *nodeone = (struct node *)malloc(sizeof(struct node));
nodeone->x = num;
You have malloc()d random memory and put "num" into x, but you need to put a null into where 'link' goes, i.e.
nodeone->link = NULL;
When you create the memory block using malloc(), you have to manually make sure the 'link' member is set to NULL - otherwise the number left there from last use will point who knows where...

Linked List Null in C

i dont know why the list returned is NULL, this is the code:
In my List.h
struct nodo_ {
char* dato;
struct nodo_ *next;
};
struct nodo_ *Lista;
/*Def list */
void createList(struct nodo_ **Lista);
in my main.c
struct nodo_ *Lista;
int main(){
createList(Lista);
while(Lista != NULL){
printf("The date is %s\n ",Lista->dato); //Error here now
Lisa = Lista->next;
}
return 0 ;
}
in my List.c im create the List :
void createList(struct nodo_ *Lista){
struct nodo_ *Aux_List = list_D;
aux_List = malloc(sizeof(struct nodo_));
char* path_a = "Hello";
char* path_B = "Minasan";
/* Store */
aux_List->dato = path_a;
aux_List = Aux_List->next;
aux_List = malloc(sizeof(struct nodo_));
aux_List->dato = path_b;
aux_List->next = NULL;
}
Thanks.
That pointer is being passed by value, i.e., a copy is made. If you wish to initialize the pointer to a completely new value then you must use another level of indirection (i.e., a nodo_**).
On a side note, typedefing pointer types is almost always a bad idea unless the type is truly opaque (which yours is not). One reason for this "rule" is evident when you consider another bug in your code:
auxList = (Lista*)malloc(sizeof(Lista));
You're allocating space for a pointer to noda_, not enough for a noda_ object. Also, don't cast the return value of malloc in C. It is redundant as a void* is safely and implicitly converted to any other pointer type and, if you forget to include stdlib.h, malloc will be assumed to be a function which returns int, and the cast hides the error. (only applies to compilers which implement C89 or an older version)
EDIT:
To initialize a pointer argument within a function:
void init(struct node **n) {
if(n)
*n = malloc(sizeof(struct node));
}
int main() {
struct node *n;
init(&n);
}
Short answer to your actual question before I dig into the code:
... why the list returned is NULL ...
There is no returned list, you neither use return to pass a result, nor set the value of an out parameter.
In your edited code:
void createList(struct nodo_ **Lista){
struct nodo_ *Aux_List = list_D;
aux_List = malloc(sizeof(struct nodo_));
you first set Aux_List to the current value of Lista, which you know isn't initialized yet, because you're trying to initialize it. Then you discard that value, overwriting aux_List with a new address returned by malloc. You never store anything into *Lista, which would be the only way for this function to work as declared.
As Ed suggests, your typedef is hiding lots of useful information from you, so let's expand it out
struct nodo {
char* dato;
struct nodo *next;
};
/*Def list */
void createList(struct nodo* list_D);
Now, you can see this createList is wrong: you can pass in the head node of a list (which is no use to it anyway), but there is no way for it to return a newly-allocated list to the caller.
Frankly your createList isn't a useful primitive anyway, so I'm going to start with a sensible foundation first:
struct nodo *alloc_nodo(char *dato, struct nodo *next)
{
struct nodo *n = malloc(sizeof(*n));
n->dato = dato;
n->next = next;
return n;
}
Now, before we re-write your createList using this, let's see what it does now:
void createList(struct nodo *list_D)
{
struct nodo *aux_List = list_D;
aux_List = malloc(sizeof(struct nodo_));
/* ^ so, we take the input argument and immediately discard it */
char* path_a = "Hello";
char* path_B = "Minasan";
/* Store */
aux_List->dato = path_a;
aux_List = Aux_List->next;
/* ^ note that we haven't initialized aux_List->next yet,
so this is a random pointer value */
aux_List = malloc(sizeof(struct nodo_));
/* again, we set aux_List to something,
but immediately overwrite and discard it */
aux_List->dato = path_b;
aux_List->next = NULL;
}
So, it ignores its input, returns no output, and leaks two partially-initialized nodes which aren't connected to each other. I believe you wanted to achieve something more like this:
struct nodo* create_my_list()
{
struct nodo *tail = alloc_nodo("Minasan", NULL);
/* the end (tail) of the linked list has a NULL next pointer */
struct nodo *head = alloc_nodo("Hello", tail);
/* the head of the linked list points to the next node */
return head;
/* like a snake, you hold a singly-linked list by the head */
}
If we write main to use this function now, it looks like:
int main()
{
struct nodo *head = create_my_list();
struct nodo *n;
for (n = head; n != NULL; n = n->next)
{
printf("The date is %s\n ", n->dato);
}
}

Pointing to structs in a linked list

We're trying to set the address of a struct to an address we are given but when we print out the address of the struct it seems to not be the same value as the address we are given.
/*a struct to keep block information*/
struct header{
int space;
int free; /* 1 = free space and 0 = full*/
struct header *nextHead;
struct header *prevHead;
};
typedef struct header node;
int myinit(int *array, int size){
int newSize = size;
node * nullPointer;
nullPointer = NULL; //make intermediatry node pointer for some bullshit reason
* (array) = newSize; /*store the size of the malloc at the first address*/
printf("Address : %p\n", &array[0]);
array++;
printf("Address after: %p\n", &array[0]);
/*initial block*/
node *root = (node *)&array; /*store the root at the next address available*/
printf("size of struct %lu\n", sizeof(struct header));
printf("%p\n", root);
root->space = newSize;
root->free = 1;
root->nextHead = nullPointer;
root->prevHead = nullPointer;
}
In the line
node *root = (node *)&array;
You're taking the address of "array" local variable. IOW, you take the address of value that's on the stack, not what you are expecting. You have to modify the function's signature like this:
int mymain(int **myarray, int size);
and modify its definition accordingly. Then, you can write:
node *root = (node *)array;
node *root = (node *)&array;
Here you obtain address of a pointer and cast it to other pointer. You should not do this. Here you must allocate the memory for the node:
node * root = (node *) malloc(sizeof(node));
// or this allocates the memory and puts zeros to it
node * root = (node *) calloc(1, sizeof(node));
Also, you don't need any nodes which points to NULL, you can simply use NULL like this:
node->nextHeader = NULL;
Also, instead of using &array[0], use array in this piece of code.
You will become less confused with pointers if you keep to simple code and understand every line you write. When you have a lot of ampersands and special signs in one line you're probably doing something wrong, train your spider sense for those situations.

Resources