Hello World!
(struct sockaddr *)&my_addr
What does this line do?... I am still trying to learn C :)
Printable View
Hello World!
(struct sockaddr *)&my_addr
What does this line do?... I am still trying to learn C :)
It casts the address of my_addr to type struct sockaddr *. By itself, it's useless.
It's for network programming. If you're interested in learning more, I suggest you read Beej's network tutorial. http://beej.us/guide/bgnet/
It's rather advanced stuff, though. Don't try it until you have some experience with C. :)
No, you're wrong.
(struct sockaddr*)&my_addr
Green = Tells the compiler it's a cast
Red = Tells the compiler what you're casting to
Blue = Address of operator: takes the address of a variable
Orange = Tells the compiler what you want to take the address of
The cast converts the expression on the right to the type specified.
Thx alot! Elysia
A bit of an advanced concept for just starting out learning C. If you are interested in network programming. "Unix Network Programming - The sockets network API" is a good read.
The thing is that your struct is just a block of raw memory in the eyes of the CPU or the computer.
The compiler just manages that memory for you.
And because memory is "raw," there are actually no "types". Your struct is just a piece of memory. The compiler keeps track of its type.
But you can tell the compiler that it's another type than it is. It works because it's just a block of memory.
Again, because it's a pointer.
It simply tells the compiler to treat the data at the address in the pointer as something else.
There's a difference:
Code:float f = 1.0f;
int n = (int)f; /* Converts the data inside f to an integer - result is 1. */
int* pN = (int*)&f; /* Tells the compiler to treat the data at the address where f resides as
an int. The result will not be 1, but something much else. */
Look, in all honesty, you're asking questions for things that require way more knowledge than you have at the moment and will likely have for some time. You need to buckle down and get a book or a tutorial and start from the basics and work your way up.
Actually, I think the answer the OP was look for is this:
Given this:
n contains 1, but p doesn't contain 1. p contains the address of the variable n in memory. This might be 0x345dee4 (memory addresses are typically written in hexadecimal), or whatever. It might change every time you run your program.Code:int n = 1;
int *p = &n;
Regardless, you shouldn't care about the exact value that p holds -- you know that it "points" to n, and that's good enough.
*p, on the other hand, tells the compiler to look at p and find the address it holds; and then look at the memory at that address. In effect, it's the same thing as looking at n directly. And thus, *p == 1.
Output of one run on my system:Code:#include <stdio.h>
int main() {
int n = 1;
int *p = &n;
printf("n: %d\n", n);
printf("p: %p\n", p);
printf("*p: %d\n", *p);
return 0;
}
Code:n: 1
p: 0x7fff6017f514
*p: 1
Doesn't (int *)&n (assuming n is an int) supposed to return a pointer to a pointer (**)?
I mean, you are casting the address of n into an int pointer - or what exactly happens if I cast something into a pointer?
Casting doesn't do anything most of the time (except in some circumstances). All it does is tell the compiler you know that you're treating an object of one type as an object of another one, when in fact you know such treatment is considered breaking some safety rules, but you want to do it anyway.
&n is float*, the address of a float variable, not a pointer to pointer.
Consequently, float* is not int*, therefore I use a cast to turn that float* into int*.
Casting turns the type on the right side into the type on the left side. It does not take the address.
Look at this:
The output is:Code:#include <stdio.h>
int main()
{
float f = 1.0f;
int* n;
memcpy(&n, &f, sizeof(int*));
printf("%p\n", n);
n = (int*)&f;
printf("%p\n", n);
printf("%i\n", *n);
printf("%f\n", *(float*)n);
return 0;
}
3F800000
0012FF60
1065353216
1.000000
Why are you passing &n and not n? &n is a pointer to a pointer, isn't it?
---
So what's up with (int *)&f?
If you're referring to...
memcpy(&n, &f, sizeof(int*));
...then what you don't seem to understand is that a pointer is a variable, and by taking its address, I can put a value into it.
The memcpy is the same as...
int* n = (int*)f;
...although the compiler will not allow it.
And give yourself a break and go back to the books. We've already explained the last one.
I'm telling you, I want to copy the data into the variable itself, and not what it points to.