char array question

This is a discussion on char array question within the C Programming forums, part of the General Programming Boards category; I have the following code which shows roughly what i am trying to do, I want to write error messages ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,746
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 04:23 PM
  2. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 09:21 AM
  3. Replies: 5
    Last Post: 05-30-2003, 01:46 AM
  4. help with array of char and char **
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-20-2002, 03:23 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 12:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21