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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:30:49+00:00 2026-05-27T03:30:49+00:00

I want to pass a char ** pointer by value. Given the following code:

  • 0

I want to pass a char ** pointer by value. Given the following code:

char **ptr; 

void init(){
    int i;
    ptr = (char**)malloc(2 * sizeof(char*));
    for(i=0;i<2;i++){
         ptr[i]=(char*)malloc(3 * sizeof(char));
         ptr[i] = ..//assign different strings of length 2
    }
}

void swap(char **x, char **y) {
    char *temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

void f(char **ptr1){
    //swap the first and second element
    swap(&ptr1[0],&ptr1[1]);
}

int main{
    init();
    f(ptr);
}

I call the function f with ptr. I want to keep ptr’s first values and want to use ptr1 as a copy of it. However, after the swap operation both ptr and ptr1 becomes same. ptr changes. How can I pass it by value?

  • 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-27T03:30:49+00:00Added an answer on May 27, 2026 at 3:30 am

    You want to deep copy (as opposed to shallow copy) your char**. When you call f(ptr), it’s like if you were writing

    ptr1 = ptr; // by the way, why ptr is global?
    

    and this makes it plain that ptr1 it is pointing to the same area of memory pointed to by ptr.
    So it’s not strange that the swap is affecting ptr, too.

    How to fix it? You have to copy each string one by one.
    You can read this answer on SO for a similar issue (it’s C++ but the point is the same).

    SUGGESTION
    If possible, I recommend to

    1. get rid of the global pointer

    2. change init to return the global pointer instead of void, so that it allocates memory and returns a pointer to it

    3. possibly: create a different function which takes in input the values with which you initialize the various strings (Two in your example) of the char**

    4. create a function which frees all the memory allocated (I guess that probably you already have it)

      So that your f() function will init() a new ptr1, will take the values from ptr to initialize the values of the ptr1, and will call the swap on the members (better yet, you can directly initialize without even swapping, just call with the appropriate arguments the function at step 3.

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

Sidebar

Related Questions

I want to pass an integer value to a form in .Net so that
I want to pass an int list (List) as a declarative property to a
I want to pass an enum value as command parameter in WPF, using something
I want to pass a pointer to a function. I want this pointer to
Example C API signature: void Func(unsigned char* bytes); In C, when I want to
I want to pass a big char* from cpp to .Net (preferably using COM).
gcc 4.6.2 c89 I have the following 2D array that I want to pass
I want to pass a pointer to pointer to a function, allocate memory in
Possible Duplicate: C# Textbox string separation I want to pass the string value ie
we declare main() as int main(int argc, char *argv[]) and pass some argument by

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.