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

  • Home
  • SEARCH
  • 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 4321122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:42:30+00:00 2026-05-21T08:42:30+00:00

I am trying to parse a c char array. It is for class so

  • 0

I am trying to parse a c char array. It is for class so I have to use c. I am having problems splitting the tokens

I need to be able to accept two different parameters in the following formats:
[1234]
or
[1234 abcd]

When I parse the “[1234 abcd]” I have no issues. But, when I try parsing “[1234]” I get a “no match” error. Although, when I try “[1234 ]” I have no issues.

Can anyone tell me why this would be the case?

Below is my test code:

$ ./parsemem
Splitting string "[1234 abcd]" into tokens:
ip1:1234, ip2:abcd
var1:1234, var2:abcd 

parsemem.c

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

//int parse()
int parse(char *str, char *v1, char *v2)
{
 char string[11];
 strcpy(string,str);
 char * pch;
 char ip[1][5];
 int i=0;

 printf ("Splitting string \"%s\" into tokens:\n",string);
 pch = strtok (string," []");
 while (pch != NULL)
 {
   strcpy(ip[i],pch);
   pch = strtok (NULL, " []");
   i++;
 }

 printf("ip1:%d, ip2:%s\n", atoi(ip[0]), ip[1]);
 strcpy(v1,ip[0]);
 strcpy(v2,ip[1]);

 return 0;
}

int main()
{
char str[] ="[1234 abcd]";
//char str[] ="[1234]";
//char str[] ="[1234 ]";
char var1[5];
char var2[5];

parse(str,var1,var2);

printf("var1:%d, var2:%s\n", atoi(var1), var2);

}

@AndreyT

I am having a similar problem again. When I tried to integrate this into my program using [1234] as an argument I am getting “No match.” Although if I put a space before the ] like [1234 ] There is no problem with strtok.

Having the ] at the end of my string I want to tokenize is a problem. Any ideas why this is the case?

$./parsemem [1234]
./parsemem: No match.


$./parsemem [1234 ]
Splitting string " [1234" into tokens:
ip1:1234, ip2:
var1:1234, var2:
  • 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-21T08:42:31+00:00Added an answer on May 21, 2026 at 8:42 am

    String "[1234 abcd]" requires a char array of size 12 (at least). Inside parse you are copying it into an array of size 11. Why?

    Also, your ip array is declared with first size 1, which means that it is illegal to access ip[1]. You can only access ip[0].

    In other words, your code is hopelessly overrunning array memory in several places. The behavior is undefined. Until you fix the above errors (and any other errors of that nature), any questions about the run-time behavior of your code make no sense at all.

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

Sidebar

Related Questions

I am trying to load a html page through UIWebview.I need to disable all
I'm trying to write test harness for part of my Android mapping application. I
I'm trying to build a C++ extension for python using swig. I've followed the
I'm trying to build a Chrome browser extension, that should enhance the way the
I am trying to redirect to a specific path based on HTTP_HOST or SERVER_NAME
I am trying to understand the practical difference during the execution of a program
I am playing with TFS 2010, and am trying to setup a build process
I have several USB mass storage flash drives connected to a Ubuntu Linux computer

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.