Thread: string and pointer

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    but I don't know how to initialize a array of strings.
    Code:
    char history_table[HISTORY_SIZE][MAX_CMD_LEN] = {0};
    will set the whole array to be contain empty strings
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    Here :
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <ctype.h>
    
    #define HISTORY_SIZE 10
    #define MAX_STRING_LEN 100
    #define  MAX_CMD_LEN 10
    
    void printHistory( char history[][MAX_CMD_LEN], int size);
    int processCommand( char *current, char *cmd, char *result); 
    void updateHistory( char history[][MAX_CMD_LEN], const char *cmd, int size)={0};
    
    int main(void)
    {
    	int i;
    	char cmd[MAX_CMD_LEN]="";
    	char current[MAX_STRING_LEN]="";
    	char result[MAX_STRING_LEN]="";
    	char history_table[HISTORY_SIZE][MAX_CMD_LEN]={};
    	
    	int history_size = 0;
    	int flag;
    
    	printf("Enter command [help] for a list of commands\n");
    
    	do {
    		
    		printf("cmd> ");
                            fflush(stdout)
    		gets(cmd); //get a command from user.
    
                            if(strcmp( cmd, "hist") == 0) {
    			printHistory( history_table, history_size);
    		}
    		else {
    			flag= processCommand(current, cmd, result);
    		}
    
             if( cmd !="")
    {
    	   updateHistory(history_table, cmd, history_size);
    	   history_size++;
    }
    
    	}while (flag!=1);
    	return 0;
    }
    
    void updateHistory( char history[][10], const char *cmd, int size)
    {
    	
    	ctrcpy(history[size],cmd);
    	
    	
    }
    void printHistory( char history[][MAX_CMD_LEN], int size)
    {
    	int i,j;
    	printf("Size of history: &#37;d\n", size);
    	for( i=0; i<size; i++)
    	{
    		puts(history[i]);
    		printf("\t");
    	}
    }
    won't compile
    Code:
    'ctrcpy': identifier not found
    Last edited by phoebus; 05-01-2008 at 09:32 AM.

  3. #18
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    Quote Originally Posted by phoebus View Post
    Here :
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <ctype.h>
    
    #define HISTORY_SIZE 10
    #define MAX_STRING_LEN 100
    #define  MAX_CMD_LEN 10
    
    void printHistory( char history[][MAX_CMD_LEN], int size);
    int processCommand( char *current, char *cmd, char *result); 
    void updateHistory( char history[][MAX_CMD_LEN], const char *cmd, int size);
    
    int main(void)
    {
    	int i;
    	char cmd[MAX_CMD_LEN]="";
    	char current[MAX_STRING_LEN]="";
    	char result[MAX_STRING_LEN]="";
    	char history_table[HISTORY_SIZE][MAX_CMD_LEN];
    	
    	int history_size = 0;
    	int flag;
    
    	printf("Enter command [help] for a list of commands\n");
    
    	do {
    		
    		printf("cmd> ");
    		gets(cmd); //get a command from user.
    
                            if(strcmp( cmd, "hist") == 0) {
    			printHistory( history_table, history_size);
    		}
    		else {
    			flag= processCommand(current, cmd, result);
    		}
                            if( cmd !=""){
    			updateHistory(history_table, cmd, history_size);
    			history_size++;
    		}
    	}while (flag!=1);
    	return 0;
    }
    
    void updateHistory( char history[][10], const char *cmd, int size)
    {
    	
    	if(cmd != '\0'){
    		history[size][0]=*cmd;
    		size=size+1;
    	}
    	
    }
    it run but give no output
    I have funny feeling that you want other people to do your work. If you don't care....

  4. #19
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    you didn't do vart's comments

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    Quote Originally Posted by nenpa8lo View Post
    you didn't do vart's comments
    I read but miss the 0.
    thanks

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    Quote Originally Posted by nenpa8lo View Post
    I have funny feeling that you want other people to do your work. If you don't care....
    because of this ? Vart said make a new post and don't edit the old post so I put it there
    Last edited by phoebus; 05-01-2008 at 09:23 AM.

  7. #22
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    Code:
    'ctrcpy': identifier not found
    what does this mean?

  8. #23
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    i type the wrong word, it should be "strcpy" instead of "ctrcpy"

  9. #24
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM