Thread: Change struct pointers

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    Change struct pointers

    I have a linked list where each node contains a pointer to a string, I'm treating the linked list as a text file and each node/string contains each line in the text file.

    I have all of that working, but now I need to create a switchLine function that takes in the two line numbers and I need to switch those lines in the text file. I initially thought of switching the pointers to each the next node in the linked list such that the node order changes, but I then realized how much work that would actually take when you need to consider so many cases. So I instead thought of changing the pointers to strings. If I want to switch the 2nd and 3rd line, then I'd want to have the 2nd node in the list have its char * pointer point to the memory position that the 3rd node's char pointer points to, and vice versa.

    However, while it sounds very simple in theory, I just can't figure out how to do it...

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Can you swap the two lines? Something like

    temp = line1
    line1 = line2
    line2 = temp

    After that, the two pointers to the lines will have been swapped.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    I tried that at first and it didn't seem like it worked, but it was actually a problem I had elsewhere that made me think it failed. Thanks for helping me notice it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Broadcast struct in C. Change ip SOURCE addres.
    By franmf in forum C Programming
    Replies: 5
    Last Post: 12-11-2012, 05:07 PM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Endian change & Byte Array -> Struct
    By bramaen in forum C Programming
    Replies: 3
    Last Post: 02-05-2010, 12:55 PM
  4. Change char array in struct
    By Rob4226 in forum C Programming
    Replies: 1
    Last Post: 10-20-2009, 03:52 PM
  5. change struct
    By WolfBend in forum C Programming
    Replies: 2
    Last Post: 05-12-2007, 11:03 PM