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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:51:53+00:00 2026-05-31T07:51:53+00:00

Assume I have char **argv , so that argv[0] = some string and argv[1]

  • 0

Assume I have char **argv, so that argv[0] = some string and argv[1] = another string etc…

I have another double pointer string array in a struct, call it s1->argv also defined as char **argv within the struct.

I am trying to copy argv into s1->argv1. I tried mallocing s1->argv to something with a max value (so 7 strings, each string of max 100 chars) and using strcpy but that obviously does not work. It only ends up copying argv[0]. This is how I used it: strcpy(*s1->argv, *argv)

How can I preserve each index of the array too?

  • 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-31T07:51:54+00:00Added an answer on May 31, 2026 at 7:51 am

    The code below makes a copy of argc and argv arguments of main into user defined structure. The copy is exactly the same as original arguments (argv is terminated by NULL as in main). This code does not handle any abnormal events (like malloc failures, signal interruption etc.).

    You may want to avoid casting the result of malloc (it is not required in C), there is a lot of debate whether this casting is a good or bad thing. The choice is yours. I value portability more, so I choose to cast, compile with all warnings enabled and eliminate all of them in the code (see the comments to this answer).

    #include <string.h>
    #include <stdlib.h>
    
    struct my_struct
    {
        int    argc;
        char** argv;
    };
    
    int main(int argc,char** argv)
    {
        int    i = 0;
        size_t n = 0;
        struct my_struct s;
    
        memset(&s,0,sizeof(s));
        s.argc = argc;
        /* alloc one more element than 'argc' to terminate 's.argv' with NULL */
        s.argv = (char**)malloc((argc + 1) * sizeof(char*));
        /* terminate 's.argv' with NULL exactly as 'argv' in 'main' */
        s.argv[argc] = NULL; 
        for(i = 0; i < argc; ++i)
        {
            n = strlen(argv[i]) + 1;
            s.argv[i] = (char*)malloc(n);
            strcpy(s.argv[i],argv[i]);
        }
        return 0;
    }
    

    Another option for copying the strings in argv could be using strdup function, then you could replace the three lines:

    n = strlen(argv[i]) + 1;
    s.argv[i] = (char*)malloc(n);
    strcpy(s.argv[i],argv[i]);
    

    with

    s.argv[i] = strdup(argv[i]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: std::vector<std::string> to char* array I have to call a c function that
I have a char array filled with some characters. Let's say I have HelloWorld
Assume I have the struct Foo struct Foo{ int a; short b; char c;
Suppose that I have a unsigned char*, let's call it: some_data unsigned char* some_data;
I have a char array that is really used as a byte array and
Assume I have char **argv . How do I determine its size? I have
Assume I have a class class A { char *attr1,*attr2; public: . . .
Assume you have some objects which have several fields they can be compared by:
Assume I have an ASP.NET MVC app that's not doing anything too fancy (no
My application is written in C. I have a module that uses some data

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.