Thread: Problem accessing the struct object.

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    36

    Problem accessing the struct object.

    I'm having some problem accessing the struct object in multiple functions. Here's the code:

    Code:
    #include<stdio.h>
    #include <malloc.h>
    
    struct os
    {
    	char process ;
    	int arrival_time;
    	int *burst;
    	int *CPU_Bursts;
    	int *IO_Bursts;
    	int l;
    	int num_of_ps;
    	int total;
    };
    
    struct Process
    {
    	int Response_time;
    	int Waiting_time;
    	int Turnaround_time;
    };
    
    void Get_File ();
    void Calculate ();
    
    void Get_File ()
    {
    	char a;
    	int b=0;
    	int j;
    	FILE *far;
    	FILE *f;
    	FILE *far2;
    	struct os pp;
    	pp.num_of_ps=0;
    	pp.total=0;
    ////////////////////////////
    	int m;
    	char s[500];
    	f=fopen ("umer.txt","r");
    	if (!f)
    		printf ("Could not open the file.\n");
    	while (fgets (s,1000,f)!=NULL)
    	{
    		if (s[m] == 'P')
    		{
    			pp.num_of_ps++;
    		}
    		//printf ("%s", s);
    	}
    	printf ("processes are: %d\n", pp.num_of_ps);
    	printf ("\n");
    	fclose(f);
    	struct os *fast;
    	fast=malloc(pp.num_of_ps*sizeof(struct os));
    
    ///////////////////////////
    	far=fopen("umer.txt","r");
    	int i;
    	for(i=0;i<pp.num_of_ps;i++)
    	{
    		fast[i].l=0;
    		fscanf(far,"%c",&fast[i].process);
    		fscanf(far,"%c",&a);
    		fscanf(far,"%d",&fast[i].arrival_time);
    		//printf ("Arrival time of process %d is %d\n", i+1, fast[i].arrival_time);         	
    		fscanf(far,"%c",&a);
    		while (a!='\n')
    		{
    			fscanf(far,"%c",&a);
    		}
    
    		fscanf(far,"%c",&a);
    		while (a!='\n')
    		{
    			if (a>='0')
    			{
    				b++;
    			}
    			else if (a=' ')
    			{
    				b=0;
    			}
    			if (a!=' '&& b==1)
    			{
    				pp.total++;
    				fast[i].l++;
    			}
    			fscanf(far,"%c",&a);
    		}
    		//printf("Bursts for the process %d are %d\n",i+1,fast[i].l);
    		fast[i].burst=malloc(fast[i].l*sizeof(int)); 
    	}
    	fclose(far);
    	int q,zo;
    	char on;
    	far2=fopen("umer.txt","r");
    	for (q=0; q<pp.num_of_ps; q++)
    	{
    		fscanf(far2,"%c",&on);
    		while (on!='\n')
    		{
    			fscanf(far2,"%c",&on);
    		}
    		fscanf(far2,"%c",&on);
    		for (zo=0; zo<fast[q].l; zo++)
    		{
    			fscanf (far2,"%d",&fast[q].burst[zo]);
    			//printf ("%d ", fast[q].burst[zo]);
    		}
    		printf ("\n");
    	}
    	//printf ("%d ", pp.total);
    }
    
    ///////////////I can't access the struct object in this function////////////////
    
    /*void Calculate ()
    {
    	Get_File();
    	struct os obj1;
    	struct os *ptr1;
    	struct Process obj2;
    	struct Process *ptr2;
    	int *CPU;
    	int *IO;
    	int size;
    	int i,zo;
    	printf ("%d ", obj1.total);      // The value should be 44 but it gives 0. That's the problem
    	size=((obj1.total)/2)+1;
    	CPU=malloc(size*sizeof(int));
    	IO=malloc(size*sizeof(int));
    	ptr1=malloc(obj1.num_of_ps*sizeof(struct os));
    }*/
    
    int main()
    {
    	//Get_File ();
    	Calculate ();
    	return 0;
    }
    
    
    What's the problem?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of this:
    Code:
    #include <malloc.h>
    Use:
    Code:
    #include <stdlib.h>
    Anyway, the problem is that you are declaring local variables and then expecting them to behave like global variables. Rather, make use of function parameters: a pointer to obj1 can be passed to the Calculate function, and thus it will be able to access and modify obj1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. problem with struct
    By megazord in forum C Programming
    Replies: 3
    Last Post: 12-25-2009, 10:44 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. DirectMusic engine problem
    By VirtualAce in forum Game Programming
    Replies: 7
    Last Post: 03-17-2005, 06:12 PM