Re: Help With Arrays




"Craig" <NOSPAMcarvinabuser@xxxxxxxxxxxxxx> wrote
> Hello friends at comp.lang.c,
>
> I'm reading from a socket descriptor and copying the data to a buffer
> using:
>
> char buffer[MAXVALUE];
> .
> .
> nread = read(newSd, line, MAXVALUE);
>
> The string to be copied to the buffer is of varying size, so what I'd like
> to know is if there's a way to copy the data, without having to specify a
> max value, making the size of the string the same size as the array.
>
(IFYM size of the array same as size of the string)
As you probably know, a C string is a NUL-terminarted list of characters.
In C, an array must have a size known at runtime. If the string is smaller
than the array, you have no problem (the NUL gives the length). if it is
larger, you will overflow.

The way round this is to use malloc(). When you know the size of the string,
call malloc() with the length plus one for the NUL. This means that "buffer"
must be a pointer rather than an array. You cna also use realloc() to grow
and shrink the buffer as required.


.



Relevant Pages

  • Re: Data type byte
    ... attempt to change the input buffer to a string (although not by changing the ... With multi dimensional array that can hold ... Also the receiving ... dimensional Byte Array is just one long string of memory. ...
    (microsoft.public.vb.general.discussion)
  • Re: String parsing in VB.net
    ... refactor your code so that myDataBuffer is an array of Byte, ... than a string. ... receives a data buffer. ... encoding - it is only defined for 0-127. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: C - malloc and user input
    ... Also I am entering a string or fgets). ... Don't cast the result of malloc; ... where a line is terminated by a '\n' character. ... characters you're willing to store in your buffer). ...
    (comp.lang.c)
  • Re: from array of ints to string and vice versa
    ... I want to convert this string to an array, ... other to generate it, separating them with ", ". ... takes a buffer and returns a size, which you first call with a NULL pointer ...
    (comp.programming)
  • Re: pls help: very odd behaviour when concatenating strings
    ... More then likely you are having a problem because your byte array is ... array to a string. ... // Put some stuff in the buffer. ... > concatenate them some values are missing (I canīt see them in the debugger). ...
    (microsoft.public.dotnet.languages.csharp)