assigning strings

This is a discussion on assigning strings within the C Programming forums, part of the General Programming Boards category; Hi, I've just started writing a small program, however I've experienced a problem (this is a tiny piece of the ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    16

    assigning strings

    Hi, I've just started writing a small program, however I've experienced a problem (this is a tiny piece of the code):

    Code:
    while ((symbol=getch())!='a')
    printf("ERROR");
    printf("%c",symbol);
    
    array[i].field=symbol; /* file structure=array of records, where "field" is defined as char field[10]*/
    I get the "Lvalue required" at the last line - what's that supposed to mean in this situation? How can I assign a string (or a char) to a string.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't assign into an array, only into individual slots of an array. Since symbol is a character, you need to specify which of the field[0] through field[9] slots that character should go into.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Code:
    while ((symbol=getch())!='a')
    printf("ERROR");
    printf("%c",symbol);
    
    [array[i].field=symbol; /* file structure=array of records, where "field" is defined as char
    shldnt it be getchar(). instead of getch

    btw you cannot assign

    Code:
    array[i].field=symbol; /* file structure=array of records, where "field" is defined as char
    it may be good in java but you need to directly store it into the array

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    So it means that I can not directly assign a string to the "field". Right, but now another thing - do I have to use a loop or something like that to assign a whole string to this field? Because I want to write something like this:

    if (symbol=='a')
    array[i].field[0]='option 1';

    and obviously it gives me an error that "Character constant must be 1 or 2 characters long" - does it mean that the only way to do that, for example, array[0].field = 'option 1' is to create a loop and assigning the whole string character by character like array[0].field[0]='o', array[0].field[1]='p' ... Sound silly - there must be a better way?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    A string is an array of several characters. So "option 1" (note the double quotes) is a string. Strings are represented with double quotes.
    A string consists of several characters. So "option 1" would contain 'o', 'p', etc.
    Since field is an array of characters, you cannot assign n characters to 1 character, naturally.
    So you want to assign many characters to an array of many characters. That would be assignment, ie: array[i].field = "option 1";
    Unfortunately, C does not allow this. However, there is another way. For string, you can use strcpy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Assigning variables to strings
    By FlyingIsFun1217 in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2006, 06:39 PM
  4. assigning strings
    By abs320 in forum C Programming
    Replies: 3
    Last Post: 09-01-2004, 05:33 AM
  5. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM

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