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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:26:00+00:00 2026-05-26T04:26:00+00:00

For a program, I would like to make an array copy of the arguments

  • 0

For a program, I would like to make an array copy of the arguments sent in by command line using malloc().

So for example if I do ./a.out one two three
I want an array with {a.out, one, two, three} in it.

However, I have some issues getting my program to work. Here’s what I have:

static char** duplicateArgv(int argc, char **argv)                                                                                                                                                         
{                                                                                                                                                                                                                                                                                                                                                                                       
    char *array;                                                                                                                                                                                                                                                                                                                                                               
    int j = 0;                                                                                                                                                                                        

    // First allocate overall array with each element of char*                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
    array = malloc(sizeof(char*) * argc);                                                                                                                                                       
    int i;                  

    // For each element allocate the amount of space for the number of chars in each      argument                                                                                                                                                              
    for(i = 1; i < (argc + 1); i++){                                                                                                                                                                      
        array[i] = malloc(strlen(*(argv + i)) * sizeof(char));                                                                                                                                        
        int j;       

        // Cycle through all the chars and copy them in one by one                                                                                                                                                                                 
        for(j = 0; j < strlen(*(argv + i)); j++){                                                                                                                                                     
            array[i][j] = *(argv + i)[j];                                                                                                                                                                                                                                                                                                                                                       
        }                                                                                                                                                                                             

    }                                                                                                                                                                                                 

    return array;

}

As you might imagine, this doesn’t work. I apologize ahead of time if this somehow totally doesn’t make sense, as I just started learning about pointers. Also, I’m not quite sure how to write code to free up every element in the *array after I do what I need to the copy.

Could anyone give me some tips on what I should look into to make it do what I want?

Thanks for any help!

  • 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-26T04:26:01+00:00Added an answer on May 26, 2026 at 4:26 am

    You’re not allocating or copying the terminating NULL characters:

    This line needs to be changed to this for the NULL.

    array[i] = malloc((strlen(*(argv + i)) + 1) * sizeof(char));   
    

    And the loop should be changed to this:

    for(j = 0; j <= strlen(*(argv + i)); j++){ 
    

    Also, the code can be better optimized if you saved the result of the strlen() call since you call it in so many places.

    Try the loop as this:

    // For each element allocate the amount of space for the number of chars in each argument
    for(i = 0; i < argc; i++){
    
        int length = strlen(argv[i]);
    
        array[i] = malloc((length + 1) * sizeof(char));
        int j;
    
        // Cycle through all the chars and copy them in one by one
        for(j = 0; j <= length; j++){
            array[i][j] = argv[i][j];
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make a program to capture video. What is the best
I have my main program window, and I would like to make a foldable
I have a java program in a certain directory. I would like to make
I would like to use python to make system calls to programs and time
I'm trying to link 10 computers together, the program I would like to write
I have a program which I would like to run every X min the
I have a single user java program that I would like to have store
I currently have a small Java program which I would like to run both
I would like my program to update itself (downloading a new exe and/or some
I would like my program to email me a bug-report when it fails. Is

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.