0

How can i make Alt.1 to work as intended by passing a pointer of an array and get the requested reference of an array in Alt.1 ?

struct mystruct
{
    int id1;
    int id2;
};

const struct mystruct local_struct[] = {
    {0, 55},
    {1, 66},
};

// Alt.1 i like to make this work (not working)
int get_reference_1(const struct mystruct *s){

   s = local_struct;
   return 0;
}

// Alt.2 works perfect but i like to use the return as status as in Alt.1.
const struct mystruct *get_reference_2(){
   return local_struct;
}

int main()
{
  struct mystruct *s = NULL;

  // Alt.1
  if(get_reference_1(s))
     /* Expected Fail*/
  else
     /* Expected Success*/

  // Alt.2
  s = get_reference_2()
  if(!s)
     /* Expected Fail*/
  else
     /* Expected Success*/

  return 0;
}

Maybe i'm thinking wrong and i need to pass a double pointer?

Edit: Corrected with 'const'. Edit2: Updated header.

4
  • There are many duplicates. You apparently still don't understand quite well how pointers work, please read more about it. The reason it doesn't work is because s is local to get_referenec_1. You have to pass a pointer to the poitner you want to update. Commented Feb 16, 2018 at 14:50
  • why would tou call a global struct "local_struct"? Are you trying to confuse the enemy? Commented Feb 16, 2018 at 14:50
  • @IharobAlAsimi: updated with const. Commented Feb 16, 2018 at 15:05
  • @EugeneSh. This is just a short example of my problem. I'm working in a bigger system which i cannot include everything. Sorry for the confusion. Commented Feb 16, 2018 at 15:07

2 Answers 2

3

s = local_struct; is changing a local variable - it won't change the one in main. Pass the address of the variable and make changes to the original variable dereferencing it.

int get_reference_1(struct mystruct **s){

   *s = local_struct;
   return 0;
}

Calling it would be

  if(get_reference_1(&s))
     /* Expected Fail*/
  else
     /* Expected Success*/

Also you are making the compiler complain by assigning a const variable to non-const one. Here the local_struct is a constant struct declared in your code. Solution check whether you are doing the right thing - is this assignment necessary? You could also add const qualifiers as needed:

int get_reference_1(const struct mystruct **s){
   *s = local_struct;
   return 0;
}
...
const struct mystruct *s = NULL;

In the worst case drop the const qualifier.

Sign up to request clarification or add additional context in comments.

6 Comments

Right now I have terrible net. My edits are slow.. I apologize.
@dasblinkenlight.: const struct mystruct *a is basically saying that a is a pointer to a const. Modifying the value of a struct will be illegal. But the pointer is free. We can make it point anywhere. So we can only read through this pointer. If we want to create a linked node by modifying some member of a struct instance then it will complain. (via p we will access it). Thanks for the edit.
Since OP's local_struct is marked const, we have no alternative but to produce a const pointer to it. Otherwise we might accidentally modify local_struct through a pointer to it, causing undefined behavior. The only way to keep s modifiable is to remove const from local_struct.
@dasblinkenlight. Is the right thing to say is pointer to const struct? Just working my way through writing more clearly.
@dasblinkenlight, the purpose is to avoid modification, therefor the const.
|
2

Here you go what you want

struct mystruct
{
    int id1;
    int id2;
};

 struct mystruct local_struct[] = {
    {0, 55},
    {1, 66},
};

// Alt.1 i like to make this work (not working)
int get_reference_1(struct mystruct **s){

   *s = local_struct;
   return 0;
}

// Alt.2 works perfect but i like to use the return as status as in Alt.1.
struct mystruct *get_reference_2(){
   return local_struct;
}

int main()
{
  struct mystruct *s = NULL;

  // Alt.1
  if(get_reference_1(&s))
  {  
      /* Expected Fail*/
  }
  else
  {
      /* Expected Success*/
  }   

  // Alt.2
  s = get_reference_2();
  if(!s)
  {   
      /* Expected Fail*/

  }
  else
  {
      /* Expected Success*/
  }   

  return 0;
}

It will execute successfully.

1 Comment

Thanks for your quick reply, this worked as well as the proposed one before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.