Ask Your Question
1

What is the reason for being able to set a value by dereferencing a pointer to an int, but unable to do the same for a struct?

asked 2023-01-20 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-11-22 06:00:00 +0000

lakamha gravatar image

The reason for this is that a pointer to an int is a simple data type, while a struct is a more complex data type. When we dereference a pointer to an int, we get a reference to the underlying int value, which we can then modify or assign a new value to. However, when we dereference a pointer to a struct, we get a reference to the entire struct object, which is a more complex data type consisting of multiple values. Modifying or assigning a new value to a struct requires accessing or modifying its individual members, which cannot be accomplished simply by dereferencing a pointer. Instead, we must access and modify the members of the struct using the dot or arrow operators.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-01-20 11:00:00 +0000

Seen: 9 times

Last updated: Nov 22 '21