C Pointer Question: Is It the Same?

  • Thread starter Thread starter James889
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on the correct usage of pointers in C, specifically regarding the syntax for accessing the `sin_addr` field of a `struct sockaddr_in`. The first code snippet, `return &(((struct sockaddr_in*)sa)->sin_addr);`, is deemed incorrect due to improper dereferencing of the pointer `sa`. The correct approach involves ensuring proper parentheses placement to avoid syntax errors, leading to the valid expression `&(*((struct sockaddr_in*)sa)).sin_addr;`. The conversation highlights the importance of clarity in pointer manipulation to enhance code readability.

PREREQUISITES
  • Understanding of C programming language syntax
  • Familiarity with pointers and memory management in C
  • Knowledge of socket programming and the `struct sockaddr_in` structure
  • Experience with type casting in C
NEXT STEPS
  • Study C pointer arithmetic and dereferencing techniques
  • Learn about the `struct sockaddr` and `struct sockaddr_in` in socket programming
  • Explore best practices for writing readable C code
  • Investigate event-driven programming patterns in C
USEFUL FOR

Software developers, particularly those working with C and socket programming, as well as anyone looking to improve their understanding of pointer manipulation and code readability in C.

James889
Messages
190
Reaction score
1
Hello,

I have a question regarding pointers
Assume you have the following:

Code:
return &(((struct sockaddr_in*)sa)->sin_addr);

and
Code:
return &(struct sockaddr_in *)(sa->sin_addr);

Is this the same as writing &((struct sockaddr *)*sa).sin_addr)
and &(struct sockaddr *)(*sa.sin_addr)
?
 
Physics news on Phys.org
Play around and find out. seriously.

The top piece of code I do not believe is correct. sa I do not think is a socketaddr_in struct.. but it has been a while since i have done sockets.

But one is wrong, either the top or bottom, and I believe the top is wrong.

Now concerning your question, yes an no. you have some syntax problems. The first would work if you moved the deference away from sa.. actually both require it. you have to add another set of a parens .

&(*((struct sockaddr_in*)sa)).sin_addr; that should work.

and the bottom requires the deference for sa outside of the parens and that should work.

The issue is that doing it with void pointers or any data that has to be cast like that is it makes the code hard to read.

C is can be hard enough when you are reading poorly written and document code. there is no reason to make it worse.

No the valid use of that is if you were building an event driven system and built a matrix of event or state functions. And you wanted to call them on the fly. Or if you had a matrix of complex data.

That is about how C++ virtual tables work.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
10K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
7K