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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:43:13+00:00 2026-05-18T12:43:13+00:00

I’m programming a feedback loop that takes in user commands and processes the arguments

  • 0

I’m programming a feedback loop that takes in user commands and processes the arguments based on the command received. So far my function looks like this:

//main loop of the program that keeps repeating, which each cycle taking in one user input
 void mainLoop(){

char input[100]; // initial command
char reg[2]; // registers R1-R7 and PC
char value[20]; // register value
char breakStatus[20]; // can be either "set" or clear"
char breakAddress[80]; // address of break point in hex


//Retrieve user input
printf("Enter a command: ");
scanf("%s", &input);

//Process user input:

//set (completed)
if(strcasecmp(input, "set") == 0){
    scanf("%s", &reg);
    scanf("%s", &value);    
    set(reg, value);
    printf("register value is %s\n", value);
    printf("value[0] is %x\n", value[0]);
    //printf("Setting %s to %s!\n", reg, value);
}

//break (something's wrong here)
else if(strcasecmp(input, "break") == 0){
    scanf("%s", &breakStatus);
    scanf("%s", &breakAddress);

    printf("breakStatus is %s\n", breakStatus);
    printf("breakStatus[0] is %c\n", breakStatus[0]);
    //printf("%sing break point at address %s\n", breakStatus, breakAddress);
    executeBreak(breakStatus, breakAddress);

}


//error (completed)
else
    printf("Error: %s is not a valid command.\n", input);

 }

My problem is that whenever I try to access breakAddress[index], I get a Segmentation Fault. When I tried printf-ing breakStatus and breakAddress, they both hold the expected values (an input example would be break set x0001, where breakStatus = “set” and breakAddress = “x0001”).

I know that if I use the specifier to %c, or printf(“breakStatus[0] is %c\n”, breakStatus[0]); , it returns a valid value. However I am passing breakStatus and breakAddress into another function, and in that function I check if breakAddress[0] == ‘x’, to which I promptly receive a Segmentation Fault.

I’m not sure how to handle this. I’ve tried casting breakAddress[0] to a char and I still get the same error.

What’s really confusing me is that I pretty much coded my Set portion in the exact same way (same char[] initialization, same scanf process) and when I tried retrieving an index from one of the variables it works perfectly fine.

Below are parts of my set() and executeBreak() functions for reference:

void set(char* reg, char* val){
    if(val[0] == 'x'){ // no segmentation fault
    sscanf(val+1, "%x", &registers[r]); 
}

void executeBreak(char* stat, char* add){
    if(!(add[0] == 'x')){ // segmentation fault here (tried casting as char also)
    printf("Error: invalid break address");
    return;
    }
}

If anyone could offer some insight as to why I’m getting a Segmentation Fault in one function and none in the other it would be greatly appreciated.

EDIT: Got rid of using %s in my printf statement since that’s not really where my problem is. Ideally I’d like to know why val[0] works in on function and add[0] does not, since they’re given the same types of arguments.

  • 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-18T12:43:14+00:00Added an answer on May 18, 2026 at 12:43 pm

    Heed the compiler warnings! 🙂

    You have defined char breakStatus[20];, i.e. an array of characters. Then, in the printf statement, you’ve got printf("...%s...", breakStatus[0]);.

    The %s formatting code expects a pointer to a string. breakStatus[0], however, is a char value. The compiler warns you about what it does to fix that: it turns the char value into a char * pointer (char value = 0 => pointer address is 0). Suppose the char value happens to be zero, then the whole thing magically turns into a NULL pointer. Trying to dereference a NULL pointer (or a 0xff pointer, perhaps… the value is essentially random, after all) almost inevitably causes a segfault.

    (Update: what Vlad said… “did you mean: %c”)

    • 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
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
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 ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.