GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Oct-2004, 04:37
if13121 if13121 is offline
New Member
 
Join Date: Oct 2004
Posts: 27
if13121 is on a distinguished road
Unhappy

another c simple question


Dear All, I don't know how to make the function
SetFile in this program work without change it become

CPP / C++ / C Code:
void SetFile(Tab * T, IdxType i,kata v)
 {
    kata x;
    CopyKata(v,&x);
    T->TFile[i] = x;
 }

Anybody pls explain more why the SetFile not work.
When should i pass parameter to function by value or by reference?
why both
void CopyKata ( kata Tin , kata *Tout) and
void CopyKata ( kata *Tin , kata *Tout) work.
Acctually this program derived from several file i just cut some funtion from them. Please just ignore that i just assign many variable with zero. here is the program

CPP / C++ / C Code:
 typedef int IdxType;
  typedef struct {
        char TKata[20];
        int len;
  }kata;

  typedef struct
  {
        int DD;
        int MM;
        int YYYY;
  }date;
   typedef struct
  {
        int  TSize[99];
        date TDate[99];
        kata TFile[99];
        int NBFile;
  }  Tab;

void PrintTable(Tab T);
int  NbElmt(Tab T);
void SetFile(Tab *T, IdxType i,kata v);
void CopyKata ( kata  Tin , kata * Tout);
void getLen(kata *L);

int main()
{
        date L;
        Tab T;
        kata Input,v,w;
        T.TDate[0].DD   = 0;    T.TDate[0].MM   = 0;    T.TDate[0].YYYY = 0;
        T.TDate[1].DD   = 0;    T.TDate[1].MM   = 0;    T.TDate[1].YYYY = 0;
        Input.TKata[0] = 't';   Input.TKata[1] = '.';   Input.TKata[2] = 't';
        Input.TKata[3] = 'x';   Input.TKata[4] = 't';   Input.len = 5;
        T.TSize[0]=1;   T.TSize[1]=1;
        T.NBFile = 2;
        CopyKata(Input,&(T.TFile[0]));
	printf("Add New File to table of content\n");
	printf("Input new file name : \n");
        scanf("%s",v.TKata);
        getLen(&v);
        CopyKata(v,&(T.TFile[1]));
        PrintTable(T);
	printf("Input new file name: \n"); 
        scanf("%s",w.TKata);
        getLen(&w);
        T.TDate[2].DD   = 0; T.TDate[2].MM   = 0; T.TDate[2].YYYY = 0;
        SetFile(&T,2,w);
        T.NBFile = 3;
        T.TSize[2]=3;
        PrintTable(T);
        return 0;
}

void PrintTable(Tab T)
 {
    int i,j;
    printf("\nTable of Content\n");
   
    if (NbElmt(T) != 0 )
    {
    for (i = 0; i < NbElmt(T);i++)
      {
         
         for (j = 0; j < T.TFile[i].len ;j++)
         {
         printf("%c",T.TFile[i].TKata[j]);
         }
        
         printf("   %d",T.TSize[i]);
        
         printf("   %d-"  ,T.TDate[i].DD);
         printf("%d-"  ,T.TDate[i].MM);
         printf("%d\n" ,T.TDate[i].YYYY);
      }
    }
    else 
    {printf("There is no file in mydir\n");}
  }

int NbElmt(Tab T)
{
     return(T.NBFile);
}

void CopyKata ( kata  Tin , kata *Tout)
{
        int i;
        (*Tout).len = (Tin).len;
        for (i = 0; i < Tin.len ;i++)
        {
           (*Tout).TKata[i] = (Tin).TKata[i];
        }

}

 void SetFile(Tab * T, IdxType i,kata v)
 {
        CopyKata(v,T.TFile[i]);
 }

void getLen(kata * L)
{
        int i=0;
        while((*L).TKata[i] != '\0')
        {i++;}
        (*L).len = i;
}
Last edited by LuciWiz : 20-Oct-2004 at 05:27. Reason: Please insert your C code between [c] [/c] tags
  #2  
Old 20-Oct-2004, 13:27
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
It wont work becuase, your CopyKata() expect "Tout" argument as a pointer to the varibale of type kata. So you need to pass "address of TFile[i] in (*T) as &((*T).TFile[i])" and not "T.TFile[i]" where in you are passing start address of structre T.

Sorry, if that doesn't explain it well.

Anyway,changing your SetFile() to the one below will work.

CPP / C++ / C Code:
 void SetFile(Tab *T, IdxType i,kata v)
 {
        
		CopyKata(v,&((*T).TFile[i]));
 }

Also, in Copykata() instead of copying single characters at a time using for loop, you can simply use string copy function strcpy() as below.(Unless there is a specific reason for it to do that way).

CPP / C++ / C Code:
void CopyKata ( kata  Tin , kata * Tout)
{
           
		strcpy((*Tout).TKata,Tin.TKata);
}

Same in PrintTable(). you can change it to

CPP / C++ / C Code:
void PrintTable(Tab T)
 {
    int i,j;
    printf("\nTable of Content\n");
   
    if (NbElmt(T) != 0 )
    {
    for (i = 0; i < NbElmt(T);i++)
      {
         
         printf("%10s\t",T.TFile[i].TKata);
         printf("%d\t",T.TSize[i]);
        
         printf("   %d-"  ,T.TDate[i].DD);
         printf("%d-"  ,T.TDate[i].MM);
         printf("%d\n" ,T.TDate[i].YYYY);
      }
    }
    else 
    {printf("There is no file in mydir\n");}
  }

When I complied and executed the code with these changes, I got following output

CPP / C++ / C Code:
TFile[0].TKata: t.txt
Add New File to table of content
Input new file name : 
n1

Table of Content
     t.txt      1    0-0-0
        n1      1    0-0-0
Input new file name: 
n2

Table of Content
     t.txt      1    0-0-0
        n1      1    0-0-0
        n2      3    0-0-0

 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
very simple c question if13121 C Programming Language 1 18-Oct-2004 00:12
New to this forum...just a little question... rune hunter C++ Forum 4 16-Oct-2004 09:33
question of practice magiccreative C++ Forum 1 06-Feb-2004 07:17
Simple Question regarding installing Apache 2.0 macjoubert Apache Web Server Forum 0 13-Nov-2003 11:39

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:06.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.