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 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

This is a challenge for the css gurus out there. I want to show
I need some help from the shell-script gurus out there. I have a .txt
I've been asked to help out on an XNA project with the AI. I'm
Can anybody help me out to know the possible reasons for which Apple store
Can someone please help me out with printing the contents of an IFrame via
I tried searching around, but I couldn't find anything that would help me out.
Can someone help me figure out the following make file? BINS=file1 file2 file3 all:
I am wondering if someone can help me figure out the best approach to
I have a little dilemma that maybe you can help me sort out. I've
Is there guide out there that will help me, A .Net developer whose been

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.