Tags down


Getting segmentation fault while assigning values to a nested structure

By : Ibrahim Salah
Date : September 12 2020, 04:00 PM
To fix this issue It is because struct Address *add[2] is an array of pointers to Address type structure. Those pointers are invalid because you didn't allocated them. It should be something like this
code :
node->add[0] = malloc(sizeof(struct Address))
node->add[1] = malloc(sizeof(struct Address))
node->add[0]->streetName = (char*)malloc(stringSize)
node->add[0]->city= (char*)malloc(stringSize)
struct Address
    char* streetName;
    char* city;
struct Employee {
    char name[32];
    int empId;
    struct Address* add[2];
    struct Employee* next;

struct Employee* createEmployee(int id, const char *name, const char *street1, const char *city1, const char *street2, const char*city2)
    struct Employee* emp = (struct Employee*)malloc(sizeof(struct Employee));

    emp->empId = id; 
    strcpy(emp->name, name); 

    emp->add[0] = (struct Address*)malloc(sizeof(struct Address));
    emp->add[0]->streetName = (char*)malloc(256);
    emp->add[0]->city = (char*)malloc(256);

    strcpy(emp->add[0]->city, city1);
    strcpy(emp->add[0]->streetName, street1);

    emp->add[1] = (struct Address*)malloc(sizeof(struct Address));
    emp->add[1]->streetName = (char*)malloc(256);
    emp->add[1]->city = (char*)malloc(256);

    strcpy(emp->add[1]->city, city1);
    strcpy(emp->add[1]->streetName, street1);

    return emp;

void insertEmployee(struct Employee** head, struct Employee* emp)
    if (*head == NULL)
        emp->next = NULL;
        *head = emp;
        emp->next = (*head);
        *head = emp;

void printList(struct Employee* head)
    while (head != NULL)
        printf("%s\n", head->name);
        printf("%d\n", head->empId);
        printf("%s\n", head->add[0]->city);
        printf("%s\n", head->add[0]->streetName);
        printf("%s\n", head->add[1]->city);
        printf("%s\n", head->add[1]->streetName);
        head = head->next;

int main()
    struct Employee* head = NULL;
    struct Employee* emp = NULL;

    emp = createEmployee(1, "name1", "street1", "city1", "street2", "city2");
    insertEmployee(&head, emp);
    emp = createEmployee(2, "name2", "street3", "city3", "street4", "city4");
    insertEmployee(&head, emp);


    return 0;

Share : facebook icon twitter icon

segmentation fault while assigning structure members in c

By : Kim Joseph
Date : March 29 2020, 07:55 AM
this one helps. Just before you do that segmentation-violation-causing instruction, insert:
code :
printf( "%p\n", l);
printf( "%p\n", l->data);
printf( "%p\n", d);
printf( "%p\n", d->name);

Segmentation fault when assigning values to pointers in function

By : J.Doe
Date : March 29 2020, 07:55 AM
it fixes the issue z1 is pointing to a string literal and modifying a string literal is undefined behavior. Alternatively you can use the following declaration of z1 which will be modifiable:
code :
char z1[] = "      weeee"; 
while (*p == ' ' && *p != '\0') {
while (*p == ' ' ) {

C segmentation fault when assigning values to the char members of my struct

By : bilalaskerr
Date : March 29 2020, 07:55 AM
wish of those help I have the following code: , Try "%c" instead of "%s" in the problem cases:
code :
case 5:
  gga_ptr->east_west_id = *token;

Segmentation fault in assigning value to a dynamic array of Nodes, inside a structure

By : Timothy Fanelli
Date : March 29 2020, 07:55 AM
may help you . You allocate space for SIZE pointers to Node, but don't initialize these in any way, so when you access M->Darray[i] in M->Darray[i]->data you get segmentation fault because value of M->Darray[i] has not been set.
You need to allocate space for each node before using it:
code :
for (i = 0; i < SIZE; i++) {
    M->Darray[i] = malloc(sizeof(Node));
    M->Darray[i]->data = 0;
    M->Darray[i]->next = NULL;
struct Node* Darray;
M->Darray = malloc(sizeof(Node) * SIZE);
for (i = 0; i < SIZE; i++) {
    M->Darray[i].data = 0;
    M->Darray[i].next = NULL;

Segmentation Fault occuring while assigning values of array

By : Biophor
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I'm trying to set array's length according as a file's line number. I declared my arrays in main function and passed to setMatrix. , The function definition should match the arrays:
code :
void setMatrix(int x, int graph[x][x], int path[x], char *pathString[x])
Related Posts Related Posts :
  • Importance of '0' character and << operator in this program
  • How to debug a quadratic probing implementation for hash tables in C without using pointers?
  • Pointer to Pointer in c, getting unexpected value
  • "Permission denied" in open() function in C
  • How to assign an array of structs
  • Sort an Array of struct by a value of a field inside the struct
  • Implement stack with pointers in c
  • C program that goes through a string and flags for non-letters
  • C while statement in switch clause
  • Can we allocate memory for char * but return it as const char *?
  • How do Comma-separated expressions as condition in a for-loop work?
  • Sort Structs by the value of an attribute in it
  • How should I make users to put in the info that I want?
  • Are bitmask checks more efficient than comparing numbers?
  • Are the fast integer types faster when stored in the CPU registers?
  • Does realloc deallocate before trying to allocate new memory?
  • Failing to compile a 15+ year old MUD codebase written in C
  • opendir dont get content of buffer char * on C language?
  • Can we have '+' operator inside printf()?
  • Why scanf function do not accept escape sequences as an input?
  • Print strings of length of k from a and b
  • C problem with passing pointer to struct to function
  • Shall I use register class variables in modern C programs?
  • Can I specify conditions for the rand() function?
  • C fprintf and fscanf saving game state
  • Fast formula to get the range a number is in, given a perfect binary subdivision?
  • Why can't I use more than two registers in inline asm?
  • A question of strstr() and fgets() in a demo from the book《Head First C》
  • the problem is from a algorithm problem I do recently,but I can't gain the right answer
  • Problem to access members in pointer struct located in another pointer struct
  • Check that all symbols defined in static library
  • Function will only properly work if I've printed the values of the attributes
  • Why my code is showing time limit exceeded
  • #define statement for address of memory
  • How to call a function from another .c file
  • Memory layout of C struct
  • Why should we use return statement when using a recursive call?
  • error: expected identifier or '(' before '=' token
  • Why is it not possible to exchange the position of array name and index in swift like C (a[i] == i[a])?
  • How can I allocate enough memory to store an entire text file in C?
  • Segmentation fault when I use afl fuzzer
  • What happens in the assembly output when we add "cc" to clobber list
  • What is the equivalent of Keil _at_ in gcc
  • Simple question about declaring an empty string in C
  • Heap-Overflow or Buffer-Overflow?
  • C: Why is &= better that =?
  • Create exactly 5 process with fork()
  • gcc compile function in ifdef
  • where is the memory leak in the code below?
  • Most efficient formula for unpacking 16-bit BCD? (e.g. 0x1234 to 0x01020304)
  • how to find which integer no is maximim out of given numbers
  • Difference between a VOID function with a string argument VS a function with a string argument that returns a char array
  • why the output of the following program different (random value) every time?
  • Why does this macro expression in C behave unexpectedly?
  • When is memory allocated and de-allocated static and dynamic memory in C?
  • In C, what happens if we left shift the bits out of range and again right shift the values in the same operation
  • why the following c program is giving segmentation error?
  • How can I run through all the characters of a string and check if they're all numbers in C?
  • adc frequency vs accuracy
  • why the output of following c program is 16 16 16?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk