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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:02:09+00:00 2026-06-12T09:02:09+00:00

I am writing a C program like the following where I am assigning a

  • 0

I am writing a C program like the following where I am assigning a value to a variable
pointed by a global pointer, which is being changed by a function call on the right side of the assignment.

But the change occurs to the location where it pointed before.
So for the following code, it prints

“GP:5 P:5 GPV:11” ,

where it should be

“GP:10 P:10 GPV:9”

I tried it compiling with both -O2 and -O0. Same output.

I know that it is fixed if I just break the assignment into 2 lines. But the project I am working on has lots of places like this.

Is this possible to make it work like I want without breaking it into 2 lines?

Any suggestion is highly appreciated.

#include <stdio.h>

int * gp;  
// volatile int *gp;   // I tried these two.    
// int * volatile gp;  // Didn't help.

int func(int *p) 
{  
   *p = 5;
   gp = p;  
   return *p;
}

int main()                                                                      
{                                                                               
   int p = 7;
   int gp_v;
   gp = &gp_v; 
   *gp = 8;
   *gp = func(&p) + *gp;
   gp_v ++;
   printf("GP:%d P:%d GPV:%d\n", *gp, p, gp_v);
   return(0); 
}
  • 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-12T09:02:11+00:00Added an answer on June 12, 2026 at 9:02 am

    This line has unspecified behavior:

    *gp = func(&p) + *gp;
    

    The C standard does not specify the order that arguments are evaluated in within expressions, so the compiler is free to execute func(&p) either before or after reading *gp for computing the addition. Likewise, it can evaluate the *gp on the left-hand side of the assignment (to determine that address where it’s storing the result to) before or after it evaluates the value it’s storing.

    You need to break up your statement into separate statements in order for it to have well-defined behavior. Either of these would work, but they have different semantics:

    // Option 1: write to old location of *gp
    int *old_gp = gp;
    int old_gp_val = *gp;
    *old_gp = func(&p) + old_gp_val;
    
    // Option 2: write to new location of *gp
    int old_gp_val = *gp;
    int new_val = func(&p) + old_gp_val;
    *gp = new_val;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing a program in which I would like to access the variable
The following is a problem which i encountered while writing a program where i
I'm writing a blackjack program and would like to be able to write the
I am writing a program in assembly and it isn't working, so I'd like
I'm currently writing a program in Python, and I would like to determine the
I'm writing a C program that needs good error handling. The code likes like
For the python program I am writing I would like to give the opportunity
I'd like to add video conversion capabilities to a program I'm writing. FFmpeg's command
I'm writing a physics simulating program, and found after() useful. I once would like
I am writing a program doing the following works: Run a command using ProcessBuilder

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.