Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9247775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:49:04+00:00 2026-06-18T09:49:04+00:00

I was going to write a simple SDL app so I thought I should

  • 0

I was going to write a simple SDL app so I thought I should implement a simple linked list that I can extend later. Now after some struggling with it I decided to ask for help here.

I know the linked list, I read about it. I saw couple implementations, not so hard, easy to grasp. So I thought I should be able to implement one myself. I am guessing that I am severely overlooking something here. 🙁

First this code does not compile well, segmentation fault. Second I do not think that it is going to produce what I want if I can overcome the basic errors I am doing with the pointers unfortunately.

Can you please show me my mistakes in my code? Where am I doing wrong?

Here is a liveworkspace link for editing
http://liveworkspace.org/code/1qu1RN$1

Here is the error I get on Cygwin gcc

temp [v 0] -> 0.460241 0.000000 0.050356
temp [v 1] -> 0.072079 0.153807 0.864573
temp [v 2] -> 0.683917 0.709301 0.080479
temp [v 3] -> 0.295755 0.264795 0.894696
address of ptemp @ 1629101750

[v 0] -> linked vertex is  7731159828445146316135105405324534341657178000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000 578395405356271.125000 1310047834864691498231654014730274851713450000.000000
Segmentation fault (core dumped)

And here is the code

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NEWline printf("\n")

typedef struct position {
    double x;
    double y;
    double z;
} position;

struct datalist {
    position v;
    struct datalist  *pnext;

};
typedef struct datalist datalist;

void initializeList (datalist *head);
void addList_II (datalist *head , double data[3] );

void initializeList (datalist *head){
    position pv= {0,0,0};
    head = (datalist *) malloc ( sizeof (head) );
        if (head == NULL){ printf("cannot allocate memory");  exit(1); }
    head->v=pv;
    head->pnext=NULL;
}

void addList_II ( datalist *head , double data[3] ) {

    datalist *adddata;
    adddata = (datalist *)malloc ( sizeof (datalist) );
        if (adddata == NULL) { printf("cannot allocate memory for insertion data"); exit(1); }
    adddata->v.x = data[0];
    adddata->v.y = data[1];
    adddata->v.z = data[2];

    adddata->pnext = head->pnext;
    head=adddata;
}

int main() {

    datalist *ptemp;
    datalist temp;
    int i,c; //counters
    datalist *head;

    initializeList(head);

    for ( i = 0; i < 4; i++ ) {
        double t[3];
        srand ( time ( NULL )*i+234 );
            temp.v.x = ((double ) rand()  )/ RAND_MAX;
        srand ( time ( NULL )*i*546 );
            temp.v.y = ((double ) rand()  )/ RAND_MAX;
        srand ( time ( NULL )*i*567+345 );
            temp.v.z = ((double ) rand()  )/ RAND_MAX;
        t[0] = temp.v.x; t[1] = temp.v.y; t[2] = temp.v.z;

        printf ( "temp [v %d] -> %F %F %F \n",i, temp.v.x, temp.v.y, temp.v.z );
        addList_II (head, t );
    }

    c=0;
    ptemp=head;
    while (ptemp!=NULL) {
        printf("[v %d] -> linked vertex is  %F %F %F \n", c, ptemp->v.x,ptemp->v.y, ptemp->v.z);
        ptemp=ptemp->pnext;

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:49:05+00:00Added an answer on June 18, 2026 at 9:49 am

    In your addList_II you are setting the wrong pointer. You are taking your new node and pointing its next pointer to the head’s next pointer, which is null.

       void addList_II ( datalist *head , double data[3] ) {
            datalist *adddata;
            adddata = (datalist *)malloc ( sizeof (datalist) );
                if (adddata == NULL) { printf("cannot allocate memory for insertion data"); exit(1); }
            adddata->v.x = data[0];
            adddata->v.y = data[1];
            adddata->v.z = data[2];
    
            //    adddata->pnext = head->pnext; //not THIS
            adddata->pnext = head;        //DO THIS
            head=adddata;
        }
    

    Additionally your pointers were going out of scope in your function calls. what you need to do is pass in ** variables. This means your passing in a pointer to a pointer, which points to your block of memory. You modify the original pointer by derefrencing your local *, and upon return you are good.

    If you need more explanation, just ask, but heres the solution

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define NEWline printf("\n")
    
    typedef struct position {
        double x;
        double y;
        double z;
    } position;
    
    struct datalist {
        position v;
        struct datalist  *pnext;
    
    };
    typedef struct datalist datalist;
    
    void initializeList (datalist **head);
    void addList_II (datalist **head , double data[3] );
    
    void initializeList (datalist **head){
        position pv= {0,0,0};
        *head = (datalist *) malloc ( sizeof (datalist) );
            if (head == NULL){ printf("cannot allocate memory");  exit(1); }
        (*head)->v=pv;
        (*head)->pnext=NULL;
    }
    
    void addList_II ( datalist **head , double data[3] ) {
        datalist *adddata;
        adddata = (datalist *)malloc ( sizeof (datalist) );
            if (adddata == NULL) { printf("cannot allocate memory for insertion data"); exit(1); }
        adddata->v.x = data[0];
        adddata->v.y = data[1];
        adddata->v.z = data[2];
    
        adddata->pnext = *head;        //DO THIS
        *head=adddata;
    }
    
    int main() {
    
        datalist *ptemp;
        datalist temp;
        int i,c; //counters
        datalist *head;
    
        initializeList(&head);
    
    
        for ( i = 0; i < 3; i++ ) {
            double t[3];
            srand ( time ( NULL )*i+234 );
                temp.v.x = ((double ) rand()  )/ RAND_MAX;
            srand ( time ( NULL )*i*546 );
                temp.v.y = ((double ) rand()  )/ RAND_MAX;
            srand ( time ( NULL )*i*567+345 );
                temp.v.z = ((double ) rand()  )/ RAND_MAX;
            t[0] = temp.v.x; t[1] = temp.v.y; t[2] = temp.v.z;
    
            printf ( "temp [v %d] -> %F %F %F \n",i, temp.v.x, temp.v.y, temp.v.z );
            addList_II (&head, t );
        }
    
        ptemp=head;
    
        while (ptemp!=NULL) {
           NEWline; 
            printf("[v ] -> linked vertex is  %F %F %F \n", ptemp->v.x,ptemp->v.y, ptemp->v.z);
            ptemp=ptemp->pnext;
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a simple console app that POSTs to a page
I'm going to write a simple helper that wraps Html.ActionLink and adds a certain
So I am trying to write a simple app that will connect to another
I am going to write an app for google services, but I stuck at
I'm going to write a CMS, but right now I'm writing down all my
I'm going to write the web portal using Cassandra databases. Can you advise me
I am going to write a hybrid Web/Desktop app. The main purpose of the
I'm going to write a function for thumbnail creation. I can use both Imagemagick
Im new on this project and am going to write, what i thought was
I'm trying to write a simple Javascript(jQuery) function that randomly displays 6 Divs out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.