Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-17-2004, 10:01 PM   #1 (permalink)
if13121
Registered User
 
if13121's Avatar
 
Join Date: Oct 2004
Location: Bandung,Indonesia
Posts: 16
if13121 is on a distinguished road
Unhappy simple c question

Dear All. I'm completely newbie to c. I make a function to copy string and get its length but it doestn't work. What is wrong with this program?

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[100];              
	date TDate[100];		
  	kata TFile[100];    		
	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,aku;
  	T.TDate[1].DD   = 0;	T.TDate[1].MM   = 0;	T.TDate[1].YYYY = 0;
	T.TDate[2].DD   = 0;	T.TDate[2].MM   = 0;	T.TDate[2].YYYY = 0;
	Input.TKata[1] = 't';	Input.TKata[2] = '.';	Input.TKata[3] = 't';
	Input.TKata[4] = 'x';	Input.TKata[5] = 't';	Input.len = 5;
	T.TSize[1]=1;	T.TSize[2]=1;
	T.NBFile = 2;
	CopyKata(Input,&(T.TFile[1]));
	CopyKata(T.TFile[1],&(T.TFile[2]));
	PrintTable(T);
	printf("insert new file to  TOC\n");
	printf("Input file name: \n"); 
	scanf("%s",v.TKata);
	getLen(v);
	printf("file name length: %d\n",v.len);
	T.TDate[3].DD   = 0; T.TDate[3].MM   = 0; T.TDate[3].YYYY = 0;
	SetFile(T,3,v);
	T.NBFile = 3;
	T.TSize[3]=3;
	PrintTable(T);
	return 0;
}

void PrintTable(Tab T)
 {
    int i,j;
    printf("\nTable of Content\n");
   
    if (NbElmt(T) != 0 ) 
    {
    
    for (i = 1; i < (NbElmt(T)+1);i++)
      {
	 
         for (j = 1; j < (T.TFile[i].len + 1);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("The table is empty\n");}
  }

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

 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-1;	  	
}

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

Last edited by Valmont; 10-18-2004 at 05:42 AM.
if13121 is offline   Reply With Quote
Old 10-18-2004, 07:09 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Try to keep control of what you're passing between the different functions:
Code:
void getLen(kata L)
{	
	int i=0;
	while(L.TKata[i ] != '\0')
	{i++;}
	L.len = i-1;	  	
}
Here you're passing a copy of kata, not the object itself, so any assignment of L.len is lost on exit of function scope.
Code:
void getLen(kata* L)
{
  int i=0;
  while(L->TKata[i ] != '\0')
  {i++;}
  L->len = i-1;	  	
}
....
getLen(&v);
will do, what you possibly want here, where the len member of the passed kata is assigned the length (altho you're off by one)

When copying, you need to think in the same direction.
Altho your code does what it should do, theres way too much pointer to pointer arritmetic in it for my taste, instead you can use
Code:
void CopyKata (kata*  Tin , kata *Tout)
{	
  int i;
  Tout->len = Tin->len;
  for (i = 1; i < (Tin->len +1);i++)
  {
    Tout->TKata[i ] = Tin->TKata[i ];
  }
}
...
CopyKata(&Input, &T.TFile[1]);
CopyKata(&T.TFile[1], &T.TFile[2]);
Another thing, I dont like the SetFile() you're using, when passing Tab T to it, you're relying on the usual pass by value effect to handle everything you've put into the Tab struct.
A better way here, would be to use pass by reference aswell.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-18-2004, 07:25 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
if13121
Adding to redhead's comments:
You are mixing up who is responsible for what. You should give the struct(s) more responsibilities.
What is it that you want to achieve exactly. Do you just want a collection of Date objects with a name tag on each Date instance or something? Then I'll rewrite it with your permission.
__________________

Last edited by Valmont; 10-18-2004 at 07:55 AM.
Valmont is offline   Reply With Quote
Old 10-18-2004, 11:20 PM   #4 (permalink)
if13121
Registered User
 
if13121's Avatar
 
Join Date: Oct 2004
Location: Bandung,Indonesia
Posts: 16
if13121 is on a distinguished road
Acctually i make a small program about directory simulation. Acctually i divide the program in some file. The table toc consist of file in the directory its size and its last modified date. I think to post my program i should make it in one file and just consist of several function that is not work. So i just assign the value of some variable above.
I'm sorry if i was wrong. Anyway thank you for the solution.
if13121 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question if13121 Standard C, C++ 10 11-01-2004 07:29 AM
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 10:43 PM
Simple C program Spooky Standard C, C++ 1 10-22-2004 08:26 AM
Really simple question... Ilya020 HTML, XML, Javascript, AJAX 12 10-29-2003 03:10 PM


All times are GMT -8. The time now is 08:52 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting