Thread: Address of Structure element, and struct malloc

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Address of Structure element, and struct malloc

    Hi there,

    I have 2 questsions:

    1) How can I access the address of an element of a structure?

    I'm trying to do some basic TCP server things, and I have the following struct defined:

    Code:
    struct clients
    {
    	char *name;
    	char ip[IP]; /*12 digits, 3 '.' and a null character, is the max length of an IP*/
    	int new_sd;
    	struct sockaddr_in client;
    	int client_len;
    
    };
    typedef struct clients Clients;
    and then I am trying to do something like this:
    Code:
    int clientCount = 0; /*Keep track of the number of clients connected*/
    Clients *clientTable; /*Memory will be dynamically allocated here, as clients connect*/
    ...
    clientTable[clientCount -1].&client = &temp_client;
    but I get a syntax error before the & token. I have tried various combinations of brackets, including the & at the beginning followed by brackets, etc.., and cannot get this to compile.
    (Note: The -1 is because clientCount is the real number of clients connected. So if 1 client is connected, the actual structure that exists is at clientTable[0])

    2) I have an array of these structures, and when I try to book off each element of the array, ie something like:
    Code:
    clientTable[clientCount -1] = (Clients)malloc(sizeof(Clients));
    I get "error: conversion to non-scalar type requested".

    Do I not have to malloc memory for this structure? Is it considered already malloc'd just by its creation?

    Thanks very much for the help!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by HellsChicken
    and then I am trying to do something like this:
    Code:
    clientTable[clientCount -1].&client = &temp_client;
    but I get a syntax error before the & token.
    Perhaps you mean for the client member to be a pointer. You certainly can't reassign a variable's address.

    Quote Originally Posted by HellsChicken
    2) I have an array of these structures, and when I try to book off each element of the array, ie something like:
    Code:
    clientTable[clientCount -1] = (Clients)malloc(sizeof(Clients));
    I get "error: conversion to non-scalar type requested".
    If using C, just get rid of the cast. Then make sure that you are malloc'ing to a pointer of the correct type.

    Use the preferred form and save yourself headaches:
    FAQ > Explanations of... > Casting malloc
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adreess of pointed structure element
    By limp in forum C Programming
    Replies: 1
    Last Post: 06-12-2009, 05:54 AM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. 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
  5. doubly linked lists
    By cworld in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2002, 09:33 AM