Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

60%
+1 −0
Q&A Are there references in C?

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...

posted 3y ago by klutt‭  ·  edited 3y ago by klutt‭

Answer
#2: Post edited by user avatar klutt‭ · 2022-02-10T01:03:26Z (almost 3 years ago)
  • 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 by user avatar klutt‭ · 2022-02-10T01:02:41Z (almost 3 years ago)
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>