C Pointer Question: Is It the Same?

  • Thread starter James889
  • Start date
In summary, the conversation discusses the use of pointers in C programming. It raises the question of whether the two code snippets provided are equivalent and if they are both correct. After some experimentation, it is determined that both codes require some syntax adjustments to work properly. The conversation also touches on the readability of code when using pointers and the valid use of pointers in event-driven systems or with complex data.
  • #1
James889
192
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
  • #2
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.
 

FAQ: C Pointer Question: Is It the Same?

What is a C pointer?

A C pointer is a variable that stores the memory address of another variable. It allows for indirect referencing and manipulation of data in computer memory.

How do I declare and initialize a C pointer?

To declare a C pointer, use the asterisk symbol (*) before the variable name. To initialize a pointer, assign it the memory address of a variable using the ampersand symbol (&).

Is a C pointer the same as a regular variable?

No, a C pointer is not the same as a regular variable. While a regular variable stores a value, a pointer stores a memory address.

Can a C pointer point to any data type?

Yes, a C pointer can point to any data type, including int, float, char, and even other pointers.

How do I use a C pointer to access and modify data?

To access data using a C pointer, use the dereference operator (*) before the pointer variable name. To modify data, assign a new value to the dereferenced pointer.

Similar threads

Replies
1
Views
9K
Replies
2
Views
1K
Replies
2
Views
2K
Replies
4
Views
2K
Replies
12
Views
9K
Replies
8
Views
2K
Replies
3
Views
2K
Replies
2
Views
3K
Replies
13
Views
7K
Back
Top