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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:06:33+00:00 2026-05-13T21:06:33+00:00

I am trying to pass in arguments to the a parent file that is

  • 0

I am trying to pass in arguments to the a parent file that is supposed to create a child process for each pair of arguments. The child processes will add up each pair and return their sum to the parent process. If there is an odd number of arguments passed in, I add a 0 to the end of the argv array to make it even. This keeps happening until all the arguments have been added up, and their sum is printed at the end.

Everything is working fine except when I pass in an even amount of arguments. A child process will successfully add the first two arguments and return them, but then when the parent does a sprintf, (line 58) there is always a segmentation fault.

Here is the code for both the parent and child processes (I am using pastebin so it doesn’t look very cluttered here. They will expire in one day, so I could re-post them if needed):

  • http://pastebin.com/zCB700Et
  • http://pastebin.com/VxXKYrdn

The second file has to be called worker when it is compiled in order to be run by the first file. I am using gcc on Ubuntu 9.10

Here are a few examples on how to run the program:

gcc -o parent parent.c
gcc -o worker worker.c
./parent 1 2 3 4

(The above example will end with a segmentation fault like I explained above). The answer should be 10 because (1 + 2) + (3 + 4) = 10.

This one below will work fine though with an odd number of arguments being passed in:

./parent 1 2 3

The answer to this one should be 6.

Any help would be greatly appreciated!

  • 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-13T21:06:34+00:00Added an answer on May 13, 2026 at 9:06 pm

    The C standard allows one to modify the strings pointed-to by argv:

    The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

    But it doesn’t guarantee you to write past the available number of arguments in argv, which is what you are doing when you store the sum at the end of argv.

    You seem to be using argv[i] for char * variables. Why not just declare separate variables for the purpose of storing the sum? You also don’t need to store "0" in argv[argc-1] if the number of numbers to sum is odd—you can easily determine that in your loop, and pass "0" to the last worker.

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

Sidebar

Related Questions

I am trying to create a custom activity for WF4 that host a child
I'm trying to pass arguments in XCode and understand you need to add them
Trying to pass a string argument to a function, which will then be used
I'm trying to pass a string argument to a target function in a process.
I am trying to pass arguments to the align-regexp function in Emacs (Lisp): (defun
I'm trying to pass some arguments into a new Powershell remoting session using the
I'm trying to pass a list of arguments to a python script using the
I'm trying to pass keyword arguments to a Django view using a dictionary, but
I'm trying to create a haml template that uses some data from my ruby
I am trying to pass some extra arguments so they can be used at

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.