Thread: accessing variables using structs

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    accessing variables using structs

    Hey guys how would i go about accessing a variable from the item
    struct such as itemID how do i access it?




    Code:
    typedef struct category* CategoryTypePtr;
    typedef struct item* ItemTypePtr;
    
    /* Structure definitions. */
    typedef struct price
    {
       unsigned dollars;
       unsigned cents;
    } PriceType;
    
    typedef struct item
    {
       char itemID[ID_LEN + 1];
       char itemName[MAX_NAME_LEN + 1];
       PriceType prices[NUM_PRICES];
       char itemDescription[MAX_DESC_LEN];
       ItemTypePtr nextItem;
    } ItemType;
    
    typedef struct category
    {
       char categoryID[ID_LEN + 1];
       char categoryName[MAX_NAME_LEN + 1];
       char drinkType;      /* (H)ot or (C)old. */
       char categoryDescription[MAX_DESC_LEN];
       CategoryTypePtr nextCategory;
       ItemTypePtr headItem;
       unsigned numItems;
    } CategoryType;
    
    typedef struct gjc
    {
       CategoryTypePtr headCategory;
       unsigned numCategories;
    } GJCType;

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    {
      ItemType foo;
    
      strcpy(foo.itemID, "bar");
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    would it still work if i go

    Code:
    GJCType new;
    char * dtype;
    
    strcpy(new->headCategory.drinkType, dType);
    so copy the dType char * into the struct would that work

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    No doesn't work. new is not a pointer and drinkType is a char.
    Code:
    GJCType new;
    char  dType= 'C';
    
    new.headCategory->drinkType = dType;
    Kurt
    Last edited by ZuK; 04-25-2006 at 10:37 AM.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Location
    Great India
    Posts
    24
    Guys Guys Guys

    I can't understand what's going on in your conversation ? The original poster of the code asked how to access the variables of an array but you guys are discussing something else.

    For the sake of the original poster i just wanna say that to access the variables of structure use "." operator try the following code:

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    typedef struct item
    {
       int a;
       float b;
    }ITEM;
    
    ITEM var;
    int main()
    {
          
         clrscr();
         printf("\n Enter first variable\t");
         scanf("%d",&var.a);
         printf("\n Enter second variable\t");
         scanf("%f",&var.b);
         printf("\n first value is %d",var.a);
         printf("\n second value is %f",var.b);
         getch();
         return 0;
    }
    
    OUTPUT:
    
    Enter first variable      25
    Enter second variable      62.5
    
    first value is     25
    second value is     62.500000

    I hope you got it. IF you wanna access variables by using pointers then use "->" operator.



    REALNAPSTER

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by realnapster
    Guys Guys Guys

    I can't understand what's going on in your conversation ? The original poster of the code asked how to access the variables of an array but you guys are discussing something else.
    Read again:
    Hey guys how would i go about accessing a variable from the item struct
    The OP's question was about structs, not arrays, which is exactly what I answered.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing variables in a set individually.
    By Dontgiveup in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2009, 05:31 PM
  2. A class to contain structs
    By earth_angel in forum C++ Programming
    Replies: 8
    Last Post: 06-28-2005, 06:08 AM
  3. accessing class variables
    By pug in forum C# Programming
    Replies: 3
    Last Post: 05-20-2005, 08:46 AM
  4. accessing members in nested structs
    By amidstTheAshes in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 02:00 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM