Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
This question most often comes up in relation to C++. That language has something that is called reference in the standard. They work like pointers, with a few differences. They have to be initi...
Answer
#2: Post edited
- This question most often comes up in relation to C++. That language has something that is called reference in the standard. They work like pointers, with a few differences.
- 1. They have to be initialized, because (2)
- 2. They cannot be reassigned. They will always point to the same object.
- 3. They cannot point to null.
- 4. They are always implicitly dereferenced. No dereferenciation is required or even allowed.
So C++ references are quite similar to const pointers, which should not be mixed up with pointer to const. A const pointer does not technically need to be initialized, but it's rather point(haha)less to not do it, because then they're completely useless. In C++, you will get a compiler error for both uninitialized references and const pointers.- The answer is yes or no, depending on how you want to interpret the phrase "pass by reference". It's both practical and common to talk about that in C, but it is also important to know the difference to "real" pass by reference. For instance, this code is guaranteed<sup>*</sup> to print 42 in C regardless of how `foo()` is defined, but that's not the case in C++.
- ```
- int x = 42;
- foo(x);
- printf("%d\n", x);
- ```
- <sup>* I'm not 100% sure that there isn't a way to provide a counterexample, but that would probably be some real fancy schmancy stuff.</sup>
- This question most often comes up in relation to C++. That language has something that is called reference in the standard. They work like pointers, with a few differences.
- 1. They have to be initialized, because (2)
- 2. They cannot be reassigned. They will always point to the same object.
- 3. They cannot point to null.
- 4. They are always implicitly dereferenced. No dereferenciation is required or even allowed.
- So C++ references are quite similar to const pointers, which should not be mixed up with pointer to const. A const pointer does not technically need to be initialized, but it's rather point<sup>(haha)</sup>less to not do it, because then they're completely useless. In C++, you will get a compiler error for both uninitialized references and const pointers.
- The answer is yes or no, depending on how you want to interpret the phrase "pass by reference". It's both practical and common to talk about that in C, but it is also important to know the difference to "real" pass by reference. For instance, this code is guaranteed<sup>*</sup> to print 42 in C regardless of how `foo()` is defined, but that's not the case in C++.
- ```
- int x = 42;
- foo(x);
- printf("%d\n", x);
- ```
- <sup>* I'm not 100% sure that there isn't a way to provide a counterexample, but that would probably be some real fancy schmancy stuff.</sup>
#1: Initial revision
This question most often comes up in relation to C++. That language has something that is called reference in the standard. They work like pointers, with a few differences. 1. They have to be initialized, because (2) 2. They cannot be reassigned. They will always point to the same object. 3. They cannot point to null. 4. They are always implicitly dereferenced. No dereferenciation is required or even allowed. So C++ references are quite similar to const pointers, which should not be mixed up with pointer to const. A const pointer does not technically need to be initialized, but it's rather point(haha)less to not do it, because then they're completely useless. In C++, you will get a compiler error for both uninitialized references and const pointers. The answer is yes or no, depending on how you want to interpret the phrase "pass by reference". It's both practical and common to talk about that in C, but it is also important to know the difference to "real" pass by reference. For instance, this code is guaranteed<sup>*</sup> to print 42 in C regardless of how `foo()` is defined, but that's not the case in C++. ``` int x = 42; foo(x); printf("%d\n", x); ``` <sup>* I'm not 100% sure that there isn't a way to provide a counterexample, but that would probably be some real fancy schmancy stuff.</sup>