| |

VerySource

 Forgot password?
 Register
Search
View: 1471|Reply: 10

Does standard C have a reference type

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-7 13:30:01
| Show all posts |Read mode
I have a function void f (int & a) {
 a + = 1;

}
I want to increase the value of a by 1 in f, and does not need to refer to * a as the parameter, can I use the method? Compilation failed. what else can we do?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-4-4 09:15:01
| Show all posts
There is a problem with the definition of this function. The address character cannot be used as a formal parameter.&is an operator, not a variable type. There are two methods:
1) Use pointers as parameters
void f (int * a) {
 (* a) + = 1;
}
2) Rely on the return value
int f (int a) {
 a + = 1;
return a;
}
Reply

Use magic Report

0

Threads

10

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-5-12 08:45:01
| Show all posts
Obviously not sprinkled
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Invalid IP Address

Post time: 2020-8-16 22:45:01
| Show all posts
Pointers can be used
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-17 00:00:01
| Show all posts
No . References are actually safe pointers in a sense
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-17 01:00:01
| Show all posts
References in C can only use pointers,&is something in C++
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-17 12:00:01
| Show all posts
The title of the master answered the question
Reply

Use magic Report

1

Threads

39

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-8-17 12:45:01
| Show all posts
void f(int a){
*(int*)a +=1;

}

c practice
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-17 22:00:01
| Show all posts
No references in C
References are the stuff of C++

All&in C means address or operator.
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 United States

Post time: 2020-8-18 19:15:01
| Show all posts
References are accessed through pointers, but the compiler does&and * operations for you. In C, you cannot achieve your purpose without *a as a parameter.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list