1

I have to call a method with the following signature:

int sendTo(const void* buffer, int length, const SocketAddress& address, int flags=0);

  • My first question is:

    What does const void* buffer exactly mean? My intention is: it means that it is a constant (unmodifiable) pointer which can point to anything. ist this somehow right?

  • Second question:

The purpose of this method is, obviously, to send data over a socket. The first parameter is the data, the second is the length of that data. If I want to pass the string "hello" as the first parameter, how would I do this?

My idea:

char hello_str[1024] = "hello"
socket.sendTo(hello_str, sizeof(hello_str),.....);

would this work? But this way I have a way too big char array.
How can I create the array with the right size?

4
  • 4
    I think that this type of things are C, not C++ Commented Aug 8, 2013 at 7:01
  • 1
    @Manu343726 Retagged. Commented Aug 8, 2013 at 7:16
  • 1
    maybe, cdecl.org will hhelp you... Commented Aug 8, 2013 at 7:21
  • 1
    @JimBalter Well, yeah, didn't notice the reference. Let's say that this is not C++-style code. This is C raped and/or pretending to be C++. Commented Aug 8, 2013 at 8:40

5 Answers 5

5

Recall that const protect its left side unless there's nothing to it's left, then it protects its right side. Applying this to your code we'll get that const is protecting void* buffer. That means that the value it points to cannot be modified:

enter image description here

The first is a pointer to a const variable - The pointer can be changed.
The second is a const pointer to a variable - The value can be changed.

This will work, you can easily try it.
And as others already answered, creating it with the right size is done simply by:

char hello_str[] = "hello";

Sign up to request clarification or add additional context in comments.

4 Comments

intersecting pics! Where did you find it?
@GrijeshChauhan I seached "lock" on google and made it with paint :)
downvoter, please let me know what I did wrong so I can learn from my mistakes. Thanks.
Flag your downvoted answers, I notice your answer intentionally down-voted (I think from same user). Flag and write in message "I guess down-voted by same users unnecessary" flag all.
5

it means that it is a constant (unmodifiable) pointer which can point to anything

No, that would be void *const. It's rather a pointer-to-anything, where the pointee (the "anything" itself) can't be modified.

would this work?

yes, apart from the missing semi-colon.

How can i create the array with the right size?

char hello_str[] = "hello";

or even

const char hello_str[] = "hello";

5 Comments

can you briefly explain how this would work with an string returned by an object? e.g.: char hello_str[] = obj.toString() gives me initializer fails to determine size of 'hello_str'
@user1291235 because the initializer of an array of char should be an array of char, not a pointer. Arrays aren't pointers, and they can't be used interchangeably.
@user1291235 This is very often a misunderstanding in C and C++. C style arrays decay to pointers when forced to, but have very different behavior - sizeof being a good example of that.
Might want to use sizeof(hello_str) - 1 for length, if you don't want to send the null character. Or (maybe better) strlen(hello_str).
@anatolyg Yes, in that case strlen() is probably even better.
1

First question: H2C03 is right, you should make the type be void * const to prevent the pointer from being modified.

Second question: You have some options here, depending on exactly what you are doing. Here are two examples that would work:

char hello_str[] = "hello"
socket.sendTo(hello_str, sizeof(hello_str)-1,...);
socket.sendTo(hello_str, strlen(hello_str),...);

In the first call to sendto, we are calculating the size of the string at compile time. We subtract 1 in order to avoid sending the null termination character at the end of the string. In the second case, we are computing it at runtime by calling the standard strlen function which is available in C and C++.

2 Comments

@DavidGaryson First answer: "no, you are not right." - What you (and OP) are talking about is void *const, not const void *.
There's not point in making the pointer unmodifiable. const void* (or void const*) is right, the OP is just confused about what it means.
1

What does const void* buffer exactly mean?

It's an untyped pointer (that's another way of saying that it can point to anything). The value it points to cannot be modified (that's not entirely correct, but at least that is what you should think when you see that). The pointer itself can change however:

const void * buffer = &a;
buffer = &b; // this is valid!

Besides that, your function call is completely correct.

Comments

1

You can do :

const char hello_str[] = "hello"; // Don't forget the const
socket.sendTo(hello_str, sizeof(hello_str)-1,.....);
// or socket.sendTo(hello_str, strlen(hello_str),.....);

To answer to your questions :

What does const void* buffer exactly mean? My intention is: it means that it is a constant (unmodifiable) pointer which can point to anything. ist this somehow right?

No, That would be void* const. const void* means that it is the pointee that cannot be modified.

You may read this.

3 Comments

@neutrino It will!
@neutrino Exactly as expected. Pierre didn't write const char *hello_str but const char hello_str[].
sizeof and strlen give different results (sizeof = strlen + 1); only one is correct (probably strlen).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.