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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:04:57+00:00 2026-06-11T12:04:57+00:00

I have a struct that has an array of strings (char **args). I need

  • 0

I have a struct that has an array of strings (char **args). I need to be able to copy an array of strings (char *input[32]) into that element of the struct. For example:

Thing s;
s.args = input; //assuming input already has some strings in it

When I try to do that, the next time s.args = input is called, it completely overwrites the old input. How can I achieve this functionality in the appropriate manner?

EDIT

This is what the struct looks like.

typedef struct{
char **args;
} Thing;

Then in my function, I have declared:

char *args[512];
.....
args[count] = string //string is a char *

Then finally, I want to do:

s.args = input.
  • 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-11T12:04:58+00:00Added an answer on June 11, 2026 at 12:04 pm

    You are not copying it. You are actually just setting the pointer. Effectively you have this:

    char **args;
    char *other[32];
    args = other;
    

    You need to actually copy the array – for that you need to allocate memory for it:

    s.args = malloc( 32 * sizeof(char*) );
    
    for( i = 0; i < 32; i++ ) s.args[i] = input[i];
    

    That is a shallow copy – it will copy your string pointers but not duplicate them. If you change the contents of a string in input, that change will be reflected in s.args. To copy the strings, you must do this:

    for( i = 0; i < 32; i++ ) s.args[i] = strdup(input[i]);
    

    Since you have allocated memory, then before you overwrite s.args again (and also when your program is finished) you need to free what you allocated. This includes the strings (if you called strdup);

    if( s.args != NULL ) {
        // Only do the loop if you did a deep copy.
        for( i = 0; i < 32; i++ ) free(s.args[i]);
    
        // Free the array itself
        free(s.args);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct that has a field called type How do i access
I have a struct, MyStruct , that has a private member private bool[] boolArray;
I have a class that has an inner struct/class (I have decided which yet,
I have an MVC application. The model has a property that is a struct
I have a structure that has an array of pointers. I would like to
Newbie here, I have a struct for a word, which contains a char array
I have a struct that represents a vertex. It has x, y and z
I have a struct that represents a vertex. It has x, y and z
I have a struct that has space for unsigned ints: typedef struct { unsigned
Say we have a struct that it's data is provided by un-managed byte array

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.