Thread: How do I pass this structure?

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    28

    How do I pass this structure?

    I am relatively new to C structures, and I have to pass the structure (serv_addr) between these functions. How do I do this?

    Basically, I want to pass the 'structure serv_addr' from the 'main function' to the 'myAddr function'. The 'myAddr function' will populate the 'structure serv_addr'. Then the 'myListen function' starts the connection using the 'structure serv_addr'.

    How do I do this?

    Thanks.

    My current code is below.

    Code:
    // Includes
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <arpa/inet.h>
     
    // TCP/IPv6
    // Function declarations
    int myListen(struct sockaddr *serv_addr);
    int myAddr(struct sockaddr *serv_addr);
    // MAIN
    int main(void) {
    	// Init Vars
    	struct sockaddr_in6 serv_addr;
    	int myAddrReturn;
    	int myListenReturn;
    	
    	myAddrReturn = myAddr(&serv_addr); // Populate structure
    	myListenReturn = myListen(&serv_addr); // Start TCP/IPv6 connection
    	
    	// Print return values
    	printf("addrReturn: %d", myAddrReturn);
    	printf("listenReturn: %d", myListenReturn);
    }
    // Populate structure function
    int myAddr(struct sockaddr *serv_addr) {
    	int portno = 3344;
    	
    	// Populate Addr
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin6_flowinfo = 0;
        serv_addr.sin6_family = AF_INET6; // AF_INET6 = 10;
        serv_addr.sin6_addr = in6addr_any;
        serv_addr.sin6_port = htons(portno); // Convert the port number to network byte order
        
        return(0); // Return on success
    }
    // Start TCP/IPv6 connection
    int myListen(struct sockaddr *serv_addr) {
    	// Declare Vars
        int sockfd, tempVar;
        // Sockets Layer Call: socket()
        sockfd = socket(AF_INET6, SOCK_STREAM, 0);
        // socket() Error Catch
        if (sockfd < 0) {
        	return(1);
        }
        // Sockets Layer Call: bind()
        tempVar = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
        // Bind() Error Catch
        if (tempVar < 0) {
        	return(2);
        }
        
        tempVar = listen(sockfd, 5);
        return(55);
    }
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > bzero((char *) &serv_addr, sizeof(serv_addr));
    > serv_addr.sin6_flowinfo = 0;
    Would become
    bzero((char *)serv_addr, sizeof(*serv_addr));
    serv_addr->sin6_flowinfo = 0;


    All other members also use -> as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass a structure to a function
    By zolfaghar in forum C Programming
    Replies: 2
    Last Post: 05-10-2016, 09:31 AM
  2. structure pass by reference
    By Yifangt in forum C Programming
    Replies: 17
    Last Post: 04-20-2016, 09:53 AM
  3. Pass structure by reference(?)
    By azerej in forum C Programming
    Replies: 4
    Last Post: 05-22-2008, 02:10 AM
  4. pass structure to function
    By tat in forum C Programming
    Replies: 18
    Last Post: 12-06-2007, 08:01 PM
  5. Structure pass-by-reference - help?
    By dxfoo in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 10:16 PM

Tags for this Thread