passing pointer to a structure in a function

This is a discussion on passing pointer to a structure in a function within the C Programming forums, part of the General Programming Boards category; Hello, I am new to socket programming and haven't done C for a long time. So I am practicing writing ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    passing pointer to a structure in a function

    Hello,

    I am new to socket programming and haven't done C for a long time.

    So I am practicing writing code that would simulate want a socket program would do. however, I have error with my code below.

    Can anyone advise me on what I am doing wrong.

    Many thanks for your advice,

    Steve

    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    
    struct in_addr
    {
      unsigned long s_addr;
    };
    
    struct sockaddr_in
    {
       short int sin_family;
       unsigned short sin_port;
       struct in_addr sin_addr;
       char sin_zero[8];
    };
    
    int bind(int sockfd, struct sockaddr *my_addr, int length);
    
    int main(int argc, char *argv[])
    {
       printf("Socket Programming example!\n");
    
       struct sockaddr_in sin;
       sin.sin_family = 123;
       sin.sin_port = 7000;
       sin.sin_addr.s_addr = 1921;
    
       printf("sin_family: %d\n", sin.sin_family);
       printf("sin_port: %d\n", sin.sin_port);
       printf("sin_addr: %lu\n", sin.sin_addr.s_addr);
    
       
       
       return EXIT_SUCCESS;
    }
    
    int bind(int sockfd, struct sockaddr *my_addr, int length)
    {
       printf("\nsockfd: %d\n", sockfd);
       printf("sin_family: %d\n", my_addr.sin_family);
       printf("sin_port: %d\n", my_addr.sin_port);
       printf("sin_address: %lu\n", my_addr.sin_addr.s_addr);
    
       int result = bind(100, (struct sockaddr*) &sin, 29);
    
       return 1;
    }
    Errors:
    /home/steve/Cprogramming/structures/socketAddress/src/socketaddress.c:23: warning: ‘struct sockaddr’ declared inside parameter list
    /home/steve/Cprogramming/structures/socketAddress/src/socketaddress.c:23: warning: its scope is only this definition or declaration, which is probably not what you want
    /home/steve/Cprogramming/structures/socketAddress/src/socketaddress.c:43: warning: ‘struct sockaddr’ declared inside parameter list
    /home/steve/Cprogramming/structures/socketAddress/src/socketaddress.c:44: error: conflicting types for ‘bind’
    /home/steve/Cprogramming/structures/socketAddress/src/socketaddress.c:23: error: previous declaration of ‘bind’ was here
    /home/steve/Cprogramming/structures/socketAddress/src/socketaddress.c: In function ‘bind’:
    /home/steve/Cprogramming/structures/socketAddress/src/socketaddress.c:46: error: request for member ‘sin_family’ in something not a structure or union
    /home/steve/Cprogramming/structures/socketAddress/src/socketaddress.c:47: error: request for member ‘sin_port’ in something not a structure or union
    /home/steve/Cprogramming/structures/socketAddress/src/socketaddress.c:48: error: request for member ‘sin_addr’ in something not a structure or union
    /home/steve/Cprogramming/structures/socketAddress/src/socketaddress.c:50: error: ‘sin’ un

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since it says that struct sockaddr is not declared, I'm going to guess that the problem is that you're not declaring struct sockaddr (you have a struct sockaddr_in).

    Is there a reason you're not #including sys/socket.h or winsock2.h (or whatever is appropriate on your system)?

  3. #3
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Deleted
    Last edited by steve1_rm; 02-03-2008 at 01:37 AM. Reason: Solved the problem after posting.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    These error messages are telling you that the thing on the left of my_addr.sin_family (et cetera) is not a structure or a union. Looking at the declaration, we see that, indeed, my_addr is not a structure or a union, but a pointer to a sockaddr_in. If we want to use the structure that my_addr points to, we must dereference it, either using (*my_addr).sin_family or using the arrow notation my_addr->sin_family.

  5. #5
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    Thanks for the reply.

    The reason i haven't included the header files, as I am practicing using the structure and passing them using pointers and references. So this is just some programming practice for me. And as I am learning socket programming, thought I would kill 2 birds with one stone.

    I have corrected what you said in your last post.

    Just one question about references

    Code:
    bind(123, (struct sockaddr_in*) &sin, 10);
    The & would mean the 'address of'. So get the address of sin and cast it to a pointer.

    I know how it works, but don't understand why? Any easy explaining would be most helpful.

    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    
    struct in_addr
    {
      unsigned long s_addr;
    };
    
    struct sockaddr_in
    {
       short int sin_family;
       unsigned short sin_port;
       struct in_addr sin_addr;
       char sin_zero[8];
    };
    
    int bind(int sockfd, struct sockaddr_in *my_addr, int length);
    
    int main(int argc, char *argv[])
    {
       printf("Socket Programming example!\n");
    
       struct sockaddr_in sin;
       sin.sin_family = 123;
       sin.sin_port = 7000;
       sin.sin_addr.s_addr = 1921;
    
       printf("sin_family: &#37;d\n", sin.sin_family);
       printf("sin_port: %d\n", sin.sin_port);
       printf("sin_addr: %lu\n", sin.sin_addr.s_addr);
    
       int result = bind(123, (struct sockaddr_in*) &sin, 10);
       
       return EXIT_SUCCESS;
    }
    
    int bind(int sockfd, struct sockaddr_in *my_addr, int length)
    {
       printf("\nsockfd: %d\n", sockfd);
       printf("sin_family: %d\n", my_addr->sin_family);
       printf("sin_port: %d\n", my_addr->sin_port);
       printf("nsin_address: %lu\n", my_addr->sin_addr.s_addr);
    
       return 1;
    }
    
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by steve1_rm View Post
    Just one question about references

    Code:
    bind(123, (struct sockaddr_in*) &sin, 10);
    The & would mean the 'address of'. So get the address of sin and cast it to a pointer.

    I know how it works, but don't understand why? Any easy explaining would be most helpful.
    There's very little to understand here: a pointer is an address -- that's the point of having pointers, is that they store memory addresses. So since your function expects/needs to have the address of the socket, you have to give it the address of the socket.

    The point of the cast, in your original code way back when, is that sockaddr_in and sockaddr are different types (that are carefully designed to have the same size and corresponding things in the same place); so here the cast served to force C to reinterpret your sockaddr_in address as a sockaddr address. (As it stands now, the cast is useless.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Passing pointer into function bug
    By CodeMonkey in forum C++ Programming
    Replies: 4
    Last Post: 04-26-2005, 11:13 PM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21