Thread: Porblem with structures and pointers

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Porblem with structures and pointers

    Hello

    I wrote a program where I used pointers to point to a structure within a function.

    Code:
    typedef struct{
      float Fish;
      float Cotton;
      float Wood;
      float Coco;
      float Sugar;
      float Guns;
      float Textiles;
      float Tobacco;
      float Wine;
    }Goods_g;
    But the problem is that I did calculations in a different function and assigned each element in the structure a value.

    Code:
    int DisplayPort(Goods_g *G, float variables[])
    { 
      G->Fish   = 50*variables[0];
      G->Cotton = 80*variables[1];
      G->Wood = 100*variables[2];
      G->Coco = 140*variables[3];
      G->Sugar = 350*variables[4];
      G->Guns = 700*variables[5];
      G->Textiles =1350*variables[6];
      G->Tobacco =1900*variables[7];
      G->Wine =2150*variables[8];
    }
    I want to use the values assigned to the elements in this function in another function. I don't know how to do so. Any help would be appriciated.

  2. #2
    Sasquatch mog's Avatar
    Join Date
    Dec 2006
    Location
    Caves of Narshe
    Posts
    16
    You can use a pointer just like you do.

    Code:
    struct Goods_g goods;
    
    DisplayPort(&goods, your_floats);
    
    Otherfunction(goods); // will use "goods" populated with values from DisplayPort -function

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Sample code which would do your job

    Code:
    #include <stdio.h>
    
    struct node
    { int num; };
    
    void Init(struct node *no)
    {  no->num = 10; }
    
    void firstchange(struct node *no)
    {  no->num = 20; }
         
    int main()
    {
        struct node tempnode;
        
        Init(&tempnode);
        firstchange(&tempnode);
        
        printf("&#37;d", tempnode.num);
        
        getchar();
        return 0;
    }
    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  2. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  3. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 12:53 PM