Thread: char array question

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    12

    char array question

    I have the following code which shows roughly what i am trying to do, I want to write error messages to the file using the write system call. I am having trouble assigning error messages to the buffer array as it shows errors, how do I make the code compile without errors?

    Code:
    #include <stdio.h>
    
    int main()
    {
            char buffer[512];
            int x = 1, y = 2;
    
            if (x == 1)
            {
                    buffer = 'hello';
                    printf("%s", buffer);
            }
            else
            {
                    buffer = "bye";
                    printf("%s", buffer);
            }
    
            if (y == 2)
            {
                    buffer = "hello2";
                    printf("%s", buffer);
            }
            else
            {
                    buffer = "bye2";
                    printf("%s", buffer);
            }
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. You must use "double" quotes, not 'single' quotes
    2. You can't assign to an array, you need something like
    strcpy( buffer, "bye" );
    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.

  3. #3
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Check out a tutorial on how to use C strings. Might I suggest CProgramming.com's?
    Code:
    void function(void)
     {
      function();
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File input question
    By Beast() in forum C Programming
    Replies: 16
    Last Post: 07-09-2004, 03:23 PM
  2. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  3. Replies: 5
    Last Post: 05-30-2003, 12:46 AM
  4. help with array of char and char **
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-20-2002, 02:23 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM