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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:18:25+00:00 2026-06-14T00:18:25+00:00

I’m having some trouble copying pointers’ contents. I’m simply trying this: char* vigia1; char*

  • 0

I’m having some trouble copying pointers’ contents. I’m simply trying this:

 char* vigia1; 
 char* vigia2;

And..

  char* aux = (char*) malloc (strlen (vigia1)+1);
  aux=vigia1;
  vigia1=vigia2;
  vigia2=aux;
  free (aux);

vigia1, vigia2 are pointers to a char pointer. They both have a malloc greater than their maximum possible size, that’s OK.

Since I’m trying to make an order for a list, I need to make this change to order the nodes’ content. But I’m getting confused: after the free(aux) , vigia2 doesn’t have any value. I think I must be pointing vigia2 to the memory region where aux is, region that ‘disappear’ after the free. So what should I do?
Thanks!

  • 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-14T00:18:26+00:00Added an answer on June 14, 2026 at 12:18 am

    Pointers, pointers, bad with them, worse without them

    A pointer is a number that stores where in memory sth is stored, with that in mind, let’s delve into what you’ve done there:

    char* aux = (char*) malloc (strlen (vigia1)+1);
    

    Good, you’ve created space somewhere in a part of the memory called heap, and stored the address of the newly created memory space at aux.

    aux=vigia1;
    

    Ops, now you’ve overwritten the address of the memory space you’ve “created” with the number stored at vigia1, that happens to be an address to another memory space.

    vigia1=vigia2;
    

    Now you’re assinging to vigia1 the value of vigia2, another address of some memory space out there.

    vigia2=aux;
    

    And, by the end of it, you make vigia2 point to the memory region previously pointed by vigia1.

    free (aux);
    

    Now, you’re freeing the memory pointed by aux. Wait a second, on the line above this one you’ve just made vigia2 point to this same address. No wonder it holds nothing useful 🙂

    Trying to help you with what you want to do:

    So long you don’t have any constraint that obliges you to mantain your list nodes ordered in memory, you don’t need to copy the content of the node, just make the pointer of the first node point to the memory region of the second node.

    A perfect swap would be:

    char *aux; // you'll need an aux to make the swap, the normal stuff
    aux = vigia1; // now aux points to the same address as vigia1
    vigia1 = vigia2; // vigia1 now points to the contents of vigia2
    vigia2 = aux; // and now vigia2 points to the content pointed previously by vigia1
    /* and tada! the swap is done :D */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.