Thread: Char array and string

  1. #1
    Registered User
    Join Date
    Nov 2008

    Char array and string

    I'm using the follow code in a socket connection function:
    char input[100];
    ssize_t n;
    while((n = read(sockfd, input, sizeof input)) > 0) {
        input[n] = '\0';
    How can I make "input" a dynamic length array? Can't get anything to work.
    Thanks for the help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    You can't pass a "variable length" array to read(), and expect it to expand automatically to whatever size the input data is.

    You have to read the input in blocks of known size. What you do after that is up to you.

    Oh, and you need to subtract 1 from the size you pass to read(), so that you have room to append a \0.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initialization of char array (c style string)
    By manzoor in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2008, 06:29 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM