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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:03:37+00:00 2026-06-13T16:03:37+00:00

I’m new to C and am having trouble with strings. What I would like

  • 0

I’m new to C and am having trouble with strings. What I would like to do is create a string like “val1, val2, val3” in a loop.

Currently my code looks something like:

char tagstr[60] = "";
int k;
int n = 5;
for (k=0; k < n; k++) {
    char temp[10]  = "";
    sprintf(temp, ", val%d", k);
    strcat(tagstr, temp);
}

But the output of tagstr is “, val#”, where # is some long integer value. I’m guessing I’m doing something wrong with pointers here but I’ve tried everything I can think of without success… any help would be much appreciated.

EDIT: more context, if it helps:

int tagsClosed = strlen(pch1) - strcspn(pch1, ")");
do {
    if (curTag.size > 0) {
        // problem section
        char tagstr[60] = "";
        int k;
        for (k = 0; k < 5; k++) {
            char temp[10] = "";
            sprintf(temp, ", val%i", temp, k);
            strcat(tagstr, temp);
        }

        // This prints out something like ", val-890132840" x 5 (same number)
        printf ("String is now: %s\n", tagstr);
    }
    curTag = *(curTag.parent);
    tagsClosed--;
} while (tagsClosed > 0);

curTag is a struct:

typedef struct Tag {
    char * name;
    int size; // number of children
    int tagnum;
    struct Tag* parent;
} Tag;
  • 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-13T16:03:39+00:00Added an answer on June 13, 2026 at 4:03 pm

    The problem is that sprintf(temp, ", val%i", temp, k); adds the value of temp (which is actually the address of the first character in the array) to the string, and doesn’t add the value of k to the string at all. This should be sprintf(temp, ", val%i", k);.

    You can calculate the amount of space you’d need in advance (including zero terminator):

    5+1 + 5+1 + 5+1 + 5+1 + 5+1 + 1 = 31 characters
    

    Also; using strcat is bad (for performance) because you’d be repeatedly searching for the end of the tagstr and then copying the new characters to the end. It would be better to keep track of the current end of tagstr and store then next group of characters directly at the end with no searching, no temporary string and no copying. For example:

    void thing(void) {
        char tagstr[60];
        int pos = 0;
        int k;
        int n = 5;
    
        for (k=0; k < n; k++) {
            pos += sprintf(&tagstr[pos], ", val%d", k);
        }
        printf ("String is now: %s\n", tagstr);
    }
    
    • 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
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.