Structs

This is a discussion on Structs within the C Programming forums, part of the General Programming Boards category; Hello If i have a char name[40], in my struct, how would I be able to read in a value ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Structs

    Hello

    If i have a char name[40], in my struct, how would I be able to read in a value for the name, and then apply it to the struct, for the char name[] ?

    Thanks

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Question

    U want to know how to read a string value into a struct variables member??

    simple

    struct <whatever> <var>
    scanf("%s",var.name);

    Is that what u were looking for?

    Regards,
    Sriharsha.
    Help everyone you can

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    main()
    {
    typedef struct {

    int year;
    char make[40];
    char colour[40];
    float price;

    } car;
    car number1;

    I would like to ask them for the make of the car, and put it into the make var(above).

    Thanks

  4. #4
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140

    Lightbulb

    printf("Enter make of car > ");
    gets(car->make);

    I hope this is what you're looking for.. let me know... Looking at it now, I'm not sure what the name of your structure is.. try it anyhow(??), if you want...

  5. #5
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    >printf("Enter make of car > ");
    >gets(car->make);

    A tiny Correction...

    gets(number1.make);


    B'Cos, car is not a structure variable and neither number1 is declared as a pointer....(so u cant use the -> operator).


    Regards,
    Sriharsha
    Help everyone you can

  6. #6
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    A tiny Correction...

    gets(number1.make);
    A big Correction...
    Why give the user the opportunity to overwrite memory not assigned for them
    Code:
    fgets( number1.make, sizeof( number1.make ), stdin );
    check your docs for fgets()
    Last edited by C_Coder; 04-16-2002 at 01:39 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

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

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