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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:19:39+00:00 2026-05-21T05:19:39+00:00

I can’t figure out how to tell C that I want a pointer that

  • 0

I can’t figure out how to tell C that I want a pointer that will not move. It will always point to the same array. That said, the array members are not constant, but the array itself is global and so, it is at a fixed position.

So, when I code this:

#include <stdio.h>

int v[2]={0, 1};
const int *cpv=v;

int main(void)
{
    v[1]=2;     printf("%d\n", v[1]);
    *(cpv+1)=3; printf("%d\n", v[1]);
    cpv[1]=4;   printf("%d\n", v[1]);
}

And get this errors:

constp.c: In function ‘main’:
constp.c:9: error: assignment of read-only location '*(cpv + 4u)'
constp.c:10: error: assignment of read-only location '*(cpv + 4u)'

I understand that the compiler thinks I need a const int v[2] to use with a const int *iv. How do I get a constant pointer to do the job?

If you see the error message, I’m not even moving the pointer (like pv++). I’m just dereferencing it dislocated some bytes.

If I do this:

int *pv=cpv;
*(pv+1)=5;  printf("%d\n", v[1]);
printf("%p == %p !?\n", cpv, pv);

I get this warning, but it works:

constp.c:9: warning: assignment discards qualifiers from pointer target type
pointer# ./constp 
5
0x601020 == 0x601020 !?

Thanks,
Beco.

  • 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-21T05:19:40+00:00Added an answer on May 21, 2026 at 5:19 am

    Move the const qualifier:

    int *const cpv=v;
    

    Explanation: in the C declaration rules, this is read from right to left starting at the identifier: “cpv is a constant pointer to int“. Your version would be read “cpv is a pointer to int constant”.

    Note that cpv+1 will still get you a pointer to the int after *cpv; making a pointer const only prevents ++, --, += and -= on it.

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

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can't work out a way to make an array of buttons in android. This
Can anyone point me to how i can view files that have been removed
Can somebody point me to a resource that explains how to go about having
Can any one please tell me where to find a simple tutorial that shows
Can any one tell, how to get the result of LINQ query contains group
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can anybody tell what is the best(easy) way to sort the following string 'index'
Can someone guide me on a possible solution? I don't want to use /bin/cp

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.