Thread: Simple Basic Struct Prob.

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    12

    Question Simple Basic Struct Prob.

    Code:
    Good Day All,
    
    I'm currently learning C from a book and unfortunately, is stuck rather embarassingly at struct. I tried the seach for structure array but the problems posted were all (hopefully only at the moment) beyond my league. 
    
    In the code below, I am unable to initialise char in struct nor outputting it onto the screen. What I receive however, are funny numbers (for char !!!) that seem more like the memory's address to me. 
    
    Ps. I spent the last 5 hours trying to figure out what's wrong and would really appreciate any help. I'm using visual c++ on 98se.
    
    Many Tks n regards
    
    Code:
    =======================================
    #include <stdio.h>
    
    typedef struct airline{
        int id;
        int assign;
        char name[21];
    }Plane;
    
    int main()
    {
        Plane seat[12];
        int i;
    
        for (i=0; i<=11; i++){
            seat[i].id = i;                           /*random test value*/
            seat[i].assign = (i+2)%2;        /*random test value*/
            seat[i].name = "fruitless try";
        }
    
        for (i=0; i<=11; i++)
            printf("%-2d   %d   %d\n", seat[i].id, seat[i].assign, seat[i].name);
    
        return 0;
    }
    Last edited by hypertension; 10-14-2002 at 04:12 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    seat[i].name = "fruitless try";

    You cannot assign strings this way. You need to use something like strcpy or strncpy.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    12
    pardon me, but what is the difference between strcpy and sprintf in this case? And what shall I do if I need the user to input a name to the seat[i].name?

    Ps. Tks for the quick reply.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    12
    Code:
    hmmm... 
    
    i assume for the second part, it would be sth like
    
    puts("Please input name");
    gets(name1);
    strcpy(seat[i].name, name1);
    
    but would still appreciated the ans to the difference question and any correction to the above if any would be great.
    
    Tks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM