I have a question about initializing the pointer variable of the C language structure.

Asked 2 years ago, Updated 2 years ago, 130 views

Hello.

There are source codes from 1 to 3 as shown below.

Which is the better way to initialize, number 1 or number 2?

And if you initialize it as duplicate(?) like number 3, there is no error in the build, is it the wrong code on the source code?

1. Initialize after declaring structure pointer

int main
{
struct Person p1;
struct Person *ptr;
ptr = &p1;
}

2. Initialize at the same time as the structure pointer declaration.

int main
{
struct Person p1;
struct Person *ptr =  &p1;
}

3. Initialize redundancy(?)

int main
{
struct Person p1;
struct Person *ptr =  &p1;
ptr = &p1;
}

Thank you.

struct pointer

2022-09-22 18:44

1 Answers

Initialization in the second way is good for readability and performance. Number 3 doesn't have a problem with actual movements, but it seems to be a code that doesn't have to be used like that.


2022-09-22 18:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.