-2

Given arrays of pointers, if I want to write a function that move about all the array and check if exists element with specific key (for example) , and if yes, I want to return exactly array[i] so that I will can to initialize this ceil again after calling to this function, for example:

func(array,size,key) = 3 --> array[i_key]=3 (it's a simple example, not of pointers like I writed above, for explain what I want).

But, can be case that key not exists in the array, and then I need to return null (at least, I think so). But I can not do it while I returns a reference.

Someone have idea how to solve this problem?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
AskMath
  • 415
  • 1
  • 4
  • 11
  • Look here for the discussion about _optional references_: https://stackoverflow.com/questions/26858034/stdoptional-specialization-for-reference-types. Generally, its recommended to use pointers in cases like yours. – Daniel Langr Jun 04 '18 at 15:31
  • 1
    Why not return the pointer? If that does not solve your problem, I suggest you give a real example. – Beta Jun 04 '18 at 15:32
  • @Beta It's not solve my problem because that exists cases such then I will need to change the ceil that this element exists there by other element (for example). – AskMath Jun 04 '18 at 15:33
  • @AskMath Don't understand. What can you do with references that you wouldn't be able to do with pointers? You could provide some example. – Daniel Langr Jun 04 '18 at 15:35
  • 1
    If a reference does the job for you, a pointer will as well. References, if used as function arguments or return values, are nothing more than some kind of special pointer with different syntax and some limitations... – Aconcagua Jun 04 '18 at 15:36
  • @DanielLangr You can't change the value in this ceil by other element if you want. You get just the element itself. – AskMath Jun 04 '18 at 15:36
  • @AskMath What do you mean by _"changing the value in this ceil by other element"_? Please explain. – Daniel Langr Jun 04 '18 at 15:37
  • I am sorry, I now understand your mention. I aksed for array of pointers, and I thought you mentioned about a some pointer in this array. But now I think that your mention is to do something like that: `return &array[i]` , Yes? – AskMath Jun 04 '18 at 15:38
  • Just one second: Given `C* c = array[i];` with some valid class C, do you mean you want to modify some unrelated `array[j]` via the pointer `c`??? You could, just add some offset to `c`: `(c + 10)->member = 12;`. However, this is ***highly*** dangerous! You need to know from pointer `c` the exact distances to to array begin and end to do such stuff safely! So if I met your point, I strongly recommend considering another approach... – Aconcagua Jun 04 '18 at 16:22

1 Answers1

1

In your case, pointers is the way to go.

If you find the element, return a pointer pointing to that element.

Else, return a null pointer.

It's very common, and natural.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    If you need to change the element that you found @AskMath, you could that via the pointer. Is that your question? – gsamaras Jun 04 '18 at 15:36