0

What are void type variable in C? I have rough idea but not sure how i can use them for below scenario.

server/ client program

I have a struct array which contains hostname, address in server. I want to send it to the client over the socket. How i can achieve it?

struct ipinfo{

  char hostname[64];
  char address[64];
}
struct ipinfo allip[5];

I read some where that i can copy into specific memory location as void type variable and them send the variable? Can any one please explain this concept? I really appreciate it.

sean
  • 146
  • 2
  • 9
  • 1
    `void` is not a complete type. You cannot have variables of type `void`. – Kerrek SB Sep 29 '13 at 23:20
  • 1
    The `void` type either applies to a function return if the function does not return anything, to a function's parameter list if it has no parameters, or it applies to a pointer variable if the type that the pointer points to is unknown. What API call are you looking at to send your data? That should indicate what you need to do. – lurker Sep 29 '13 at 23:20
  • I am going to store struct into void pointer, then send(fd, void * stored, sizeof(struct),0) then at client recv(fd, void*, sizeof (struct),0) and further assign the void variable to struct. Assumption is both client server has struct declared in them – sean Sep 29 '13 at 23:28

3 Answers3

1

In C the only time void can be used as a variable type is if it's a pointer. They are handy for when you aren't sure what type of data you have coming.

void * somePointer;

This can be used for various things.

Referencing an object without knowing the type.

Handling plain memory without a type. Malloc (and I believe new in C++) returns a void pointer as at the moment the memory is without a type.

Try not to use void pointers though, they are generally a good idea to stay away from. Likely to cause errors and headaches. You can often times find a better solution.

The void keyword can also be used in front a function.

void printHello(void)
{
   printf("Hello");
}

In this function we use void because it's not returning anything. Void functions can simply do whatever task we assign them without returning anything. We also don't need to pass any data into the function, so we specify void in the parameters.

Note: If you're ever learning C++, there's something you really need to keep in mind about function parameters.

void printHello() // <- This is bad in C, it will take any number of anything practically
{
  printf("Hello");
}

Always put void in the parameters if you want no arguments passed in for C.

void printHello() // <- Good in C++, it won't allow any arguments on a call
{
   std::cout << "Hello";
}

You cannot however use void as a variable type as in

void a = 0;
void b = 's';
void c = 5.5
// You can't use void to store anything
Austin J.
  • 138
  • 6
0

void pointers are mainly used in function argument when you expect that argument to be of any type because you can cast any type to void then back to any type without loss of data , the only thing you can't do with a void pointer is to dereference it.

Farouq Jouti
  • 1,657
  • 9
  • 15
0

I don't think void means what you hope it means. void is used in C-like languages to indicate an unknown type, or no type. For example, void* is a void pointer. It's a memory address that has some data at it, but the format of the data (and even its size) is not specified. In order to use that data you need to assign some type to it, usually through an assignment or and explicit or implicit cast.

Here's an example:

void * memory = malloc(16);
memory[0] = 0; // this won't compile!
int * int_array = memory;
int_array[0] = 0; // this is ok because we know the type

void is also used to indicate that a function doesn't have a return value.

Here's an example:

void exit(int status);

void can also be used as an indication that you're intentionally discarding the return value of a function call. This can improve the readability of your program, or suppress some compiler diagnostics.

Here's an example:

(void)memset(memory, 0, 16); // why does memset have a return value??

Another use of void is to indicate an empty parameter list. In C, a function declared like main() has an unspecified parameter list, not an empty list.

Here's an example:

int main(void) {
    ...
}

I'm afraid that none of these application are likely to help you solve your socket problem.

pburka
  • 1,434
  • 9
  • 12
  • How will you approach this problem of sending the struct data from server client? any Idea... Thanks in advance – sean Sep 29 '13 at 23:33
  • @sean I would recommend using something like Google Protocol Buffers to marshall and unmarshall your data. But your question is really more of a sockets question than a C or void question. Perhaps you should post a new question explaining what you're actually trying to accomplish. – pburka Sep 29 '13 at 23:37