can I write like this?
Code:string[1][0] = *pointer;
can I write like this?
Code:string[1][0] = *pointer;
Depends how string is declared and what 'pointer' is.
If it's a 2d array of characters, and pointer is a pointer to a character then yes.
Why not compile and see for yourself!?
the program compile and run but got error message. Can anyone help me?
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; } }
Last edited by phoebus; 05-01-2008 at 09:08 AM.
From your code it's evident you really have no idea what's going on.
I suggest you start with the basics first, er yes... even more basic than that.
Start by following the tutorials on this site, mainly the arrays & pointer sections.
And also read FAQ
for example Why gets() is bad / Buffer Overflows
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
for print the array of strings. I can use this code right?
and for copying *cmd to historytable[][10]Code:for (i=0; i<=size; i++) puts (historytable[i]);
i can use this code right?
Code:for(i=0;i<=size;i++) historytable[size][0]=*cmd;
Last edited by phoebus; 05-01-2008 at 06:13 AM.
Yes, except you want i < size Remember, arrays are 0 based not 1 based.
ie, 5 1-based is
1, 2, 3, 4, 5
0 based
0, 1, 2, 3, 4
'size' specifies how many elements, not the end point.
This copies first char of 'cmd' to first position of each string in historytable (which is wrong):you have to do it like this:Code:historytable[i][0]=*cmd;this copies "whole" cmd into historytable[0], however you have to be aware to control where 'cmd' points - if 'cmd' and historytable[][] are the same size the you are safe.... hypothetically :-)Code:for (i = 0; i < size; i++) historytable[0][i] = *cmd++; /* copy one character from 'cmd' shift pointer etc. */
this way strlen() is called at every iteration. So:
Code:size=strlen(cmd); for(i = 0; i < size; i++) {...}
this way nul-char is not copied and there are possibilities of memory overrun
there is function strcpy - used for copying C-strings
and better check that strlen(cmd) < 10 before copying...
of cource cmd buffer should be allocated somewhere... and fgets will prevent the memory overrun...
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
okay I fix my main function. Take a look at it. Sorry for the indent.
If I want to use the strcpy function I have initalize
likeCode:char history_table[HISTORY_SIZE][MAX_CMD_LEN];but I don't know how to initialize a array of strings.Code:char string[10]="";
can anyone help?
Do not edit your old posts - post new code version in the new post
See comments
Code: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; /* not initialized */ printf("Enter command [help] for a list of commands\n"); do { printf("cmd> "); /* add fflush(stdout); after this line */ gets(cmd); /* possible memory overrun */ if(strcmp( cmd, "hist") == 0) { printHistory( history_table, history_size); /* no prototype */ } else { flag= processCommand(current, cmd, result); /* no prototype */ } if( cmd !="") /* incorrect operation - use strcmp */ { updateHistory(history_table, cmd, history_size); /* no prototype */ history_size++; } }while (flag!=1); /* used not initialized var if command was hist */ return 0; } void updateHistory( char history[][10], const char *cmd, int size) { if(cmd != '\0'){ history[size][0]=*cmd; /* wrong - use strcpy */ size=size+1; /* has no effect */ } }
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