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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:28:14+00:00 2026-05-16T22:28:14+00:00

Help me out, gurus /* * In this function, I want the static variable

  • 0

Help me out, gurus

/*
 * In this function, I want the static variable position to point to next element
 * of the str, every time I call this function.
 */
void function(char *str){

   static char *position = str;

   /* Do something here to this position */

   position += 1;
 }

The purpose of this program is to do string substitution, every time I substituted a pattern of the str I have to let the static position variable point to a new position of the str, and then I will copy every thing into another new string.

The problem is, the compiler keep telling me “the initializer element is not a constant”, how can I fix this?

  • 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-16T22:28:14+00:00Added an answer on May 16, 2026 at 10:28 pm

    You can’t have a static variable in your function to point to the next element of str, because position is a global variable that is initialized once, and str may have different value every time you call a function.

    What you need here is a loop that iterates over str,

    void func1(char *str) {
        char *p;
        for (p = str; /* some condition here */; ++p) {
            /* Do something here to this position */
        }
    }
    

    or have a loop outside this function and pass str incremented by 1 every iteration.

    void func1(char *str) {
        /* Do something here to this position */
    }
    
    void func2() {
        char *str = ...;
        ...
        char *p;
        for (p = str; /* some condition here */; ++p) {
            func1(p);
        }
    }
    

    Of course, you can initialize a static to NULL first and use it to check if you started iterating over str, but that’s poor style: too stateful and error prone, not reentrant and not thread-safe.

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

Sidebar

Related Questions

In the process of trying to help out an app dev team with performance
Have a bit of a weird one and hopefully someone can help out. The
This is driving me nuts! I have a test instance of Wordpress installed on
I want prevent user to select dropdown list items using javascript methods. Is there
I got a question regarding C++ Object & Javascript Object life cycle mis-synchronization and
I'm trying to create a short bash script someone could run in cygwin to
Good Afternoon All, I have a table inside a div. The div has a
I am taking a quadratic expression, where y=ax^2 + bx + c with a
While working on a vb.net application, in which exceptions were handled poorly. It was

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.