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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:39:45+00:00 2026-05-26T00:39:45+00:00

I have been struggling with this problem for the last few hours, and it

  • 0

I have been struggling with this problem for the last few hours, and it is one of the stranger problems I have encountered in my 3 years learning programming.

I am trying to create a longer, space-separated string by concatenating strings found in argv from stdin. This is the code I originally wrote:

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

#include "stringtable.h"
#include "strhash.h"
#include "auxlib.h"


int main (int argc, char **argv) {
    int c;
    char* filename;

    while((c = getopt(argc, argv, "@:Dly")) != -1){
        switch(c){
            case '@': 
                printf("@ detected\n");
                char* debugString = optarg;
                int pos_in_input = optind;
                while(argv[pos_in_input][0] != '-' && argv[pos_in_input] != NULL){
                    strcat(debugString, " ");
                    strcat(debugString, argv[pos_in_input]);
                    pos_in_input++;
                    if(argv[pos_in_input] == NULL){break;}                  
                }
                printf("%s\n", debugString);
                break;
            case 'D':
                printf("D detected\n");
                break;
            case 'l':
                printf("l detected\n");
                break;
            case 'y':
                printf("y detected\n");
                break;
            default :
                printf("default detected\n");
        }
    }
}

Most of the above is my unfinished options handling. The code segment of interest is in the switch statement under case : “@”. The while loop is supposed to be creating the debugString by concatenating the strings found in argv, stopping when a string begins with “-” or when the end of argv is reached. I am also adding spaces between the strings with “strcat(debugString, ” “);” It is adding spaces that is giving me trouble.

If I concatenate all the strings without adding spaces in this way:

            while(argv[pos_in_input][0] != '-' && argv[pos_in_input] != NULL){
                strcat(debugString, argv[pos_in_input]);
                pos_in_input++;
                if(argv[pos_in_input] == NULL){break;}                  
            }

I get the following:

 Input: -@what is going on here?
Output: @ detected
        whatisgoingonhere?

This is how I expected it to work. However, if I run the code that adds spaces:

            while(argv[pos_in_input][0] != '-' && argv[pos_in_input] != NULL){
                strcat(debugString, " ");
                strcat(debugString, argv[pos_in_input]);
                pos_in_input++;
                if(argv[pos_in_input] == NULL){break;}                  
            }

Then I get the following as output:

 Input: -@what is going on here?
Output: @ detected
        what  going on here?

Notice the two spaces between ‘what’ and ‘going’. My program is still correctly adding spaces, even though for some reason a word is getting deleted. I have tried this with many different inputs, it is always the second string in input that gets deleted. Anyone know what is going on?

P.S. Compiling with gcc -g -O0 -Wall -Wextra -std=gnu99 -lm

None of the include files that I created are used in this piece of code, so the problem is not something in my includes.

  • 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-26T00:39:45+00:00Added an answer on May 26, 2026 at 12:39 am

    Your code of interest starts with:

                char* debugString = optarg;
    

    This makes the debugString pointer point to the same place that optarg points to. This does not allocate a “new” string in any way. By calling strcat(debugString, ...), you are overwriting whatever else happens to appear in memory after the end of optarg. Chaos will ensue.

    To address this problem, try:

                char debugString[1000];
                strcpy(debugString, optarg);
    

    This will allocate 1000 bytes for you to stuff bits of string into using strcat. It is your responsibility to ensure that you do not write past the end of the debugString buffer (so if you find you have more than 1000 bytes to concatenate, you’ll have to decide what you want to do with that).

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

Sidebar

Related Questions

I have been struggling with this seeminly easy problem for 48 hours, and I
I have been struggling with this problem for a few days now and I
I've been struggling with this problem for 5 hours and I have a feeling
I've been struggling with this problem for a few hours and it is preventing
I have been struggling to find a solution to this problem. In my code,
I have been struggling a while with this problem and read a lot but
I've been struggling with this problem for a good 2 hours now and I
I have been struggling with this problem on hostgator shared hosting server for the
I'm a Flex noob and I've been struggling with this problem for a last
I have been struggling with this problem for a while now. I have 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.