Char strings, pointers and substrings.

L

Lawrie

Hi,

I have checked the FAQ and can't find an answer to something that has
been bugging me for a while.......


I am using the socket.h write function in non blocking mode

char* message = "blah blah blah";
int bytes_sent
int message_length = strlen(message);

bytes_sent = write(socket_id, message, message_length);

If bytes_sent is greater than 0 then some of my message has been sent.
If bytes_sent < message_length then some of my message remains unsent
so I need resend the rest.

Here is my question:

I know that my char pointer is equivalent to the first element of a \0
terminated character array.

If I then set the pointer to point to the first unsent char in the
array can I then pass this as the remining unsent part of the string to
the write function (in effect a ponter to a substring containing the n
last chars in the original string)?

Example

char* message = "Mary had a little lamb";

write function returns 5 bytes sent.

Can I then call write again with *(message + 5) to send the remainder
of the string?

If not is there a better/safer method?

Many thanks

Lawrie
 
?

=?ISO-8859-1?Q?Gerg=F6_Barany?=

Lawrie said:
If I then set the pointer to point to the first unsent char in the
array can I then pass this as the remining unsent part of the string to
the write function (in effect a ponter to a substring containing the n
last chars in the original string)?

Yes, you can do that. Since apparently you also need to pass the message
length to the write function, remember to adjust that as well.
Example

char* message = "Mary had a little lamb";

write function returns 5 bytes sent.

Can I then call write again with *(message + 5) to send the remainder
of the string?

Here you are not passing a pointer to the fifth element but the element
itself, since *(message + 5) is equivalent to message[5]. You want to
pass just message + 5.


Gergo
 
?

=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=

Lawrie said:
I am using the socket.h write function in non blocking mode

char* message = "blah blah blah";

this should produce a warning (the rhs is of type "char const *")
int bytes_sent

this should produce an error (missing semicolon)
int message_length = strlen(message);

this should produce a warning (no prototype for strlen() in scope)
bytes_sent = write(socket_id, message, message_length);

write() is not part of the C language.
If not is there a better/safer method?

Use fwrite(), which is part of the C language. Your system probably
has a function named fdopen() which will return a FILE * that
corresponds to your (non-C) file or socket descriptor.

DES
 
C

Christian Kandeler

Dag-Erling Smørgrav said:
Lawrence Kirby said:
char* message = "blah blah blah";
this should produce a warning (the rhs is of type "char const *")
The RHS has type char [15]. Note that it is not const.

When did string literals become writable?

They aren't. Yet they have type char[], not const char[]. That's the beauty
of C.


Christian
 
C

CBFalconer

Dag-Erling Smørgrav said:
The RHS has type char [15]. Note that it is not const.

When did string literals become writable?

They aren't. That doesn't make them const in C-speak, because of
backwards compatibility. However if you declare them as const char
* attempts to write into them will produce a warning. With gcc you
can use -Wwrite-strings to automatically treat all such string
literals as const.
 
K

Keith Thompson

this should produce a warning (the rhs is of type "char const *")

No, as others have pointed out.
this should produce an error (missing semicolon)


this should produce a warning (no prototype for strlen() in scope)

The code is obviously an incomplete fragment. You might as well
assume that the declaration is outside any function, and therefore
illegal because the initializer isn't constant. (Yes, it's better to
post a small complete compilable program if possible.)
write() is not part of the C language.


Use fwrite(), which is part of the C language. Your system probably
has a function named fdopen() which will return a FILE * that
corresponds to your (non-C) file or socket descriptor.

Since the program is using sockets, which are nonstandard, there may
well be a valid reason to use write() rather than fwrite().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top