Thread: string and pointer

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    30

    string and pointer

    can I write like this?
    Code:
    string[1][0] = *pointer;

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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!?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    30

    write to a Array of strings and print to the screen

    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.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    for print the array of strings. I can use this code right?
    Code:
    for (i=0; i<=size; i++)
            puts (historytable[i]);
    and for copying *cmd to historytable[][10]
    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.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    Quote Originally Posted by zacs7 View Post
    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.
    you are right.

    and back to the main code above

    Code:
    size++
    in the updatehistory function, it doesn't add one to the variable history_size. why is that?
    Last edited by phoebus; 05-01-2008 at 06:20 AM.

  9. #9
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    This copies first char of 'cmd' to first position of each string in historytable (which is wrong):
    Code:
    historytable[i][0]=*cmd;
    you have to do it like this:
    Code:
    for (i = 0; i < size; i++)
        historytable[0][i] = *cmd++;  /* copy one character from 'cmd' shift pointer etc. */
    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 :-)

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    Quote Originally Posted by nenpa8lo View Post
    This copies first char of 'cmd' to first position of each string in historytable (which is wrong):
    Code:
    historytable[i][0]=*cmd;
    you have to do it like this:
    Code:
    for (i = 0; i < size; i++)
        historytable[0][i] = *cmd++;  /* copy one character from 'cmd' shift pointer etc. */
    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 :-)
    i would do like this:
    [CODE]
    Code:
    for (i = 0; i <strlen(cmd); i++)
        historytable[size][i] = *cmd++;
    i need to copy 9 more strings
    Last edited by phoebus; 05-01-2008 at 06:57 AM.

  11. #11
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    this way strlen() is called at every iteration. So:
    Code:
    size=strlen(cmd);
    for(i = 0; i < size; i++)
        {...}

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  13. #13
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    First of all can you tell what are you going to do with this code? Secondly there is few mistakes:

    Quote Originally Posted by phoebus View Post
    the program compile and run but got error message. Can anyone help me?

    Code:
    int main(void)
    {
              char historytable[10][10];
               int history_size = 0;
                printf("cmd> ");
    	gets(cmd); //get a command from user.    where is declared 'cmd' ? 
    	updateHistory(historytable, cmd, history_size);
                printHistory( historytable, history_size);
               return 0;
    }
    void printhistory( char hist[][10], int size)
    {
    	int i;
    	printf("Size of history: %d\n", size);
    	for( i=0; i<=size; i++)  size is 0 so why you want to print it in loop ? 
    	{
    		puts(hist[i]);
    	}
    	printf("\n");
    }
    
    void updateHistory( char history[][10], const char *cmd, int size)
    {
    	
    	if(cmd != '\0'){
    		history[size][0]=*cmd;
    		size=size+1;    this makes no sense as 'size' is on the stack! If you want to make permanent changes to size you have to pass it like : updateHistory(...., &size); and then in function body cach it like updateHistory(...., int *size)
    	}
    	
    }

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    okay I fix my main function. Take a look at it. Sorry for the indent.

    Quote Originally Posted by vart View Post
    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...
    If I want to use the strcpy function I have initalize
    Code:
    char history_table[HISTORY_SIZE][MAX_CMD_LEN];
    like
    Code:
    char string[10]="";
    but I don't know how to initialize a array of strings.
    can anyone help?

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

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