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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:33:23+00:00 2026-06-02T06:33:23+00:00

I have the following code where strcat is causing problem. char* tokens = strtok(buf,

  • 0

I have the following code where strcat is causing problem.

      char* tokens = strtok(buf, "+");
      int n = 0;
      int type = 0;
      char* name = "";
      char* lifetime = "";
      char* data = "";
      for(n=0; tokens!=NULL; n++) {
          if(n==0)
            type = atoi(tokens);
          if(n==1)
            name = tokens;
          if(n == 2) {
             if(type == 1)
                lifetime = tokens;
             else
                data = tokens;
          }
          if(n == 3)
             lifetime = tokens;
          tokens = strtok(NULL, "+");
      }

      if(type == 2) {
         printf("Received Data with Name: %s, Data: \"%s\" and lifetime: %s seconds\n", name, data, lifetime);

         strncat(name, "+", 1);
         printf("Data: %s\n", data);
         strncat(name, data, strlen(data));
         printf("Full Name: %s\n", name);
      }

In the line where I print the name, data and lifetime everything prints correctly. But after the strcat operations I find that data value is empty. After adding debug printf statements I have found that data value becomes empty right after strncat(name,"+",1). What could be the reason? Please help.

  • 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-02T06:33:24+00:00Added an answer on June 2, 2026 at 6:33 am

    Both the name and data pointers point somewhere into the buf string. When concatenating into the name string, you probably happen to overwrite the data string with a null character. You need to allocate a new buffer for name before writing to it:

    char resultingName[BUFFER_LENGTH];
    strcpy(resultingName, name);
    strncat(resultingName, "+", 1);
    [...]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code. #include<stdio.h> #include<string.h> int main() { char str1[1000]=/; char unit[1000]=Hai;
I have following code class User attr_accessor :name end u = User.new u.name =
I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have the following code. It gives me a problem with free memory, but
I have following code in a html form <select name=category class=input onchange=ShowTB(this,'suggest');> <option value=0
I have following code to serialize my data into a file: out = new
I have following code: $boxId = 1; $explainationBox='<input type=button id=<?php echo $boxId; ?> value=send
I have following code: typedef int (*t_Function) (int x); t_Function Functions[MAX_FUNCTIONS]; int f(int x)
i have following code in which, i am fetching the data from the sqlite
I have following code that I creating a grid var store = Ext.create('Ext.data.ArrayStore', {

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.