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

  • Home
  • SEARCH
  • 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 9239773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:03:52+00:00 2026-06-18T08:03:52+00:00

I made a code to swap two strings: void swap (char *a, char *b)

  • 0

I made a code to swap two strings:

void swap (char *a, char *b)
{
    char *t = a;
    a = b;
    b = t;
}

int main()
{
    char * strings[2];
    strings [0] = "luck!";
    strings [1] = "good ";
    swap (strings[0], strings[1]);
    printf( "%s %s\n",strings[0], strings[1]);
    return 0;
}

And it fails. What i have trouble understanding is when i call swap() i pass two pointers. Both pointers point to the first character of their assigned arrays. I then created a temporary pointer inside the function and perform basic switch. What is flawed here? I really want to understand why this approach is wrong?

  • 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-18T08:03:53+00:00Added an answer on June 18, 2026 at 8:03 am

    You are switching the parameters of the function, which are local to the function scope. When your function is executed, the parameters (a, of type char*, and b, of type char*) are passed by value, put on the stack, and the function is executed. The parameters are modified and then popped of the stack without effect.

    To make a difference, you need to pass references to the parameters:

    void swap (char **a, char **b)
    {
        char *t = *a;
        *a = *b;
        *b = t;
    }
    

    and then call with:

    swap (&strings[0], &strings[1]);
    

    You now pass pointers to individual array elements in strings, which is in main’s stack segment and thereby persists past the context of swap.

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

Sidebar

Related Questions

I made this code below: #include <stdio.h> #include <stdlib.h> main() { int n, i,
I just made a Swap routine in C# like this: static void Swap(ref int
I have written code to swap strings, but I am not able to swap.
I have seen this code: void reverse_string(char *str) { /* skip null */ if
I made some code, for understanding the concept/basic of pointer: int a=1; int *b=&a;
I have made a code that read data from flash Nand (without filesystem). fd
I have made a code where I have to input 1 . no of
I have made a code for scrolling a background in Cocos2d to create an
I have made this code: var foo=document.createElement(div); var childs=foo.getElementsByTagName(*); console.log(childs.length);//0 OK var a=document.createElement(a); foo.appendChild(a);
now i made JavaScript code (jquery code) and when i click ok it return

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.