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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:16:09+00:00 2026-06-06T14:16:09+00:00

As caf talked about it in https://stackoverflow.com/a/1113041/1354779 , there is a way to use

  • 0

As caf talked about it in https://stackoverflow.com/a/1113041/1354779,
there is a way to use initialisers when the variable is declared to initialize an array already declared.
That is great, and I now would like to know if there is a way to use every item of the array, one by one.

I use that to pass on a list of commands through a pipe, and I know that this works:

char* script[]={"report blabla","report bla"};
char line[200];
char** command;
for (command = script ; **command ; **command ? command++ : 0){
    if (**command){
        SendCommand(*command, line, sizeof(line));
    }   
}

But when I try my code below, I get an “Unhandled exception at 0x778915de in pipe_GWB9.exe: 0xC0000005: Access violation reading location 0xcccccccc” :

char* request[] = {"report watact"};
char line[200];
// [...] Other code [...]
static const char *tmp[8] = 
  {
  "report molality H+",
  "report molality Cl-",
  "report molality Ca++",
  "report molality Mg++",
  "report molality K+",
  "report molality Fe++",
  "report molality SO4--",
  "report molality Na+"
  }; 
  memcpy(request, tmp, sizeof request);
char** command;
command=request;
SendCommand(*command, line, sizeof(line));
// Until here, everything works great.
**command ? command++ : 0;
SendCommand(*command, line, sizeof(line));
// But THAT doesn't work!!

Would you be able to help me calling for the other items of the array request?

Thanks

  • 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-06T14:16:11+00:00Added an answer on June 6, 2026 at 2:16 pm
    char* request[] = {"report watact"};
    

    request is an array of pointers to char which contains 1 element.

    static const char *tmp[8] = 
      {
      "report molality H+",
      "report molality Cl-",
      "report molality Ca++",
      "report molality Mg++",
      "report molality K+",
      "report molality Fe++",
      "report molality SO4--",
      "report molality Na+"
      }; 
    

    tmp is an array of pointers to char with 8 elements.

    memcpy(request, tmp, sizeof request);
    

    You copy the first four bytes (assuming sizeof request == 4) from tmp into request, i.e., you copy the first element of tmp into the first element of request.

    char** command;
    command=request;
    

    Simple enough; command now points to the first (and only!) element of request.

    **command ? command++ : 0;
    

    Oops. You just ran past the bounds of command (request) because request contains only a single element. You then dereference it when calling SendCommand on the next line, which gives you your seg fault. Note that anything could happen here because you are invoking UB.

    For example, what may happen if you cut out the code between the two arrays is that the string "report molality H+" is sent twice because incrementing the pointer past the bounds of request may very well just plop you onto the beginning tmp.

    If you want to copy all of tmp into request then you need to make sure the two arrays are of the same size (or at least, that request is as large as tmp).

    As an aside, this ternary expression is a bit silly:

    **command ? command++ : 0;
    

    You only care about one branch, so why use a ternary at all? Prefer:

    if(**command)
        command++;
    

    That said, this is not a safe way to check the bounds of your array (as you found out). You are treating the array of pointers as if it were a pointer to char, i.e., scan until you find the NULL terminator.

    There is no NULL terminator for an array, you simply end up walking past its boundaries and invoking undefined behavior, so you need to keep track of the size separately and check against that.

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

Sidebar

Related Questions

is there a way to convert my recorded .caf files to .mp3 using the
are there speed/performance differences between caf and mp3? Which format is better to use
Can I safely rename an aif file to caf and use that with Core
with iphone SDK 4.0 i want to use some system sound files eg /System/Library/Audio/UISounds/dtmf-0.caf
I have several directories of 12 .caf files and am loading them programmatically: NSString
My raw input file text file contains a string: Caf&eacute (Should be Café) The
Is it possible to save . caf file into the photo library ? In
-(IBAction)playSound{ AVAudioPlayer *myExampleSound; NSString *myExamplePath = [[NSBundle mainBundle] pathForResource:@myaudiofile ofType:@caf]; myExampleSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
I am using following code to combine .mp4 and .caf into .mov . (Note
Can i store iPhone's recorded audio sound into CAF format?

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.