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 6830009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:34:03+00:00 2026-05-26T22:34:03+00:00

I am new to linked list in C and the problem I have is

  • 0

I am new to linked list in C and the problem I have is that I am trying to make a linked list of Strings, but when I try to print that list it prints first char from two different strings. I think I am messing some pointers. Any help please?
Here is my code…

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


int main()
{
    typedef struct _song{char *songTitle; char *songAuthor; char *songNote; struct _song *next;}SONG;

    int songCount =4;


    char SongTitle[songCount];
    char AuthorName[songCount];
    char SongNotes[songCount];


    char songTitle0[21] = "19 problems";
    char songArtist0[21]="JayZ";
    char songNotes0[81]="JiggaWhoJiggaWhat";
    SongTitle[0]=*songTitle0;//points at string songTitle0
    AuthorName[0]=*songArtist0;
    SongNotes[0]=*songNotes0;

    char songTitle1[21] = "Cig Poppa";
    char songArtist1[21]="Biggie Smalls";
    char songNotes1[81]="I Luv it When you call me big poppa";
    SongTitle[1]=*songTitle1;
    AuthorName[1]=*songArtist1;
    SongNotes[1]=*songNotes1;

    SONG *CurrentSong, *header, *tail;

    int tempCount=0;
    header = NULL;

    for(tempCount=0;tempCount<songCount;tempCount++)
    {

        CurrentSong = malloc(sizeof(struct _song));
        CurrentSong->songTitle= &SongTitle[tempCount];
        CurrentSong->songAuthor=&AuthorName[tempCount];
        CurrentSong->songNote=&SongNotes[tempCount];

        if(header == NULL)
        {
            header=CurrentSong;//head points to first thing in memory
        }
        else
        {
            tail->next=CurrentSong;
        }
        tail = CurrentSong;//always the last thing in the list 
        tail->next=NULL;//the next pointer is null always

    }
    tempCount =0;
    for(CurrentSong=header; CurrentSong!=NULL; CurrentSong=CurrentSong->next)
                    {
                        printf("\n%d: ", tempCount);
                            printf("Title: %s ",CurrentSong->songTitle);


                        printf("Author: %s ",CurrentSong->songAuthor);
                        tempCount++;
                    }


    return 0;
}
  • 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-05-26T22:34:04+00:00Added an answer on May 26, 2026 at 10:34 pm

    That’s not how you’re supposed to use linked lists.

    It’s

    typedef struct list {
        void *data;
        struct list *next;
    }
    SONG *s = (SONG *)songList->data;
    

    Likewise, for cloning strings, you need to use strdup.

    E.g.

    s->songTitle = strdup(SongTitle);
    s->songAuthor = strdup(AuthorName);
    s->songNote = strdup(SongNotes);
    

    Don’t forget to free the strings once you’re done with them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a new website that is linked to from many places but the
I have a problem either adding to or traversing a linked list. The main
I have an object that contains, as part of its data, a linked list.
I have a class that looks like this: typedef std::list<char*> PtrList; class Foo {
I'm very new to pointers and linked lists but I'm trying to write a
I have a project where I get a list of file location strings that
OK here is my problem. I have a linked list of card objects. I
I need to copy two linked lists recursively and return a new list .
new to c#. I'm trying to make a simple system where I can search
I face a problem in using AbstractTableModel, i use linked list as a container

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.