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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:08:45+00:00 2026-06-13T17:08:45+00:00

I have a struct which contains some pointers. I want the value of these

  • 0

I have a struct which contains some pointers. I want the value of these to be unmodifiable. But simply writing const infront doesn’t make the structs members unmutable

typedef struct{
  int *x;
  int *y;
}point;

void get(const  point *p,int x, int y){
  p->x[0]=x;//<- this should not be allowed
  p->y[0]=y;//<- this should not be allowed
}

Can someone point me in the right direction.

EDIT:

So it would seem that there is no simple way of using the function prototype to tell that everything belonging to the struct should be unmodifiable

  • 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-13T17:08:47+00:00Added an answer on June 13, 2026 at 5:08 pm

    To explain what you need to establish, when you write

    point str;
    
    point *p=&str;
    

    Here p is a pointer to str which is of type point

    When you declare it as const, it means that p is a constant pointer. This does not restrict the pointers that the structure may contain.

    If you want the const ness to apply inside the structure, you have to define the pointers inside the structure also as const

    typedef struct{
       const int *  x;
       const int *  y;
    }point;
    

    Again to push home my point declare the parameter as

        void get(point * const  p,int x, int y) 
       //Constant Pointer ( *to prevent p from pointing to anything else*)
    
        //    AND
    
       //Make the pointers inside point structure constant
       //( *to prevent the int pointers x & y from pointing to anything else*)
    

    If the structure it is pointing to is also const use

          void get(const point * const p, int x, int y)
         //Constant Pointer to constant structure 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an huge array which contains a struct Tile. The program im writing
I want to have some data cache which contains some objects which I can
-In my c code I have a struct which contains many unknown sized arrays
I have a file Tools.ml which contains some common utility functions I write myself.
I'm accessing a C struct which contains some time_t fields using python ctypes module.
seeking some advice here. I have a structure which contains a pointer to another
I have a structure which contains character array on C side stuct s {
I have a struct which corresponds to a register in hardware. typedef unsigned int
I have a struct which has several arrays within it. The arrays have type
For example, I have a struct which is something like this: struct Test {

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.