Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

problem with adding to a linked list

  • 19-03-2003 12:19pm
    #1
    Registered Users, Registered Users 2 Posts: 2,591 ✭✭✭


    there prob is that everything is grand when i enter the first node but when i go to add a second the contents of the node that i had just added become the same as the one that i just created
    this all happens when i use the line

    strcpy(newnode->SiteName,site);

    shown below is the code so far and the structure of the node any help would be graetly appreciated
    tanx

    struct vertexnode{

    char* SiteName;
    int XCoord;
    int YCoord;
    struct vertexnode *nextvertex;
    struct arcnode *nextarc;
    };


    vertexnode* InsertNewVertex(vertexnode* graph)
    {
    int xcord,ycord;
    char site[10];

    clrscr();
    vertexnode* newnode;
    newnode = GetvertexNode(); // allocates memory
    newnode->nextvertex = NULL;
    newnode->nextarc = NULL;
    printf("please enter the name of the site you wish to enter\n");
    fflush(stdin);
    gets(site);
    strcpy(newnode->SiteName,site); // problem line
    printf("please enter the x coord of the site you wish to enter\n");
    scanf("%d",&xcord);
    newnode->XCoord = xcord;
    printf("please enter the y coord of the site you wish to enter\n");
    scanf("%d",&ycord);
    newnode->YCoord = ycord;
    newnode->nextvertex = graph;
    graph = newnode;
    graph->nextvertex->SiteName;
    graph->nextvertex->nextvertex->SiteName;
    return(graph);
    :confused:


Comments

  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    I edited your code and mine ran fine

    see my code (in zip file)

    compiled with borland 4.5


  • Registered Users, Registered Users 2 Posts: 2,591 ✭✭✭tommycahir


    tanx hussey
    that helped alot
    could you tell me what the prob was though?


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    not really sure

    could have been something simple like the way you called it or mem allocation etc..


    you code looks ok to me


  • Registered Users, Registered Users 2 Posts: 2,591 ✭✭✭tommycahir


    got the code from a lecturer and it had worked in my other projects as well so i don't think ythat it was that but here it is, this prob really bugging me cos i dont like when bugs happen and can't be explained


    /* This function allocates memory for the node structure
    and returns a pointer to this block of memory. This memory
    is available until it is released using free() */

    vertexnode *GetvertexNode(void)
    {
    vertexnode *pt ;
    pt = (vertexnode *) malloc(sizeof(vertexnode) ) ;
    if (pt == (vertexnode *) NULL )
    {
    printf("Failed to allocate memory\n") ;
    exit(1) ;
    }
    return pt ;
    }

    called usin
    head = InsertNewVertex(head);

    with the following prototype
    vertexnode* InsertNewVertex(vertexnode*);

    and head is

    vertexnode* head;

    please some one help cos bugs really bug me...

    i nearly sure it something to do with the strcpy ..


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭Zab


    Where do you allocate memory for the string? SiteName is just a pointer, so when you use malloc all you are making is enough room for the pointer. You then have to point the pointer somewhere with some space for the string, or you could change it to an array of chars.

    Also, your last few lines:
    ---
    graph = newnode;
    graph->nextvertex->SiteName;
    graph->nextvertex->nextvertex->SiteName;
    return(graph);
    ---
    Don't do anything useful... why don't you just return newnode?

    Zab.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,591 ✭✭✭tommycahir


    graph->nextvertex->SiteName;
    graph->nextvertex->nextvertex->SiteName;

    were just put in so that i could put a watch on the sitename part of the node

    i see now that i was trying to place an array of chars where there was only room for a pointer;

    tanx for the tip!!


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    i see now that i was trying to place an array of chars where there was only room for a pointer;

    an array of chars is a pointer!!!!!!!!!!!!!!!!!

    so thats not your problem

    have a look at my other code .. its the uses the same code except I put a main in it


  • Registered Users, Registered Users 2 Posts: 4,196 ✭✭✭deadl0ck


    IN the original post, where you do the strcpy, where do you actually malloc the memory for newnode->SiteName ?

    You're trying to copy into unallocated memory...or didi you just leave this bit out in the code you pasted in ?


  • Registered Users, Registered Users 2 Posts: 2,591 ✭✭✭tommycahir


    deadl0ck - the malloc part of the code is shown in the third post and also all the other needeed code to figure this out..

    HUSSEY - using the code that you gave me now will let you no if any probs but i still f**ked if i no what gone wrong.


  • Registered Users, Registered Users 2 Posts: 4,196 ✭✭✭deadl0ck


    Are you talking about this:

    vertexnode *GetvertexNode(void)
    {
    vertexnode *pt ;
    pt = (vertexnode *) malloc(sizeof(vertexnode) ) ;
    if (pt == (vertexnode *) NULL )
    {
    printf("Failed to allocate memory\n") ;
    exit(1) ;
    }
    return pt ;
    }

    This only allocates memory for the Vertex node structure structure, you need to allocate

    Each pointer in this need to be alloced as well:

    char* SiteName;
    struct vertexnode *nextvertex;
    struct arcnode *nextarc;

    So - in this you also need:

    SiteName = (char *)malloc(SIZE * sizeof(char));

    where SIZE is however big you want it (Perhaps 11 in you case as you're copying in a string of Size 10 + 1 for the '\0')

    etc...


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,591 ✭✭✭tommycahir


    got it working perfectly by changing the char * in the struct to an array of 11 and there no probs now tanx guys for all the help was greatly appreciated


  • Registered Users, Registered Users 2 Posts: 4,196 ✭✭✭deadl0ck


    So it was the memory issue then ?


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by hussey
    an array of chars is a pointer!!!!!!!!!!!!!!!!!

    Not quite. Most operations on an array of chars is really an operation on a pointer, and when you pass an array you really pass a pointer.
    However when you are allocating you are allocating with the actual bunch of chars, rather than your way of addressing them, so in that context there is quite a difference.


  • Registered Users, Registered Users 2 Posts: 2,591 ✭✭✭tommycahir


    yea totally messed up with my pointer allocation.. damn pointers are wrecking my head at the mo for the three projects that i doing
    aghhh.


Advertisement