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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:35:19+00:00 2026-06-09T17:35:19+00:00

I am having problems passing an array of strings to a function by reference

  • 0

I am having problems passing an array of strings to a function by reference in C.

Context: I am trying to make a very simple shell with a few commands. When the commands are typed in each argument of the command is saved in a slot of an array of strings. The function used to parse the command is called lineParsing (I do not have the source for this – I only have the .o and .h)

The function in question has the following header:

void lineParsing (char **, int, char **, char **, char **, int *);

void lineParsing (char **item, int nargs, char **inputRedir, char **outputRedir, char **errorRedir, int *background);

The description of the function is as follows:
Specifies if the array of arguments (first argument), contains a redirection or background execution.

Inputs:
1-the array of arguments
2-number of arguments
by reference:
3-An array of strings in which to place the names of the files for input redirection
4-An array of strings in which to place the names of the files for output redirection
5-An array of strings in which to place the names of the files for error redirection
6-Saves 1 in background if & is used, if not 0

I am calling this function in a temporary main program as follows:

int main (int argc, char *argv[]){
    char **parrayArgumentos=NULL;
    int i,numargs,background;

    char *inputRedir[4]={"","","",""};
    char *outputRedir[4]={"","","",""};
    char *errorRedir[4]={"","","",""};

    parrayArgumentos = lineInput (&numargs); //Asks for command via standard input.
    printf ("You have typed in: %d arguments \n",numargs); //displays number of arguments

    i=0;
    while(i<=numargs-1){
        printf ("%s \n",parrayArgumentos[i]);
        i++;
    }

lineParsing(parrayArgumentos,numargs,inputRedir,outputRedir,errorRedir,&background);
//This call is problematic

    printf ("The command you have introduced has:\n%c for input redirection\n%c for output Redirection\n%s For error Ridirection\n%d background\n",inputRedir[0],outputRedir[0],errorRedir[0],background); 

    freeLineInput(parrayArgumentos);

    return 0;

}

This compiles with no errors or warnings via a makefile I have made:

mishell: mishell.o parser64.o
    gcc mishell.o parser64.o -o mishell

mishell.o: mishell.c

clean:
    rm -f mishell.o mishell

When I execute the binary I get the following error at the point in which the call to lineParsing occurs:

[xxxx@xxxx src]$ ./mishell 
ls -la > listoffiles
You have typed in: 4 arguments 
ls 
-la 
> 
listoffiles 
*** glibc detected *** ./mishell: free(): invalid pointer: 0x0000000000401078 ***
======= Backtrace: =========
/lib/libc.so.6(+0x784a6)[0x7f8a5f76b4a6]
./mishell[0x400d99]
./mishell[0x4007b6]
/lib/libc.so.6(__libc_start_main+0xf5)[0x7f8a5f714725]
./mishell[0x4005e9]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:04 7865560                            /home/xxxxx/Desktop/xxxx/xxxx/xxxxx/src/mishell
00601000-00602000 rw-p 00001000 08:04 7865560                            /home/xxxxx/Desktop/xxxxx/xxxxx/xxxxx/src/mishell
00e25000-00e46000 rw-p 00000000 00:00 0                                  [heap]
7f8a5f4de000-7f8a5f4f3000 r-xp 00000000 08:03 1185891                    /usr/lib/libgcc_s.so.1
7f8a5f4f3000-7f8a5f6f2000 ---p 00015000 08:03 1185891                    /usr/lib/libgcc_s.so.1
7f8a5f6f2000-7f8a5f6f3000 rw-p 00014000 08:03 1185891                    /usr/lib/libgcc_s.so.1
7f8a5f6f3000-7f8a5f88e000 r-xp 00000000 08:03 2490393                    /lib/libc-2.16.so
7f8a5f88e000-7f8a5fa8d000 ---p 0019b000 08:03 2490393                    /lib/libc-2.16.so
7f8a5fa8d000-7f8a5fa91000 r--p 0019a000 08:03 2490393                    /lib/libc-2.16.so
7f8a5fa91000-7f8a5fa93000 rw-p 0019e000 08:03 2490393                    /lib/libc-2.16.so
7f8a5fa93000-7f8a5fa97000 rw-p 00000000 00:00 0 
7f8a5fa97000-7f8a5fab8000 r-xp 00000000 08:03 2490410                    /lib/ld-2.16.so
7f8a5fc8a000-7f8a5fc8d000 rw-p 00000000 00:00 0 
7f8a5fcb4000-7f8a5fcb8000 rw-p 00000000 00:00 0 
7f8a5fcb8000-7f8a5fcb9000 r--p 00021000 08:03 2490410                    /lib/ld-2.16.so
7f8a5fcb9000-7f8a5fcba000 rw-p 00022000 08:03 2490410                    /lib/ld-2.16.so
7f8a5fcba000-7f8a5fcbb000 rw-p 00000000 00:00 0 
7fff8533d000-7fff8535e000 rw-p 00000000 00:00 0                          [stack]
7fff853ff000-7fff85400000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted

I am using gcc 4.7.1 under ArchLinux 3.4.4-2-ARCH x86_64

Does anyone know why this is happening?

Thank you very much for all help in advance

  • 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-09T17:35:21+00:00Added an answer on June 9, 2026 at 5:35 pm

    A possible cause would be an attempt to free any of the elements in inputRedir, outputRedir or errorRedir if they are unchanged. As they are initialised to a string literal (empty string) passing this to free() is illegal:

    If ptr is a null pointer, no action occurs. Otherwise, if
    the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc,
    the behavior is undefined.

    A speculative fix would be to initialise the elements to NULL pointers (it is safe to pass a NULL pointer to free()) for these three arrays:

    char *inputRedir[4]  = { NULL, NULL, NULL, NULL };
    char *outputRedir[4] = { NULL }; /* Unspecifed initializers will */
    char *errorRedir[4]  = { NULL }; /* be NULL by default.          */
    

    Note that the format specifiers in the printf() statement are incorrect as inputRedir[0] (and outputRedir[0]) is a char* but the format specifier provided is %c, which is for type char: use %s for char*.

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

Sidebar

Related Questions

I am having problems passing a javascript array to an MVC3 controller, not sure
I'm having trouble passing a big array to a function in C. I declare:
I'm having some problems passing an integer array to an MVC controller. I'm grabbing
Im having problems passing PHP Strings to Javascript functions, I'v read a number of
I'm having problems passing an array parameter to my .ashx handler. Querystring parameters work
I am having problems with passing arguments to new threads, I have the following
I'm having some problems with Jquery UI Dialog and TinyMCE. Passing data to a
I'm trying to use HTTPWebRequest to access a web service, and am having problems
I'm having a problem passing strings that exceed 80 characters in JSON. When I
I'm having a problem with the inArray function not sure why. I'm passing a

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.