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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:52:50+00:00 2026-05-30T09:52:50+00:00

I’m having a lot of trouble performing a very basic task: resizing an array.

  • 0

I’m having a lot of trouble performing a very basic task: resizing an array. Every intro to programming class I’ve ever taken taught me to do this by creating a larger array, filling it, and then point the original array to the new (larger) one.

The program below tokenizes a string into a program name and its argv[] (it’s ultimately going to be a basic shell implementation). It allocates space for 8 arguments at a time — if there are more than 8 then it recursively allocates a larger array and fills it.

Everything is working well (please let me know otherwise!) except I can’t point the args array to the moreArgs array. I have a statement that should do this at the end of the getArgs function but it simply is not reassigning the address of args[]. What am I doing wrong?

#define debug 1

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

char ** getArgs( char *input,  char **args, int ct);

/*Is there a better way than making these global?*/
char ** args;
char **moreArgs;

int main(int argc, char* argv[]) {
  char input[]="echo arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10";
  char inputcpy[strlen(input)];
  strcpy(inputcpy, input);
  char * temp;
  temp=strtok(input, " ");
  char * prog=temp;

  args=( char **) calloc(8, sizeof( char*));  

  getArgs(inputcpy, args, 1);

  if(debug) {
    printf("arg address after: %p\n", args);
    printf("morearg address after func: %p\n", moreArgs);
  }

  /*This is basically  what the shell will look like. The actual implementation will use stdin
   for input. (Unless a pipe or < is present in the input)*/
  int q;
  int pid=fork();
  if (pid!=0) {
    execvp(prog, args); //when moreArgs!=null, args should point to moreArgs
    return 0;
  }
  else {
    int status=0;
    wait(&status);
  }
}


/*This function should takes the first argument and inserts int into the second as " " separated tokens. If the second argument is too small -- the function recurses, and resizes the array as needed. The third argument is used to keep
  track of the recursion*/
char ** getArgs( char *input,  char **args, int ct) {
  int adj=(ct-1)*8;//if we recurse, this ensures correct indexes are used
  char *inputcpy=malloc(strlen(input));
  strcpy(inputcpy, input);

  /*Initialize indexes/Prepare for copying*/
  int i; 
  if(ct==1) {
    i=1; // this might throw off later adjusts
    args[0]=" "; //quick hack to ensure all args are used by exec()
  }
  else
    i=0;

  /**Actually do the copying now**/
  char *temp=strtok(NULL, " "); //What if later tokens are longer?
  args[adj+i++]=temp;

  while (temp != NULL && i<8) {
    temp=strtok(NULL, " ");
    args[adj+i++]=temp;
  }   

  /*If there are more args than we have room for*/
  if(i>=8){

    //is this allocation right?
    moreArgs = (char **) malloc((++ct)*8*sizeof( char *));

    /*Fill moreArgs with args*/
    int j;
    for (j=0; /*j<ct*8 && */args[j]!=NULL; j++) {
      moreArgs[j]=args[j];
    }   

    getArgs(inputcpy, moreArgs, (ct) ); //could probably move inc to malloc

    //free(args)?    
    if(ct>1)
      args=moreArgs;
  }
  /*Done with too many args problem*/


  return NULL;//(char **) args; //we don't want the global args though
}
  • 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-30T09:52:51+00:00Added an answer on May 30, 2026 at 9:52 am

    The reason it’s not behaving the way you want it to is because you’re passing args by value.

    char ** getArgs( char *input,  char ***args, int ct);
    

    This way, you can reassign args.

    Edit: Make sure you free args before reassigning. Edit 2: That was too specific of me. Make sure you free all the objects that you dynamically allocated. There’s quite a few that you have just left.

    As a side note, you’re calling execvp from the parent process, and calling wait from the child process. It should be the other way around. Also, you should avoid using fork with execvp and use system instead. You get the benefit that it’s an atomic operation.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.