Pass by reference in C

Hello,

Can I have the below code in C? (I don't want to use a pointer.)
The below code wrote in C++ and works fine. but I want this code in C.

Thanks

in C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
      #include <iostream>
      
       void doit( int & x ) 
        { 
             x = 5; 
        } 
 

        int  main() 
        { 
          int z = 27; 
          doit( z ); 
          cont<<z; 
 
          return 0; 
        } 
        


in C and it doesnt work!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
      #include <stdio.h>
      
       void doit( int & x ) 
        { 
             x = 5; 
        } 
 

        int  main() 
        { 
          int z = 27; 
          doit( z ); 
          printf("z is now %d\n", z); 
 
          return 0; 
        } 
        
> Can I have the below code in C? (I don't want to use a pointer.)
If you're going to cripple yourself before you start, no you can't.

I assume you want a real re-usable answer and not some silly side trick like
- disguise your pointer as an array of 1 element
- global variables
- pre-processor macro tricks.
If you don't want to use pointers don't use C.
You could compile your C code with a C++ compiler.
Firstly, "doesn't work" is a useless problem description. Tell us clearly, precisely and completely what behaviour you're seeing, and how it differs from the behaviour you expect.

Something I can see right away is that you're using a reference as an argument to doit(). References are a C++ feature, not a C feature, so you'll need to change that.

The obvious way to fix it would be to use a pointer argument instead. Is there any good reason why you don't want to use them?
Last edited on
https://www.cs.utah.edu/~germain/PPS/Topics/C_Language/c_functions.html

That link offers using a c++ compiler on your 'extended C' to use c++ & syntax and also shows that the & syntax is not available in C without the extensions. Which is just more words and pictures over what was already said in here.

I assume you mean "I want to use c++ references" not "I don't want to use any pointers".
C is going to be nearly unusable without pointers, probably lots of them, if you plan to write anything of substance.

If you are going to write C with a few C++ extensions anyway, that was a thing in the late 80s... its not a bad pseudolanguage as far as it goes. All you are really doing is writing C++ and using the C relic code that is pulled in by nature of C++ being built from C originally. Its not really a useful thing to do today; the languages have split too far apart. I learned C++ using that style, and all it has accomplished is bad habits that I still struggle to break. Its bad professionally: no shop is going to tolerate such code. Its good because you can do a lot with a little for small home projects if you are just coding as a hobby or something -- you get performance in a smaller, easier to learn package. Though, if you don't get pointers, it may not be easier to learn ... its critical path for C or hybrid C.
Last edited on
Topic archived. No new replies allowed.