Turbo C 2.1 and structs

This is a discussion on Turbo C 2.1 and structs within the C Programming forums, part of the General Programming Boards category; I have searched and searched, and found nothing to solve my problem. I am using Turbo C 2.1 to make ...

  1. #1
    VOX
    VOX is offline
    Deleting... VOX's Avatar
    Join Date
    Oct 2004
    Location
    VA
    Posts
    94

    Turbo C 2.1 and structs

    I have searched and searched, and found nothing to solve my problem. I am using Turbo C 2.1 to make a program, but for some reason my structures get wierd errors. Here is an example of what I am doing.

    Code:
    struct charinfo {
        char name[20];
    };
    That compiles fine, but when I start to mess around with the stuff in the structure

    Code:
    charinfo.name = "Hello";
    Or this

    Code:
    gets(charinfo.name);
    I keep getting errors. Am I doing this wrong? Or is it my compiler?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    charinfo.name = "Hello";
    Well here you are trying to assign a pointer to a constant array of characters to an array. This is not allowed. However you can do it at initalization:
    Code:
    struct charinfo bob = { "Bob" };
    Also since charinfo is the name of your struct you can not use it as a variable. This is what is causing the problem with your gets(). As a side note there is NO REASON TO USE gets() EVER!

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    VOX
    VOX is offline
    Deleting... VOX's Avatar
    Join Date
    Oct 2004
    Location
    VA
    Posts
    94
    Ok so I changed it to this.

    Code:
    struct playerinfo {
        char name[20];
    } charinfo;
    So I am not using the name of my struct. (I think)

    Code:
    scanf("%s",&charinfo.name);
    It ceases to work. I get "Illegal structure operation" errors on the lines where I do anything with charinfo.name.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >scanf("%s",&charinfo.name);
    This is no better than gets, and now instead of reading a line you only go to the first whitespace. By the way, name is already a pointer, so the ampersand should be removed.
    My best code is written with the delete key.

  6. #6
    VOX
    VOX is offline
    Deleting... VOX's Avatar
    Join Date
    Oct 2004
    Location
    VA
    Posts
    94
    It works! Thanks guys, now all I have to do is get this fgets to work...

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    fgets(charinfo.name, sizeof charinfo.name, stdin);
    Is it really that difficult?

  8. #8
    VOX
    VOX is offline
    Deleting... VOX's Avatar
    Join Date
    Oct 2004
    Location
    VA
    Posts
    94
    The syntax is not difficult. It's the output that doesn't work right. When I do a printf to show what the user typed in for charinfo.name, it shows what they typed in, then a newline with everything else in the printf.

    With
    Code:
    printf("%s, you have chosen to be a class.\n",charinfo.name);
    What it should be

    Code:
    VOX, you have chosen to be a class.
    What it is

    Code:
    VOX
     , you have chosen to be a class.
    And it only does that with fgets and not scanf.
    Last edited by VOX; 11-28-2004 at 03:28 PM.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    fgets copies the newline from the stream into your string. It's easy to get rid of:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char string[BUFSIZ];
    char *p;
    
    fgets ( string, sizeof string, stdin );
    p = strchr ( string, '\n' );
    if ( p != NULL )
      *p = '\0';
    My best code is written with the delete key.

  10. #10
    VOX
    VOX is offline
    Deleting... VOX's Avatar
    Join Date
    Oct 2004
    Location
    VA
    Posts
    94
    Thanks, and does anyone know how I assign a string of text to a variable in my structure? Say I have
    Code:
    struct playerinfo {
        char name[20];
        int class;
        char classname[20];
    } charinfo;
    How would I put a string of text into the variable charinfo.classname?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    strcpy, strncpy, strcat, strncat, a loop, sprintf... You have several options.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 09:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 03:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 02:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 06:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 01:38 PM

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